English
HOME | PROJECTS | C++ CLASSES | c\c++ source code | Gallery | Guestbook | BW Servers | Contact me | Login

<< Create a Wav file >>

visits: 2027


Simple program that creates a wav file. In the file there is an A note (440hz) that last one second.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8. long x=0;
  9. FILE *s;
  10. char *head;
  11. char *audio;
  12. char campione;
  13.  
  14. head = new char[44];
  15. audio = new char[8000];
  16.  
  17. sprintf(head, "RIFF");
  18.  
  19. //impostiamo un secondo di audio...
  20. memcpy(&head[4], "\x64\x1f\x00\x00", 4); //num campioni * num canali * bytes per campione
  21.  
  22. sprintf(&head[8], "WAVE");
  23. sprintf(&head[12], "fmt ");
  24. memcpy(&head[16], "\x10\x00\x00\x00", 4);
  25. memcpy(&head[20], "\x01\x00", 2); //audio format
  26. memcpy(&head[22], "\x01\x00", 2); //numero canali
  27. memcpy(&head[24], "\x40\x1f\x00\x00", 4); //frequenza campionamento
  28. memcpy(&head[28], "\x40\x1f\x00\x00", 4); //byterate = frequenza campionamento * numero canali * bit per campione/8
  29. sprintf(&head[32], "\x01\x00"); //allineamento blocchi = bytes per campione * num canali
  30. sprintf(&head[34], "\x08\x00"); //bit per campione
  31. sprintf(&head[36], "data");
  32.  
  33. //impostiamo un secondo di audio...
  34. memcpy(&head[40], "\x40\x1f\x00\x00", 4); //num campioni * num canali * bytes per campione
  35.  
  36. s = fopen("mio_wav.wav", "wb");
  37.  
  38. //scrivo l'header
  39. fwrite(head,1,44,s);
  40.  
  41. //scrivo audio
  42. for(x=0;x<8000;x++){
  43. campione = 128+30*cos((double)x/2.866); //onda sinusoidale a circa 440hz, la nota LA
  44.  
  45. audio[x] = campione;
  46. }
  47. fwrite(audio, 1, 8000, s);
  48.  
  49. fclose(s);
  50.  
  51. return 1;
  52. }
  53.  
Website version: 1.03.01 by mamo139. - All the source codes are intended to be released under "GNU GPL version 3".