Mercurial > hg > beaglert
comparison projects/basic/render.cpp @ 108:3068421c0737 ultra-staging
Merged default into ultra-staging
| author | Giulio Moro <giuliomoro@yahoo.it> | 
|---|---|
| date | Tue, 18 Aug 2015 00:35:15 +0100 | 
| parents | 3c3a1357657d | 
| children | 07cfd337ad18 | 
   comparison
  equal
  deleted
  inserted
  replaced
| 54:d3f869b98147 | 108:3068421c0737 | 
|---|---|
| 4 * Created on: Oct 24, 2014 | 4 * Created on: Oct 24, 2014 | 
| 5 * Author: parallels | 5 * Author: parallels | 
| 6 */ | 6 */ | 
| 7 | 7 | 
| 8 | 8 | 
| 9 #include "../../include/render.h" | 9 #include <BeagleRT.h> | 
| 10 #include <cmath> | 10 #include <cmath> | 
| 11 | 11 | 
| 12 float gFrequency; | 12 float gFrequency = 440.0; | 
| 13 float gPhase; | 13 float gPhase; | 
| 14 float gInverseSampleRate; | 14 float gInverseSampleRate; | 
| 15 | 15 | 
| 16 // initialise_render() is called once before the audio rendering starts. | 16 // setup() is called once before the audio rendering starts. | 
| 17 // Use it to perform any initialisation and allocation which is dependent | 17 // Use it to perform any initialisation and allocation which is dependent | 
| 18 // on the period size or sample rate. | 18 // on the period size or sample rate. | 
| 19 // | 19 // | 
| 20 // userData holds an opaque pointer to a data structure that was passed | 20 // userData holds an opaque pointer to a data structure that was passed | 
| 21 // in from the call to initAudio(). | 21 // in from the call to initAudio(). | 
| 22 // | 22 // | 
| 23 // Return true on success; returning false halts the program. | 23 // Return true on success; returning false halts the program. | 
| 24 | 24 | 
| 25 bool initialise_render(int numMatrixChannels, int numDigitalChannels, int numAudioChannels, | 25 bool setup(BeagleRTContext *context, void *userData) | 
| 26 int numMatrixFramesPerPeriod, | |
| 27 int numAudioFramesPerPeriod, | |
| 28 float matrixSampleRate, float audioSampleRate, | |
| 29 void *userData, RTAudioSettings* settings) | |
| 30 { | 26 { | 
| 31 // Retrieve a parameter passed in from the initAudio() call | 27 // Retrieve a parameter passed in from the initAudio() call | 
| 32 gFrequency = *(float *)userData; | 28 if(userData != 0) | 
| 29 gFrequency = *(float *)userData; | |
| 33 | 30 | 
| 34 gInverseSampleRate = 1.0 / audioSampleRate; | 31 gInverseSampleRate = 1.0 / context->audioSampleRate; | 
| 35 gPhase = 0.0; | 32 gPhase = 0.0; | 
| 36 | 33 | 
| 37 return true; | 34 return true; | 
| 38 } | 35 } | 
| 39 | 36 | 
| 40 // render() is called regularly at the highest priority by the audio engine. | 37 // render() is called regularly at the highest priority by the audio engine. | 
| 41 // Input and output are given from the audio hardware and the other | 38 // Input and output are given from the audio hardware and the other | 
| 42 // ADCs and DACs (if available). If only audio is available, numMatrixFrames | 39 // ADCs and DACs (if available). If only audio is available, numMatrixFrames | 
| 43 // will be 0. | 40 // will be 0. | 
| 44 | 41 | 
| 45 void render(int numAnalogFrames, int numAudioFrames, int numDigitalFrames, float *audioIn, float *audioOut, | 42 void render(BeagleRTContext *context, void *userData) | 
| 46 float *analogIn, float *analogOut, uint32_t *digital) | |
| 47 { | 43 { | 
| 48 for(int n = 0; n < numAudioFrames; n++) { | 44 for(unsigned int n = 0; n < context->audioFrames; n++) { | 
| 49 float out = 0.8f * sinf(gPhase); | 45 float out = 0.8f * sinf(gPhase); | 
| 50 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate; | 46 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate; | 
| 51 if(gPhase > 2.0 * M_PI) | 47 if(gPhase > 2.0 * M_PI) | 
| 52 gPhase -= 2.0 * M_PI; | 48 gPhase -= 2.0 * M_PI; | 
| 53 | 49 | 
| 54 for(int channel = 0; channel < gNumAudioChannels; channel++) | 50 for(unsigned int channel = 0; channel < context->audioChannels; channel++) | 
| 55 audioOut[n * gNumAudioChannels + channel] = out; | 51 context->audioOut[n * context->audioChannels + channel] = out; | 
| 56 } | 52 } | 
| 57 } | 53 } | 
| 58 | 54 | 
| 59 // cleanup_render() is called once at the end, after the audio has stopped. | 55 // cleanup() is called once at the end, after the audio has stopped. | 
| 60 // Release any resources that were allocated in initialise_render(). | 56 // Release any resources that were allocated in setup(). | 
| 61 | 57 | 
| 62 void cleanup_render() | 58 void cleanup(BeagleRTContext *context, void *userData) | 
| 63 { | 59 { | 
| 64 | 60 | 
| 65 } | 61 } | 
