c - Gradually slow down audio file from normal speed to half speed? -


i'm trying contents audio file , write them audio file--except output should start @ normal speed , slow down time progresses , finish @ half original speed. i'm trying lagrange interpolation approach can't seem quite right. held off on struct see i'm doing. here's code:

#include <stdio.h> #include <stdlib.h> #include <string.h>  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 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 not exist.\n");         return exit_failure;     }     fread(chunkid, 4, 1, original);     fread(&chunksize, 4, 1, original);     fread(format, 4, 1, original);     fread(subchunk1id, 4, 1, original);     fread(&subchunk1size, 4, 1, original);     fread(&audioformat, 2, 1, original);     fread(&numchannels, 2, 1, original);     fread(&samplerate, 4, 1, original);     fread(&byterate, 4, 1, original);     fread(&blockalign, 2, 1, original);     fread(&bitspersample, 2, 1, original);     fread(subchunk2id, 4, 1, original);     fread(&subchunk2size, 4, 1, original);     fseek(original, 0, seek_set);     slowdown = fopen(dslowdown, "wb");     fwrite(chunkid, 4, 1, slowdown);     fwrite(&chunksize, 4, 1, slowdown);     fwrite(format, 4, 1, slowdown);     fwrite(subchunk1id, 4, 1, slowdown);     fwrite(&subchunk1size, 4, 1, slowdown);     fwrite(&audioformat, 2, 1, slowdown);     fwrite(&numchannels, 2, 1, slowdown);     fwrite(&samplerate, 4, 1, slowdown);     fwrite(&byterate, 4, 1, slowdown);     fwrite(&blockalign, 2, 1, slowdown);     fwrite(&bitspersample, 2, 1, slowdown);     fwrite(subchunk2id, 4, 1, slowdown);     fwrite(&subchunk2size, 4, 1, slowdown);     short int t[4], audio[subchunk2size / 2], f[4];     (i = 0; < subchunk2size / 2; i++) {         fread(&data, 2, 1, original);         audio[i] = data;     }     (i = 0; < 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);     (i = 0; < 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];     }     (i = 0; < subchunk2size; i++) {         audio[i] = audio[f[i]];     }     fwrite(audio, sizeof(audio), 1, slowdown);     fseek(slowdown, 40, seek_set);     fwrite(&subchunk2size, 4, 1, slowdown);     fseek(slowdown, 40, seek_set);     fwrite(audio, sizeof(audio), 1, slowdown);     fseek(slowdown, 4, seek_set);     fwrite(&chunksize, 4, 1, slowdown);     fclose(slowdown);     fclose(original);     return exit_success; } 

i figured out. went struct , redid math.

#include <stdio.h> #include <stdlib.h> #include <string.h>  int main() {     file *original, *fast, *slow, *slowdown;     unsigned int riffsize, fmtsize, datasize;     struct wave {         char chunkid[4];         unsigned int chunksize;         char format[4];         char subchunk1id[4];         unsigned int subchunk1size;         unsigned short int audioformat;         unsigned short int numchannels;         int samplerate;         int byterate;         unsigned short int blockalign;         unsigned short int bitspersample;         char subchunk2id[4];         unsigned int subchunk2size;     } o, f, s, sd;     char path[filename_max], dfast[filename_max], dslow[filename_max], dslowdown[filename_max];     printf("enter path input wave (.wav) file:\n");     gets(path);     strcpy(dfast, path);     dfast[strlen(path) - 4] = '\0';     strcpy(dslow, dfast);     strcpy(dslowdown, dslow);     strcat(dfast, "_fast.wav");     strcat(dslow, "_slow.wav");     strcat(dslowdown, "_slowdown.wav");     original = fopen(path, "rb");     if (!original) {         printf("error: file not exist.\n");         return exit_failure;     }     fread(&o, 44, 1, original);     fseek(original, 0, seek_set);     fread(&f, 44, 1, original);     fseek(original, 0, seek_set);     fread(&s, 44, 1, original);     fseek(original, 0, seek_set);     fread(&sd, 44, 1, original);     fmtsize = o.subchunk1size + 8;     datasize = o.subchunk2size + 8;     riffsize = o.chunksize + 8 - (fmtsize + datasize);     printf("characteristics of %s:\n", path);     printf("riff size:     %d\n", riffsize);     printf("fmt size:      %d\n", fmtsize);     printf("data size:     %d\n\n", datasize);     printf("chunkid:       %.4s\n", o.chunkid);     printf("chunksize:     %d\n", o.chunksize);     printf("format:        %.4s\n\n", o.format);     printf("subchunk1id:   %.4s\n", o.subchunk1id);     printf("subchunk1size: %d\n", o.subchunk1size);     printf("audioformat:   %d\n", o.audioformat);     printf("numchannels:   %d\n", o.numchannels);     printf("samplerate:    %d\n", o.samplerate);     printf("byterate:      %d\n", o.byterate);     printf("blockalign:    %d\n", o.blockalign);     printf("bitspersample: %d\n\n", o.bitspersample);     printf("subchunk2id:   %.4s\n", o.subchunk2id);     printf("subchunk2size: %d\n\n", o.subchunk2size);     fseek(original, 0, seek_set);     fast = fopen(dfast, "wb");     f.samplerate = o.samplerate * 2;     f.byterate = 2 * f.samplerate;     fseek(fast, 0, seek_set);     fwrite(&f, 44, 1, fast);     fmtsize = f.subchunk1size + 8;     datasize = f.subchunk2size + 8;     riffsize = f.chunksize + 8 - (fmtsize + datasize);     printf("\ncharacteristics of %s:\n", dfast);     printf("riff size:     %d\n", riffsize);     printf("fmt size:      %d\n", fmtsize);     printf("data size:     %d\n\n", datasize);     printf("chunkid:       %.4s\n", f.chunkid);     printf("chunksize:     %d\n", f.chunksize);     printf("format:        %.4s\n\n", f.format);     printf("subchunk1id:   %.4s\n", f.subchunk1id);     printf("subchunk1size: %d\n", f.subchunk1size);     printf("audioformat:   %d\n", f.audioformat);     printf("numchannels:   %d\n", f.numchannels);     printf("samplerate:    %d\n", f.samplerate);     printf("byterate:      %d\n", f.byterate);     printf("blockalign:    %d\n", f.blockalign);     printf("bitspersample: %d\n\n", f.bitspersample);     printf("subchunk2id:   %.4s\n", f.subchunk2id);     printf("subchunk2size: %d\n\n", f.subchunk2size);     short int data;     unsigned int i;     (i = 0; < f.subchunk2size / 2; i++) {         fread(&data, 2, 1, original);         fwrite(&data, 2, 1, fast);     }     fclose(fast);     fseek(original, 0, seek_set);     slow = fopen(dslow, "wb");     s.samplerate = o.samplerate / 2;     s.byterate = 2 * s.samplerate;     fseek(slow, 0, seek_set);     fwrite(&s, 44, 1, slow);     printf("\ncharacteristics of %s:\n", dslow);     printf("riff size:     %d\n", riffsize);     printf("fmt size:      %d\n", fmtsize);     printf("data size:     %d\n\n", datasize);     printf("chunkid:       %.4s\n", s.chunkid);     printf("chunksize:     %d\n", s.chunksize);     printf("format:        %.4s\n\n", s.format);     printf("subchunk1id:   %.4s\n", s.subchunk1id);     printf("subchunk1size: %d\n", s.subchunk1size);     printf("audioformat:   %d\n", s.audioformat);     printf("numchannels:   %d\n", s.numchannels);     printf("samplerate:    %d\n", s.samplerate);     printf("byterate:      %d\n", s.byterate);     printf("blockalign:    %d\n", s.blockalign);     printf("bitspersample: %d\n\n", s.bitspersample);     printf("subchunk2id:   %.4s\n", s.subchunk2id);     printf("subchunk2size: %d\n\n", s.subchunk2size);     (i = 0; < s.subchunk2size / 2; i++) {         fread(&data, 2, 1, original);         fwrite(&data, 2, 1, slow);     }     fclose(slow);     fseek(original, 0, seek_set);     slowdown = fopen(dslowdown, "wb");     unsigned int samples;     fseek(original, 44, seek_set);     fseek(slowdown, 44, seek_set);     samples = o.subchunk2size / 2;     double dt = 1/22050.0, out, space, t;     double point[4], ta, tb, tc, td;     unsigned int stamp;     unsigned int count = 0;     short int fd[4], audio;     t = dt;     space = dt;     point[0] = (-1.0) * (-2.0) * (-3.0) * dt;     point[1] = (1.0) * (-1.0) * (-2.0) * dt;     point[2] = (2.0) * (1.0) * (-1.0) * dt;     point[3] = (3.0) * (2.0) * (1.0) * dt;      while (t < (samples - 2) * dt) {         stamp = (int)(t / dt - 1);         fseek(original, 44 + stamp * 2, seek_set);         for(i = 0; < 4; i++) {             fread(&fd[i], 2, 1, original);     }         ta = stamp * dt;         tb = (stamp + 1) * dt;         tc = (stamp + 2) * dt;         td = (stamp + 3);         out = (((t - tb) * (t - tc) * (t - td)) / (point[0])) * fd[0] + (((t - ta) * (t - tc) * (t - td)) / (point[1])) * fd[1] + (((t - ta) * (t - tb) * (t - td)) / (point[2])) * fd[2] + (((t - ta) * (t - tb) * (t - tc)) / (point[3])) * fd[3];         audio = (short int)(out);         fwrite(&audio, 2, 1, slowdown);         space = (((samples - 1) * dt - t) / ((samples - 1) * dt)) * dt + (t / ((samples - 1) * dt)) * .4 * dt;         t += space;         count++;     }     sd.subchunk2size = count * 2;     sd.chunksize = 36 + sd.subchunk2size;     fseek(slowdown, 0, seek_set);     fwrite(&sd, 44, 1, slowdown);     fmtsize = sd.subchunk1size + 8;     datasize = sd.subchunk2size + 8;     riffsize = sd.chunksize + 8 - (fmtsize + datasize);     printf("\ncharacteristics of %s:\n", dslowdown);     printf("riff size:     %d\n", riffsize);     printf("fmt size:      %d\n", fmtsize);     printf("data size:     %d\n\n", datasize);     printf("chunkid:       %.4s\n", sd.chunkid);     printf("chunksize:     %d\n", sd.chunksize);     printf("format:        %.4s\n\n", sd.format);     printf("subchunk1id:   %.4s\n", sd.subchunk1id);     printf("subchunk1size: %d\n", sd.subchunk1size);     printf("audioformat:   %d\n", sd.audioformat);     printf("numchannels:   %d\n", sd.numchannels);     printf("samplerate:    %d\n", sd.samplerate);     printf("byterate:      %d\n", sd.byterate);     printf("blockalign:    %d\n", sd.blockalign);     printf("bitspersample: %d\n\n", sd.bitspersample);     printf("subchunk2id:   %.4s\n", sd.subchunk2id);     printf("subchunk2size: %d\n\n", sd.subchunk2size);     printf("\nas can see, differences between fast/slow output files , original input file sample rate , byte rate. however, slowdown output file differs original input file not in sample rate , byte rate, in data size, chunk size, , subchunk2 size.\n");     fclose(slowdown);     fclose(original);     return exit_success; } 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -