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