Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- //02
- #include <stdio.h>
- //03
- #include <stdlib.h>
- //04
- #include <fstream>
- #include "FFT.cpp"
- //05
- using namespace std;
- //06
- //double byteToDouble( char firstByte, char secondByte );
- //07
- //08
- // WAVE PCM soundfile format (you can find more in https://ccrma.stanford.edu/courses/422/projects/WaveFormat/ )
- //09
- typedef struct header_file
- //10
- {
- //11
- char chunk_id[4];
- //12
- int chunk_size;
- //13
- char format[4];
- //14
- char subchunk1_id[4];
- //15
- int subchunk1_size;
- //16
- short int audio_format;
- //17
- short int num_channels;
- //18
- int sample_rate; // sample_rate denotes the sampling rate.
- //19
- int byte_rate;
- //20
- short int block_align;
- //1
- short int bits_per_sample;
- //22
- char subchunk2_id[4];
- //23
- int subchunk2_size; // subchunk2_size denotes the number of samples.
- //24
- //char data; // actual data : Added by tarmizi
- //25
- } header;
- //26
- //27
- typedef struct header_file* header_p;
- //28
- //29
- ///30
- //31
- //32
- double real_in[131072],image_in[131072],Amplitude[131072];
- int main()
- //33
- ////34
- {
- for(int k=0;k<131072;k++)
- {
- real_in[k]=0;
- image_in[k]=0;
- }
- //35
- ofstream myFile;
- //36
- myFile.open("mizi.txt");
- //37
- //38
- //39
- FILE * infile = fopen("C:\\Users\\vip7009pro\\Downloads\\Music\\Casio-MT-45-16-Beat.wav","rb"); // Open wave file in read mode
- //40
- FILE * outfile = fopen("D:\\Output.txt","wb"); // Create output ( wave format) file in write mode;
- //41
- FILE * svFile;
- //42
- //43
- int BUFSIZE = 256; // BUFSIZE can be changed according to the frame size required (eg:512)
- //44
- int count = 0; // For counting number of frames in wave file.
- //45
- short int buff16[BUFSIZE]; // short int used for 16 bit as input data format is 16 bit PCM audio
- //46
- header_p meta = (header_p)malloc(sizeof(header)); // header_p points to a header struct that contains the wave file metadata fields
- //47
- int nb; // variable storing number of bytes returned
- //48
- //49
- if (infile)
- //50
- {
- //51
- fread(meta, 1, sizeof(header), infile);
- //52
- //fwrite(meta,1, sizeof(*meta), outfile);
- //53
- //54
- int tang=0;
- //55
- cout << "first chunk is :" << sizeof(meta->chunk_id) << " bytes in size" << endl;
- //56
- cout << "The file is a :" << meta->chunk_id << " format" << endl;
- //57
- cout << " Size of Header file is "<<sizeof(*meta)<<" bytes" << endl;
- //58
- cout << " Sampling rate of the input wave file is "<< meta->sample_rate <<" Hz" << endl;
- //59
- cout << " Size of data in the audio is: " << sizeof(meta->subchunk2_size)<< " bytes" << endl;
- //60
- cout << " The number of channels of the file is "<< meta->num_channels << " channels" << endl;
- //61
- cout << " The audio format is PCM:"<< meta->audio_format << endl;
- while ((nb = fread(buff16,1,BUFSIZE,infile))>0)
- {
- // Reading data in chunks of BUFSIZE
- //cout << nb <<endl;
- count++;
- // Incrementing > of frame
- for (int i = 0; i<nb; i++) // nb = 256 (frame size)
- {
- // convert the 16 bit samples to double
- int c = (buff16[i]<<8) | buff16[i+1];
- double t = c/32768.0;
- // output the samples to a txt file.
- //cout << data[x] << endl;
- myFile << t <<endl;
- real_in[tang]=t;
- tang++;
- }
- fwrite(buff16,1,nb,outfile); // Writing read data into output file
- }
- cout << " Number of frames in the input wave file are " <<count << endl;
- //90
- /*
- FFT(17,real_in,image_in);
- for(int kk=0;kk<131072;kk++)
- {
- myFile<<kk+1<<": "<<real_in[kk]<<"+"<<image_in[kk]<<"j"<<" Amplitude: ";
- myFile<<sqrt((real_in[kk]*real_in[kk]) + (image_in[kk]*image_in[kk]))<<endl;
- }
- */
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement