test-audioGrab.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "Audio/AudioWavFile.H"
00039 #include "Component/ModelManager.H"
00040 #include "Devices/AudioGrabber.H"
00041 #include "Devices/AudioMixer.H"
00042 #include "GUI/XWindow.H"
00043 #include "Image/DrawOps.H"
00044 #include "Image/Image.H"
00045 #include "Util/log.H"
00046 #include "fstream"
00047 #include <signal.h>
00048
00049 static bool goforever = true;
00050 #define SCALE 4
00051
00052
00053 void terminate(int s) { LERROR("*** INTERRUPT ***"); goforever = false; }
00054
00055 int main(int argc, const char **argv)
00056 {
00057
00058 signal(SIGHUP, terminate); signal(SIGINT, terminate);
00059 signal(SIGQUIT, terminate); signal(SIGTERM, terminate);
00060
00061
00062 ModelManager manager("Test Model for AudioGrabber Class");
00063
00064
00065 nub::soft_ref<AudioMixer> mix(new AudioMixer(manager));
00066 manager.addSubComponent(mix);
00067
00068 nub::soft_ref<AudioGrabber> agb(new AudioGrabber(manager));
00069 manager.addSubComponent(agb);
00070
00071
00072 if (manager.parseCommandLine(argc, argv, "", 0, 0) == false) return(1);
00073
00074 std::ofstream outFile("bit8data.dat");
00075
00076
00077 const uint nsamples = agb->getModelParamVal<uint>("AudioGrabberBufSamples");
00078 int nchan = agb -> getModelParamVal<uint>("AudioGrabberChans");
00079
00080 XWindow win(Dims(nsamples, 256 / SCALE * nchan),
00081 -1, -1, "test-audioGrab window");
00082 Image<byte> ima(nsamples, 256 / SCALE * nchan, NO_INIT);
00083
00084 if (agb->getModelParamVal<uint>("AudioGrabberBits") != 8U)
00085 LFATAL("Sorry, this app only supports 8-bit audio grabbing.");
00086
00087
00088 manager.start();
00089
00090 std::vector<AudioBuffer<byte> > rec;
00091 int totalSamp =0;
00092
00093
00094 while(goforever) {
00095
00096
00097 AudioBuffer<byte> buffer;
00098 agb->grab(buffer);
00099 rec.push_back(buffer);
00100
00101
00102 ima.clear();
00103
00104
00105 Point2D<int> p1, p2;
00106 for (uint i = 1; i < nsamples; i ++)
00107 {
00108 totalSamp++;
00109 p1.i = i - 1;
00110 p1.j = (255 - buffer.getVal(i - 1, 0)) / SCALE;
00111 p2.i = i;
00112 p2.j = (255 - buffer.getVal(i, 0)) / SCALE;
00113 outFile << p1.j << std::endl;
00114
00115 drawLine(ima, p1, p2, byte(255));
00116
00117 if (nchan > 1)
00118 {
00119 p1.i = i - 1;
00120 p1.j = (511 - buffer.getVal(i - 1, 1)) / SCALE;
00121 p2.i = i;
00122 p2.j = (511 - buffer.getVal(i, 1)) / SCALE;
00123 drawLine(ima, p1, p2, byte(255));
00124 }
00125 }
00126
00127
00128 win.drawImage(ima);
00129 }
00130
00131 LINFO("writing out audio file: testAudio.wav");
00132 LINFO("total samps recorded is %d", totalSamp);
00133
00134 writeAudioWavFile("testAudio.wav", rec);
00135
00136
00137 manager.stop();
00138
00139
00140 return 0;
00141 }
00142
00143
00144
00145
00146
00147