1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
#include <cstdlib>
#include <iostream>
#include <string>
#include <iomanip>
#include <locale>
#include <sstream>
#include <stdio.h>
using namespace std;
// declare stucture for wav file header elements
struct wav_header
{
char chunk_id[4];
unsigned long chunksize;
char format[4];
char subchunk1_id[4];
unsigned long subchunk1_size;
unsigned short audio_format;
unsigned short num_channels;
unsigned long sample_rate;
unsigned long byte_rate;
unsigned short block_align;
unsigned short bits_per_sample;
char subchunk2_id[4];
unsigned long subchunk2_size;
} wavfile;
/* This function allocates the values to the WAV file header elements and writes the wav file. */
void write_wav_file(float* data, unsigned long no_samples, unsigned long sample_rate, FILE* fptr)
{
// assign header values
wavfile.chunk_id[0] = 'R';
wavfile.chunk_id[1] = 'I';
wavfile.chunk_id[2] = 'F';
wavfile.chunk_id[3] = 'F';
wavfile.format[0] = 'W';
wavfile.format[1] = 'A';
wavfile.format[2] = 'V';
wavfile.format[3] = 'E';
wavfile.subchunk1_id[0] = 'f';
wavfile.subchunk1_id[1] = 'm';
wavfile.subchunk1_id[2] = 't';
wavfile.subchunk1_id[3] = ' ';
wavfile.subchunk1_size = 16;
wavfile.audio_format = 1;
wavfile.num_channels = 1;
wavfile.sample_rate = sample_rate;
wavfile.byte_rate = 2*sample_rate;
wavfile.bits_per_sample = 16;
wavfile.block_align = wavfile.bits_per_sample*wavfile.num_channels/8;
wavfile.subchunk2_id[0] = 'd';
wavfile.subchunk2_id[1] = 'a';
wavfile.subchunk2_id[2] = 't';
wavfile.subchunk2_id[3] = 'a';
wavfile.subchunk2_size = wavfile.block_align*no_samples;
wavfile.chunksize = 36 + wavfile.subchunk2_size;
if (fptr != NULL) // check if file is open ok
{
// Write file data
fwrite(&wavfile, sizeof(wavfile), 1, fptr); // write header
/* Convert samples from floating 0 - 1 range to quantised 16 bit values */
short* quantised;
quantised = (short*) malloc(no_samples*sizeof(short));
for (int i=0;i<no_samples; i++)
quantised[i] = (short) (data[i]*32767);
/* Now write all quantised samples to WAV file */
fwrite(quantised, sizeof(short), no_samples, fptr); // write audio data
free (quantised);
cout << "Written WAV file successfully\n";
}
else cout<< "Unable to write file";
}
// Funtion to allocate memory for audio sample data
void* allocate_memory(unsigned long data_length)
{
void* data;
data = (void*) malloc(data_length*sizeof(float));
return data;
}
/* This function takes the step number and floating point value, */
/* and converts them to text to put into a CSV file for Excel */
void write_CSV(int step, float value, FILE* fptr)
{
if (fptr != NULL)
{
string step_text, value_text; // declare strings which will contain the result
ostringstream convert1; // assign stream used for the conversion
convert1 << step; // insert the textual representation of 'Number' in the characters in the stream
step_text = convert1.str(); // set 'Result' to the contents of the stream
ostringstream convert2; // stream used for the conversion
convert2 << value; // insert the textual representation of 'Number' in the characters in the stream
value_text = convert2.str(); // set 'Result' to the contents of the stream
// cout << step_text <<"\t"<<value_text<<"\n";
char comma=',';
char newline = 10;
for(int i=0; i<step_text.length(); i++)
fwrite(&step_text[i], sizeof(char),1, fptr); // write the step value
fwrite(&comma, sizeof(char), 1, fptr);
for(int i=0; i<value_text.length(); i++)
fwrite(&value_text[i], sizeof(char), 1, fptr); // write the sample value
fwrite(&newline, sizeof(char), 1, fptr);
}
else
cout << "Unable to write file";
}
/* Find the nth root of a number (used to find pitch: one semi-tone = 12 root of 2, times or divide by the frequency) */
double root(double radicand,double degree)
{
return pow(radicand,1.0/degree);
}
|