title: how to fill the "data field" of wavfile
body: <p>Hi i am trying to record from a board and i have successfully record 4 seconds. Problem is when i try to record for more time, i got an error telling me that there not enough memory. my target is to record a 5 minutes file. Until now i have create a buffer named snIn[256] where are the samples. i send it to a big buffer of [16K * 4sec] and when it is full, i create the wav file.</p>
  
  <pre><code>#include "SAI_InOut.hpp"
  #include "F746_GUI.hpp"
  #include "Delay.hpp"
  #include "WaveformDisplay.hpp"
  #include "SDFileSystem.h"
  #include "wavfile.h"
  using namespace Mikami;
  
  #define RES_STR_SIZE 0x20
  #define WAVFILE_SAMPLES_PER_SECOND 16000
  #define REC_TIME 4
  
    //Create an SDFileSystem object
   SDFileSystem sd("sd");
  
  bool flag = 1;
  int count = 0;
  char *res_buf; 
  int rp = 0;
  const int NUM_SAMPLES = WAVFILE_SAMPLES_PER_SECOND * REC_TIME;
  Array&lt;int16_t&gt; my_buffer(NUM_SAMPLES);
  int j = 0;
  static const char *target_filename = "/sd/rectest.wav";     
  const int SEG_SIZE = 256;
  int sent_array = 0;
  
  
  int rec(const char *filename, Array&lt;int16_t&gt; my_buffer)
  {   
      j = 0;
      flag = 0;
      sent_array = 0;
  
      WavFileResult result;
      wavfile_info_t info;
      wavfile_data_t data;
  
      WAVFILE_INFO_AUDIO_FORMAT(&amp;info)    = 1;
      WAVFILE_INFO_NUM_CHANNELS(&amp;info)    = 1;
      WAVFILE_INFO_SAMPLE_RATE(&amp;info)     = WAVFILE_SAMPLES_PER_SECOND;
      WAVFILE_INFO_BITS_PER_SAMPLE(&amp;info) = 16;
      WAVFILE_INFO_BYTE_RATE(&amp;info)       = WAVFILE_INFO_NUM_CHANNELS(&amp;info) * WAVFILE_INFO_SAMPLE_RATE(&amp;info) * (WAVFILE_INFO_BITS_PER_SAMPLE(&amp;info) / 8);
      WAVFILE_INFO_BLOCK_ALIGN(&amp;info)     = 2;
  
  
      WAVFILE *wf = wavfile_open(filename, WavFileModeWrite, &amp;result);
      if (result != WavFileResultOK) {
          wavfile_result_string(result, res_buf, RES_STR_SIZE);
          printf("%s", res_buf);
          return result; 
      } else printf ("Open file success \r\n");
  
      rp = 0;
      WAVFILE_DATA_NUM_CHANNELS(&amp;data) = 1;
  
           result = wavfile_write_info(wf, &amp;info);
          if (result != WavFileResultOK) {
              wavfile_result_string(result, res_buf, RES_STR_SIZE);
              printf("%s", res_buf);
              return result; } else printf ("Write info success \r\n");
  
      while ( rp &lt; NUM_SAMPLES ) {
          WAVFILE_DATA_CHANNEL_DATA(&amp;data, 0) = my_buffer[rp];       
          result = wavfile_write_data(wf, &amp;data);
          rp += 1;
      }
  
      if (result != WavFileResultOK) {
          wavfile_result_string(result, res_buf, RES_STR_SIZE);
          printf("%s", res_buf);
          return result; }  else printf ("Write Data file success \r\n");
  
      result = wavfile_close(wf);
      if (result != WavFileResultOK) {
          wavfile_result_string(result, res_buf , RES_STR_SIZE);
          printf("%s", res_buf);
          return result; }  else printf ("Close file success \r\n");
  
      //UnMount the filesystem
      sd.unmount();
  
      printf("Success rec !\r\n");
      return 0;
  }
  
  
  int main()
  {
      //Mount the filesystem
        sd.mount();
  
      const float MAX_DELAY = 0.5f;       // 最大遅延，単位：秒
      const int FS = I2S_AUDIOFREQ_16K;   // 標本化周波数: 16 kHz
      const uint32_t MAX_ARRAY_SIZE = (uint32_t)(MAX_DELAY*FS);
      SaiIO mySai(SaiIO::BOTH, 256, FS, INPUT_DEVICE_DIGITAL_MICROPHONE_2);
      Label myLabel(185, 10, "Delay System", Label::CENTER, Font16);
      // ButtonGroup: "ON", "OFF"
      const uint16_t BG_LEFT = 370;
      const uint16_t BG_WIDTH = 100;
      const uint16_t BG_HEIGHT = 45;
      ButtonGroup onOff(BG_LEFT, 40, BG_WIDTH/2, BG_HEIGHT,
                        2, (string[]){"ON", "OFF"}, 0, 0, 2, 1);
      const uint16_t SB_LEFT = BG_LEFT - 320;
      const uint16_t SB_WIDTH = 270;
      const uint16_t SB_Y0 = 240;
      char str[20];
      sprintf(str, "    %3.1f [s]", MAX_DELAY);
      SeekBar barDelay(SB_LEFT, SB_Y0, SB_WIDTH,
                       0, MAX_ARRAY_SIZE, 0, "0", "", str);
      NumericLabel&lt;float&gt; labelDelay(SB_LEFT+SB_WIDTH/2, SB_Y0-40, "DELEY: %4.2f", 0, Label::CENTER);
      DelaySystem delaySystem(MAX_ARRAY_SIZE);
      WaveformDisplay displayIn(*GuiBase::GetLcdPtr(), SB_LEFT+7, 70, 256, 9,LCD_COLOR_WHITE, LCD_COLOR_CYAN,GuiBase::ENUM_BACK);
      Label inLabel(SB_LEFT-30, 65, "IN");
      WaveformDisplay displayOut(*GuiBase::GetLcdPtr(), SB_LEFT+7, 130, 256, 9,LCD_COLOR_WHITE, LCD_COLOR_CYAN,GuiBase::ENUM_BACK);
      Label outLabel(SB_LEFT-30, 125, "OUT");
  
      int runStop = 1;
      Array&lt;int16_t&gt; snIn(mySai.GetLength());
      Array&lt;int16_t&gt; snOut(mySai.GetLength());
  
      mySai.RecordIn();
      mySai.PlayOut();
      mySai.PauseOut();
  
  
      while (true)
      {
          // On/OFF
          int num;
          if (onOff.GetTouchedNumber(num))
              if (runStop != num)
              {
                  if (num == 0) mySai.ResumeOut();
                  else          mySai.PauseOut();
                  runStop = num;
              }
  
          if (mySai.IsCompleted())
          {
              for (int n=0; n&lt;mySai.GetLength() ; n++)
              {
                  int16_t xL, xR;
                  mySai.Input(xL,xR);
                  int16_t xn = xL + xR;
                  snIn[n] = xn;
                  my_buffer[j] = xn;
                  j++;
                  if (j == NUM_SAMPLES &amp;&amp; flag == 1) {
                      rec (target_filename , my_buffer); } 
  
                  int16_t yn = delaySystem.Execute(xn);
                  mySai.Output(yn, yn);
                  snOut[n] = yn;  
              }
  
              mySai.Reset();     
              displayIn.Execute(snIn);  
  
          }     
      }
  }
  </code></pre>
  
  <p>I thought about a possible solution, to fill directly the "data field" of the wavefile with the snIn[256] buffer (instead of using my_buffer) again and again and at the end close the wavfile. Please let me know what you think about that and other solutions</p>