<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    <div class="moz-cite-prefix">Hi,<br>
      <br>
      the example you are following just writes the frame out in raw
      format, without doing any kind of sample rate or format
      conversion.<br>
      <br>
      To convert a sample you need to check against the sample format
      (dec_ctx->sample_fmt) and implement the right conversion.<br>
      For example if your audio stream uses a sample format of S16
      (Signed 16-bit integer), converting that to float is really easy
      (Just divide by max int16):<br>
      <br>
          int sampleStride =
      av_get_bytes_per_sample(dec_ctx->sample_fmt);<br>
          for (int sampleIndex = 0; sampleIndex <
      frame->nb_samples; ++sampleIndex) {<br>
              for (int channel = 0; channel < dec_ctx->channels;
      ++channel) {<br>
                // For signed 16-bit integer to float conversion<br>
                  if (dec_ctx->sample_fmt == AV_SAMPLE_FMT_S16) {<br>
                      int16_t *inputPtr = (int16_t *)(frame->data[ch]
      + sampleStride * sampleIndex);<br>
                      int16_t inputSample = *inputPtr;<br>
                      float outputSample;<br>
                      if (inputSample < 0) {<br>
                          outputSample = inputSample / (float)(INT16_MAX
      - 1);<br>
                      } else {<br>
                          outputSample = inputSample / (float)INT16_MAX;<br>
                      }<br>
                  }<br>
              }<br>
          }<br>
      <br>
      Hope this helps.<br>
      <br>
      Greetings,<br>
      Final<br>
      <br>
      Am 20.10.2018 um 11:30 schrieb Matthieu Regnauld:<br>
    </div>
    <blockquote type="cite"
cite="mid:CADjM6tDNfP7UGaFjMhXeJU-KsoefVeO03aKToRA71KKHuf+Amw@mail.gmail.com">
      <meta http-equiv="content-type" content="text/html; charset=utf-8">
      <div dir="ltr">
        <div dir="ltr">
          <div dir="ltr">
            <div dir="ltr">
              <div dir="ltr">Hello,
                <div><br>
                </div>
                <div>I try to extract raw audio frames from an audio
                  file, using the following example: <a
                    href="http://ffmpeg.org/doxygen/3.4/decode_audio_8c-example.html"
                    moz-do-not-send="true">http://ffmpeg.org/doxygen/3.4/decode_audio_8c-example.html</a></div>
                <div><br>
                </div>
                <div>As far as I understand, I get these frames in the <font
                    face="monospace, monospace">decode()</font>
                  function, in the <font face="monospace, monospace">frame->data</font>
                  array.</div>
                <div><br>
                </div>
                <div>That said, I need to convert the frames into floats
                  between -1 and 1. Here is what I tried (in my test, <font
                    face="monospace, monospace">dataSize == 4</font>):</div>
                <div><br>
                </div>
                <div><font face="monospace, monospace">// here is where
                    I want to get my frame:</font></div>
                <div><font face="monospace, monospace">float myFrame;<br>
                    <br>
                    // first try:</font></div>
                <div><font face="monospace, monospace">memcpy(&myFrame,
                    &frame->data[ch][i], dataSize *
                    sizeof(uint8_t));<br>
                  </font></div>
                <div><font face="monospace, monospace"><br
                      class="gmail-Apple-interchange-newline">
                    // second try:<br>
                    myFrame = (frame->data[ch][i]<<0) |
                    (frame->data[ch][i + 1]<<8) |
                    (frame->data[ch][i + 2]<<16) |
                    (frame->data[ch][i + 3]<<24);<br>
                  </font></div>
                <div><font face="monospace, monospace"><br>
                  </font></div>
                <div><font face="monospace, monospace">// third try:<br>
                    myFrame = (frame->data[ch][i + 3]<<0) |
                    (frame->data[ch][i + 2]<<8) |
                    (frame->data[ch][i + 1]<<16) |
                    (frame->data[ch][i]<<24);</font><br>
                </div>
                <div><br>
                </div>
                <div>But the problem is; it doesn't work, all I got so
                  far is some kind of white noice.<br>
                </div>
                <div><br>
                </div>
                <div>So what is the proper way to extract audio frames
                  and convert them to float amplitudes?</div>
                <div><br>
                </div>
                <div>Thanks for your help.</div>
                <div><br>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
Libav-user mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Libav-user@ffmpeg.org">Libav-user@ffmpeg.org</a>
<a class="moz-txt-link-freetext" href="http://ffmpeg.org/mailman/listinfo/libav-user">http://ffmpeg.org/mailman/listinfo/libav-user</a>
</pre>
    </blockquote>
    <p><br>
    </p>
  </body>
</html>