Advertisement
Guest User

WAV to TEXT

a guest
May 7th, 2016
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. #include <iostream>
  2. //02
  3. #include <stdio.h>
  4. //03
  5. #include <stdlib.h>
  6. //04
  7. #include <fstream>
  8. #include "FFT.cpp"
  9. //05
  10. using namespace std;
  11. //06
  12. //double byteToDouble( char firstByte, char secondByte );
  13. //07
  14.  
  15. //08
  16. // WAVE PCM soundfile format (you can find more in https://ccrma.stanford.edu/courses/422/projects/WaveFormat/ )
  17. //09
  18. typedef struct header_file
  19. //10
  20. {
  21. //11
  22. char chunk_id[4];
  23. //12
  24. int chunk_size;
  25. //13
  26. char format[4];
  27. //14
  28. char subchunk1_id[4];
  29. //15
  30. int subchunk1_size;
  31. //16
  32. short int audio_format;
  33. //17
  34. short int num_channels;
  35. //18
  36. int sample_rate; // sample_rate denotes the sampling rate.
  37. //19
  38. int byte_rate;
  39. //20
  40. short int block_align;
  41. //1
  42. short int bits_per_sample;
  43. //22
  44. char subchunk2_id[4];
  45. //23
  46. int subchunk2_size; // subchunk2_size denotes the number of samples.
  47. //24
  48. //char data; // actual data : Added by tarmizi
  49. //25
  50. } header;
  51. //26
  52.  
  53. //27
  54. typedef struct header_file* header_p;
  55. //28
  56.  
  57. //29
  58.  
  59. ///30
  60.  
  61. //31
  62.  
  63. //32
  64. double real_in[131072],image_in[131072],Amplitude[131072];
  65.  
  66.  
  67. int main()
  68. //33
  69.  
  70. ////34
  71. {
  72. for(int k=0;k<131072;k++)
  73. {
  74. real_in[k]=0;
  75. image_in[k]=0;
  76. }
  77. //35
  78. ofstream myFile;
  79. //36
  80. myFile.open("mizi.txt");
  81. //37
  82.  
  83. //38
  84.  
  85. //39
  86. FILE * infile = fopen("C:\\Users\\vip7009pro\\Downloads\\Music\\Casio-MT-45-16-Beat.wav","rb"); // Open wave file in read mode
  87. //40
  88. FILE * outfile = fopen("D:\\Output.txt","wb"); // Create output ( wave format) file in write mode;
  89. //41
  90. FILE * svFile;
  91. //42
  92.  
  93. //43
  94. int BUFSIZE = 256; // BUFSIZE can be changed according to the frame size required (eg:512)
  95. //44
  96. int count = 0; // For counting number of frames in wave file.
  97. //45
  98. short int buff16[BUFSIZE]; // short int used for 16 bit as input data format is 16 bit PCM audio
  99. //46
  100. header_p meta = (header_p)malloc(sizeof(header)); // header_p points to a header struct that contains the wave file metadata fields
  101. //47
  102. int nb; // variable storing number of bytes returned
  103. //48
  104.  
  105. //49
  106. if (infile)
  107. //50
  108. {
  109. //51
  110. fread(meta, 1, sizeof(header), infile);
  111. //52
  112. //fwrite(meta,1, sizeof(*meta), outfile);
  113. //53
  114.  
  115. //54
  116. int tang=0;
  117. //55
  118. cout << "first chunk is :" << sizeof(meta->chunk_id) << " bytes in size" << endl;
  119. //56
  120. cout << "The file is a :" << meta->chunk_id << " format" << endl;
  121. //57
  122. cout << " Size of Header file is "<<sizeof(*meta)<<" bytes" << endl;
  123. //58
  124. cout << " Sampling rate of the input wave file is "<< meta->sample_rate <<" Hz" << endl;
  125. //59
  126. cout << " Size of data in the audio is: " << sizeof(meta->subchunk2_size)<< " bytes" << endl;
  127. //60
  128. cout << " The number of channels of the file is "<< meta->num_channels << " channels" << endl;
  129. //61
  130. cout << " The audio format is PCM:"<< meta->audio_format << endl;
  131. while ((nb = fread(buff16,1,BUFSIZE,infile))>0)
  132. {
  133. // Reading data in chunks of BUFSIZE
  134. //cout << nb <<endl;
  135. count++;
  136. // Incrementing > of frame
  137. for (int i = 0; i<nb; i++) // nb = 256 (frame size)
  138. {
  139.  
  140. // convert the 16 bit samples to double
  141. int c = (buff16[i]<<8) | buff16[i+1];
  142. double t = c/32768.0;
  143. // output the samples to a txt file.
  144. //cout << data[x] << endl;
  145. myFile << t <<endl;
  146. real_in[tang]=t;
  147. tang++;
  148.  
  149.  
  150. }
  151.  
  152.  
  153.  
  154.  
  155. fwrite(buff16,1,nb,outfile); // Writing read data into output file
  156. }
  157. cout << " Number of frames in the input wave file are " <<count << endl;
  158. //90
  159.  
  160. /*
  161. FFT(17,real_in,image_in);
  162. for(int kk=0;kk<131072;kk++)
  163. {
  164. myFile<<kk+1<<": "<<real_in[kk]<<"+"<<image_in[kk]<<"j"<<" Amplitude: ";
  165. myFile<<sqrt((real_in[kk]*real_in[kk]) + (image_in[kk]*image_in[kk]))<<endl;
  166. }
  167.  
  168. */
  169. return 0;
  170. }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement