title: FFmpeg.swr_convert: audio to raw 16 bit pcm, to be used with xna SoundEffect. Audio cuts out when i convert
body: <p>I want to resample mkv(vp8/ogg) and also raw 4 bit adpcm to raw 16bit pcm byte[] to be loaded into SoundEffect from xna library. So I can play it out while I'm using other code to display the frames (the video side is working).
  I can read a 16 bit wav file and play it. But when I goto resample something it doesn't play 100%. One file is 3 mins and 15 secs. I only get 13 sec and 739 ms before it quits playing. I have been learning to do this by finding code samples in c++ and correcting it to work in c# using ffmpeg.autogen.</p>
  
  <p>the below is my best attempt at resampling.</p>
  
  <pre><code>            int nb_samples = Frame-&gt;nb_samples;
                      int output_nb_samples = nb_samples;
                      int nb_channels = ffmpeg.av_get_channel_layout_nb_channels(ffmpeg.AV_CH_LAYOUT_STEREO);
                      int bytes_per_sample = ffmpeg.av_get_bytes_per_sample(AVSampleFormat.AV_SAMPLE_FMT_S16) * nb_channels;
                      int bufsize = ffmpeg.av_samples_get_buffer_size(null, nb_channels, nb_samples,
                                                               AVSampleFormat.AV_SAMPLE_FMT_S16, 1);
  
                      byte*[] b = Frame-&gt;data;
                      fixed (byte** input = b)
                      {
                          byte* output = null;
                          ffmpeg.av_samples_alloc(&amp;output, null,
                              nb_channels,
                              nb_samples,
                              (AVSampleFormat)Frame-&gt;format, 0);//
  
                          // Buffer input
  
                          Ret = ffmpeg.swr_convert(Swr, &amp;output, output_nb_samples / 2, input, nb_samples);
                          CheckRet();
                          WritetoMs(output, 0, Ret * bytes_per_sample);
                          output_nb_samples -= Ret;
  
                          // Drain buffer
                          while ((Ret = ffmpeg.swr_convert(Swr, &amp;output, output_nb_samples, null, 0)) &gt; 0)
                          {
                              CheckRet();
                              WritetoMs(output, 0, Ret * bytes_per_sample);
                              output_nb_samples -= Ret;
                          }
                      }
  </code></pre>
  
  <p>I changed that all to this but it cuts off sooner.</p>
  
  <pre><code>  Channels = ffmpeg.av_get_channel_layout_nb_channels(OutFrame-&gt;channel_layout);
                      int nb_channels = ffmpeg.av_get_channel_layout_nb_channels(ffmpeg.AV_CH_LAYOUT_STEREO);
                      int bytes_per_sample = ffmpeg.av_get_bytes_per_sample(AVSampleFormat.AV_SAMPLE_FMT_S16) * nb_channels;
  
                      if((Ret = ffmpeg.swr_convert_frame(Swr, OutFrame, Frame))&gt;=0)
                          WritetoMs(*OutFrame-&gt;extended_data, 0, OutFrame-&gt;nb_samples * bytes_per_sample);
                      CheckRet();
  </code></pre>
  
  <p>Both code use a function to set Swr it runs one time after the first frame is decoded.</p>
  
  <pre><code>        private void PrepareResampler()
      {
          ffmpeg.av_frame_copy_props(OutFrame, Frame);
          OutFrame-&gt;channel_layout = ffmpeg.AV_CH_LAYOUT_STEREO;
          OutFrame-&gt;format = (int)AVSampleFormat.AV_SAMPLE_FMT_S16;
          OutFrame-&gt;sample_rate = Frame-&gt;sample_rate;
          OutFrame-&gt;channels = 2;
          Swr = ffmpeg.swr_alloc();
          if (Swr == null)
              throw new Exception("SWR = Null");
          Ret = ffmpeg.swr_config_frame(Swr, OutFrame, Frame);
          CheckRet();
          Ret = ffmpeg.swr_init(Swr);
          CheckRet();
          Ret = ffmpeg.swr_is_initialized(Swr);
          CheckRet();
      }
  </code></pre>
  
  <p>This is where I take the output and put it in the sound effect</p>
  
  <pre><code>private void ReadAll()
      {
  
          using (Ms = new MemoryStream())
          {
              while (true)
              {
                  Ret = ffmpeg.av_read_frame(Format, Packet);
                  if (Ret == ffmpeg.AVERROR_EOF)
                      break;
                  CheckRet();
                  Decode();
              }
              if (Ms.Length &gt; 0)
              {
                  se = new SoundEffect(Ms.ToArray(), 0, (int)Ms.Length, OutFrame-&gt;sample_rate, (AudioChannels)Channels, 0, 0);
                  //se.Duration; Stream-&gt;duration;
  
  
                  see = se.CreateInstance();
                  see.Play();
              } 
          }
      }
  </code></pre>