Main Page   Namespace List   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   Related Pages  

main.cpp

00001 //----------------------------------------------------------------------------
00002 #include <iostream>
00003 #include <vector>
00004 #include <string>
00005 #include <sstream>
00006 #include <cmath>
00007 #include "WAVReader.h"
00008 #include "WAVWriter.h"
00009 #include "Synth.h"
00010 //----------------------------------------------------------------------------
00011 using namespace std;
00012 using namespace ac3;
00013 //----------------------------------------------------------------------------
00014 #define SAMPLERATE 44100
00015 #define LENGTH 6
00016 //----------------------------------------------------------------------------
00017 int main()
00018 {
00019   // Create a stereo demo sound, don't care about it
00020   cout << "Creating demo sound...";
00021   
00022   CSynthBuffer b[4];
00023   int i;
00024   for (i = 0; i < 4; ++i)
00025   {
00026     b[i].CreateBuffer(SAMPLERATE, LENGTH*SAMPLERATE);
00027     b[i].FillDB(0, LENGTH*SAMPLERATE, -6.0);
00028   }
00029   
00030   b[0].Sinus(0, LENGTH/3*SAMPLERATE, 78.0, 110.0, 0.0);
00031   b[0].Sinus(LENGTH/3*SAMPLERATE, 2*LENGTH/3*SAMPLERATE, 112.0, 112.0, 0.0);
00032   b[1].Sinus(0, LENGTH*SAMPLERATE, 218.0, 217.0, 0.0);
00033   b[2].Sinus(0, LENGTH*SAMPLERATE, 222.0, 223.0, 0.0);
00034   b[3].Sinus(0, LENGTH/3*SAMPLERATE, 311.0, 446.0, 0.0);
00035   b[3].Sinus(LENGTH/3*SAMPLERATE, 2*LENGTH/3*SAMPLERATE, 446.0, 446.0, 0.0);
00036 
00037   CSynthBuffer res1(b, 2);
00038   CSynthBuffer res2(&b[2], 2);
00039   res1.SinusSqr(0, LENGTH*SAMPLERATE, 20.0, 20.0, 0.0);
00040   res2.SinusSqr(0, LENGTH*SAMPLERATE, 20.0, 20.0, 0.0);
00041 
00042   cout << "ready" << endl;
00043 
00044   // We create one buffer for each channel and store the demo sound to them.
00045   // We will create a 32 bit stereo WAV-file in the first step
00046   vector<INT32> ch1_32bit(LENGTH*SAMPLERATE);
00047   vector<INT32> ch2_32bit(LENGTH*SAMPLERATE);
00048   
00049   // Now transfer the content of the demo sound to the buffers.
00050   for (i = 0; i < LENGTH*SAMPLERATE; ++i)
00051   {
00052     ch1_32bit[i] = (INT32)floor(res1.m_samples[i] * 0x7FFFFFFF + 0.5);
00053     ch2_32bit[i] = (INT32)floor(res2.m_samples[i] * 0x7FFFFFFF + 0.5);
00054   }
00055 
00056   // So now we can write the data to a file.
00057   try
00058   {
00059     cout << "Writing demo sound to a 32 bit stereo WAV-file...";
00060     CWAVWriter writer;
00061   
00062     // We create a file.
00063     writer.OpenFile("32bit_stereo.wav", SAMPLERATE, 2, 32);
00064 
00065     // Now we need an array containing pointers to the buffers
00066     // of each channel.
00067     INT32* pSources[2] = { &ch1_32bit[0], &ch2_32bit[0] };
00068 
00069     // We are ready to write the data
00070     writer.Write32BitSamples(pSources, LENGTH*SAMPLERATE);
00071 
00072     // Note, that it is possible to call the WritexxBitSamples functions
00073     // any number of time in any combination writing any amount data to
00074     // it.
00075     // Actually we don't have to do that because our WAV-file is very small.
00076 
00077     writer.CloseFile();
00078     cout << "ready" << endl;
00079 
00080     // At this point we will read our just created WAV-file.
00081     CWAVReader reader;
00082     reader.OpenFile("32bit_stereo.wav");
00083 
00084     // We assume that we don't know anything about the actual WAV-file.
00085     // In this step we will write every channel of the file to a
00086     // seperate 16 bit mono file.
00087 
00088     // We will use a 24 bit buffer with a size of 1024 samples to store
00089     // the data from the source. This is only to demonstrate that both
00090     // classes are able to do the bit rate conversion automatically.
00091     // So the 32 bit source samples are converted to 24 bit and when
00092     // written to the file they are converted to 16 bit.
00093     // However, 24 bit samples are stored in 32 bit integers, so our
00094     // buffer has INT32 as type.
00095     vector<INT32> buffer(1024, 0);
00096     for (i = 0; i < reader.GetChannelCount(); ++i)
00097     {
00098       cout << "Writing channel " << i << " of WAV-file...";
00099       
00100       ostringstream os;
00101       os << "ch" << i << "_16bit_mono.wav";
00102       string sFilename(os.str());
00103       writer.OpenFile(sFilename, SAMPLERATE, 1, 16);
00104 
00105       UINT32 nSampleCount = reader.GetSampleCount();
00106       while (nSampleCount > 0)
00107       {
00108         UINT32 nToRead = (nSampleCount > 1024)? 1024 : nSampleCount;
00109         reader.GetSamplesAs24Bit(nToRead, i, &buffer[0]);
00110         INT32* pSource[1] = { &buffer[0] };
00111         writer.Write24BitSamples(pSource, nToRead);
00112         nSampleCount -= nToRead;
00113       }
00114       writer.CloseFile();
00115       cout << "ready" << endl;
00116     }
00117   }
00118   catch (CWAVWriterException wwe)
00119   {
00120     // Here we will get if an WAV-writer error occurs
00121     cout << "Writer error: " << wwe.GetString() << endl;
00122     return -1;
00123   }
00124   catch (CWAVReaderException wre)
00125   {
00126     // Here we will get if an WAV-reader error occurs
00127     cout << "Reader error: " << wre.GetString() << endl;
00128     return -1;
00129   }
00130 
00131   return 0;
00132 }
00133 //----------------------------------------------------------------------------

Generated on Sat Sep 25 14:06:37 2004 for ac3 by doxygen1.2.18