title: Gradually slow down audio file from normal speed to half speed?
body: <p>I'm trying to get the contents from an audio file and write them into another audio file--except the output should start at the normal speed and then slow down as time progresses and finish at half its original speed. I'm trying the Lagrange interpolation approach but can't seem to get it quite right. I held off on struct for now just to see what I'm doing. Here's my code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  int main() {
      FILE *original, *slowdown;
      unsigned int ChunkSize, Subchunk1Size, Subchunk2Size, RIFFSize, fmtSize, dataSize, SampleRate, ByteRate;
      unsigned short int AudioFormat, NumChannels, BlockAlign, BitsPerSample;
      char ChunkID[5], Format[5], Subchunk1ID[5], Subchunk2ID[5];
      ChunkID[4] = '\0';
      Format[4] = '\0';
      Subchunk1ID[4] = '\0';
      Subchunk2ID[4] = '\0';
      char path[FILENAME_MAX], dslowdown[FILENAME_MAX];
      printf("Enter path to Alejandro_project.wav file:\n");
      scanf("%s", path);
      strcpy(dslowdown, path);
      dslowdown[strlen(path) - 21] = '\0';
      strcat(dslowdown, "Alejandro_fast.wav");
      original = fopen(path, "rb");
      if (!original) {
          printf("Error: file does not exist.\n");
          return EXIT_FAILURE;
      }
      fread(ChunkID, 4, 1, original);
      fread(&amp;ChunkSize, 4, 1, original);
      fread(Format, 4, 1, original);
      fread(Subchunk1ID, 4, 1, original);
      fread(&amp;Subchunk1Size, 4, 1, original);
      fread(&amp;AudioFormat, 2, 1, original);
      fread(&amp;NumChannels, 2, 1, original);
      fread(&amp;SampleRate, 4, 1, original);
      fread(&amp;ByteRate, 4, 1, original);
      fread(&amp;BlockAlign, 2, 1, original);
      fread(&amp;BitsPerSample, 2, 1, original);
      fread(Subchunk2ID, 4, 1, original);
      fread(&amp;Subchunk2Size, 4, 1, original);
      fseek(original, 0, SEEK_SET);
      slowdown = fopen(dslowdown, "wb");
      fwrite(ChunkID, 4, 1, slowdown);
      fwrite(&amp;ChunkSize, 4, 1, slowdown);
      fwrite(Format, 4, 1, slowdown);
      fwrite(Subchunk1ID, 4, 1, slowdown);
      fwrite(&amp;Subchunk1Size, 4, 1, slowdown);
      fwrite(&amp;AudioFormat, 2, 1, slowdown);
      fwrite(&amp;NumChannels, 2, 1, slowdown);
      fwrite(&amp;SampleRate, 4, 1, slowdown);
      fwrite(&amp;ByteRate, 4, 1, slowdown);
      fwrite(&amp;BlockAlign, 2, 1, slowdown);
      fwrite(&amp;BitsPerSample, 2, 1, slowdown);
      fwrite(Subchunk2ID, 4, 1, slowdown);
      fwrite(&amp;Subchunk2Size, 4, 1, slowdown);
      short int t[4], audio[Subchunk2Size / 2], f[4];
      for (i = 0; i &lt; Subchunk2Size / 2; i++) {
          fread(&amp;data, 2, 1, original);
          audio[i] = data;
      }
      for (i = 0; i &lt; sizeof(t) / 2; i++)
          t[i + 1] = t[i] + (11.41 - t[i]) / 11.41 * (1.0 / 22050.0) + t[i] / 11.41 * (0.4 / 22050.0);
      for (i = 0; i &lt; sizeof(f) / 2; i++) {
          f[i + 1] = ((t[i] - t[i - 1]) * (t[i] - t[i + 1]) * (t[i] - t[i + 2])) / ((t[i - 2] - t[i - 1]) * (t[i - 2] - t[i + 1]) * (t[i - 2] - t[i + 2])) * f[i - 2] + ((t[i] - t[i - 2]) * (t[i] - t[i + 1]) * (t[i] - t[i + 2])) / ((t[i - 1] - t[i - 2]) * (t[i - 1] - t[i + 1]) * (t[i - 1] - t[i + 2])) * f[i - 1] + ((t[i] - t[i - 2]) * (t[i] - t[i - 1]) * (t[i] - t[i + 2])) / ((t[i + 1] - t[i - 2]) * (t[i + 1] - t[i - 1]) * (t[i + 1] - t[i + 2])) * f[i + 1] + ((t[i] - t[i - 2]) * (t[i] - t[i - 1]) * (t[i] - t[i + 2])) / ((t[i + 1] - t[i - 2]) * (t[i + 1] - t[i - 1]) * (t[i + 1] - t[i + 2])) * f[i + 2];
      }
      for (i = 0; i &lt; Subchunk2Size; i++) {
          audio[i] = audio[f[i]];
      }
      fwrite(audio, sizeof(audio), 1, slowdown);
      fseek(slowdown, 40, SEEK_SET);
      fwrite(&amp;Subchunk2Size, 4, 1, slowdown);
      fseek(slowdown, 40, SEEK_SET);
      fwrite(audio, sizeof(audio), 1, slowdown);
      fseek(slowdown, 4, SEEK_SET);
      fwrite(&amp;ChunkSize, 4, 1, slowdown);
      fclose(slowdown);
      fclose(original);
      return EXIT_SUCCESS;
  }
  </code></pre>