Mercurial > hg > beaglert
comparison projects/basic_writeFile/render.cpp @ 153:3b7270949a97
Added WriteFile class to log data to disc in a low priority thread
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Wed, 07 Oct 2015 20:58:53 +0100 |
parents | |
children | cb47043c8c28 |
comparison
equal
deleted
inserted
replaced
145:8ff5668bbbad | 153:3b7270949a97 |
---|---|
1 #include <BeagleRT.h> | |
2 #include <Scope.h> | |
3 #include <cmath> | |
4 #include <WriteFile.h> | |
5 | |
6 float gPhase1, gPhase2; | |
7 float gFrequency1, gFrequency2; | |
8 float gInverseSampleRate; | |
9 | |
10 WriteFile file1; | |
11 WriteFile file2; | |
12 | |
13 bool setup(BeagleRTContext *context, void *userData) | |
14 { | |
15 gInverseSampleRate = 1.0/context->audioSampleRate; | |
16 file1.init("out1.m"); //set the file name to write to | |
17 file1.setHeader("myvar=[\n"); //set a line to be printed at the beginning of the file | |
18 file1.setFooter("];\n"); //set a line to be printed at the end of the file | |
19 file1.setEcho(true); // enable echo to the console (as well as to the file) | |
20 file1.setFormat("%.5f %.10f %f\n"); // set the format that you want to use for your output. Please use %f only (with modifiers) | |
21 file2.init("out2.m"); | |
22 file2.setHeader("input=[\n"); | |
23 file2.setFooter("];\n"); | |
24 file2.setEcho(false); | |
25 file2.setFormat("%f\n"); | |
26 gPhase1 = 0.0; | |
27 gPhase2 = 0.0; | |
28 | |
29 gFrequency1 = 200.0; | |
30 gFrequency2 = 201.0; | |
31 return true; | |
32 } | |
33 | |
34 void render(BeagleRTContext *context, void *userData) | |
35 { | |
36 static int count = 0; | |
37 if((count&16383) == 0){ | |
38 file2.log(context->audioIn, context->audioFrames); //write the input buffer every so often | |
39 } | |
40 for(unsigned int n = 0; n < context->audioFrames; n++) { | |
41 float chn1 = sinf(gPhase1); | |
42 float chn2 = sinf(gPhase2); | |
43 gPhase1 += 2.0 * M_PI * gFrequency1 * gInverseSampleRate; | |
44 gPhase2 += 2.0 * M_PI * gFrequency2 * gInverseSampleRate; | |
45 if(gPhase1 > 2.0 * M_PI) | |
46 gPhase1 -= 2.0 * M_PI; | |
47 if(gPhase2 > 2.0 * M_PI) | |
48 gPhase2 -= 2.0 * M_PI; | |
49 if( (count&511) == 0){ | |
50 file1.log(chn1); | |
51 file1.log(chn2); | |
52 file1.log(count); | |
53 } | |
54 count++; | |
55 } | |
56 } | |
57 | |
58 // cleanup_render() is called once at the end, after the audio has stopped. | |
59 // Release any resources that were allocated in initialise_render(). | |
60 | |
61 void cleanup(BeagleRTContext *context, void *userData) | |
62 { | |
63 | |
64 } |