annotate examples/07-DataLogging/basic-writeFile/render.cpp @ 480:4ff80956c27a prerelease

heavy supports digitals at message rate
author Giulio Moro <giuliomoro@yahoo.it>
date Tue, 21 Jun 2016 05:44:21 +0100
parents 8fcfbfb32aa0
children
rev   line source
robert@464 1 /*
robert@464 2 ____ _____ _ _
robert@464 3 | __ )| ____| | / \
robert@464 4 | _ \| _| | | / _ \
robert@464 5 | |_) | |___| |___ / ___ \
robert@464 6 |____/|_____|_____/_/ \_\
robert@464 7
robert@464 8 The platform for ultra-low latency audio and sensor processing
robert@464 9
robert@464 10 http://bela.io
robert@464 11
robert@464 12 A project of the Augmented Instruments Laboratory within the
robert@464 13 Centre for Digital Music at Queen Mary University of London.
robert@464 14 http://www.eecs.qmul.ac.uk/~andrewm
robert@464 15
robert@464 16 (c) 2016 Augmented Instruments Laboratory: Andrew McPherson,
robert@464 17 Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack,
robert@464 18 Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved.
robert@464 19
robert@464 20 The Bela software is distributed under the GNU Lesser General Public License
robert@464 21 (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt
robert@464 22 */
robert@464 23
robert@464 24
robert@464 25 #include <Bela.h>
robert@464 26 #include <cmath>
robert@464 27 #include <WriteFile.h>
robert@464 28
robert@464 29 float gPhase1, gPhase2;
robert@464 30 float gFrequency1, gFrequency2;
robert@464 31 float gInverseSampleRate;
robert@464 32
robert@464 33 WriteFile file1;
robert@464 34 WriteFile file2;
robert@464 35
robert@464 36 bool setup(BelaContext *context, void *userData)
robert@464 37 {
robert@464 38 gInverseSampleRate = 1.0/context->audioSampleRate;
robert@464 39 file1.init("out1.m"); //set the file name to write to
robert@464 40 file1.setHeader("myvar=[\n"); //set a line to be printed at the beginning of the file
robert@464 41 file1.setFooter("];\n"); //set a line to be printed at the end of the file
robert@464 42 file1.setEcho(true); // enable echo to the console (as well as to the file)
robert@464 43 file1.setFormat("%.5f %.10f %f\n"); // set the format that you want to use for your output. Please use %f only (with modifiers)
robert@464 44 file2.init("out2.m");
robert@464 45 file2.setHeader("input=[\n");
robert@464 46 file2.setFooter("];\n");
robert@464 47 file2.setEcho(false);
robert@464 48 file2.setFormat("%f\n");
robert@464 49 gPhase1 = 0.0;
robert@464 50 gPhase2 = 0.0;
robert@464 51
robert@464 52 gFrequency1 = 200.0;
robert@464 53 gFrequency2 = 201.0;
robert@464 54 return true;
robert@464 55 }
robert@464 56
robert@464 57 void render(BelaContext *context, void *userData)
robert@464 58 {
robert@464 59 static int count = 0;
robert@464 60 if((count&16383) == 0){
robert@464 61 file2.log(context->audioIn, context->audioFrames); //write the input buffer every so often
robert@464 62 }
robert@464 63 for(unsigned int n = 0; n < context->audioFrames; n++) {
robert@464 64 float chn1 = sinf(gPhase1);
robert@464 65 float chn2 = sinf(gPhase2);
robert@464 66 gPhase1 += 2.0 * M_PI * gFrequency1 * gInverseSampleRate;
robert@464 67 gPhase2 += 2.0 * M_PI * gFrequency2 * gInverseSampleRate;
robert@464 68 if(gPhase1 > 2.0 * M_PI)
robert@464 69 gPhase1 -= 2.0 * M_PI;
robert@464 70 if(gPhase2 > 2.0 * M_PI)
robert@464 71 gPhase2 -= 2.0 * M_PI;
robert@464 72 if( (count&511) == 0){
robert@464 73 file1.log(chn1);
robert@464 74 file1.log(chn2);
robert@464 75 file1.log(count);
robert@464 76 }
robert@464 77 count++;
robert@464 78 }
robert@464 79 }
robert@464 80
robert@464 81 // cleanup_render() is called once at the end, after the audio has stopped.
robert@464 82 // Release any resources that were allocated in initialise_render().
robert@464 83
robert@464 84 void cleanup(BelaContext *context, void *userData)
robert@464 85 {
robert@464 86
robert@464 87 }
robert@464 88
robert@464 89 /* ------------ Project Explantation ------------ */
robert@464 90
robert@464 91 /**
robert@464 92 \example 07-write-file
robert@464 93
robert@464 94 Writing data to a file
robert@464 95 ---------------------------
robert@464 96
robert@464 97 This sketch demonstrates how to log values from within a project for later processing or analysis.
robert@464 98
robert@464 99 */
robert@464 100