Mercurial > hg > beaglert
comparison projects/basic_analog_output/render.cpp @ 12:a6beeba3a648
Initial support for higher matrix sample rates by reducing the number of channels. Input not tested yet, and not all examples updated to new format.
| author | andrewm |
|---|---|
| date | Thu, 22 Jan 2015 19:00:22 +0000 |
| parents | 09f03ac40fcc |
| children | 6adb088196a7 |
comparison
equal
deleted
inserted
replaced
| 11:517715b23df0 | 12:a6beeba3a648 |
|---|---|
| 17 | 17 |
| 18 float gFrequency; | 18 float gFrequency; |
| 19 float gPhase; | 19 float gPhase; |
| 20 float gInverseSampleRate; | 20 float gInverseSampleRate; |
| 21 | 21 |
| 22 int gMatrixChannels; | |
| 23 | |
| 22 // initialise_render() is called once before the audio rendering starts. | 24 // initialise_render() is called once before the audio rendering starts. |
| 23 // Use it to perform any initialisation and allocation which is dependent | 25 // Use it to perform any initialisation and allocation which is dependent |
| 24 // on the period size or sample rate. | 26 // on the period size or sample rate. |
| 25 // | 27 // |
| 26 // userData holds an opaque pointer to a data structure that was passed | 28 // userData holds an opaque pointer to a data structure that was passed |
| 27 // in from the call to initAudio(). | 29 // in from the call to initAudio(). |
| 28 // | 30 // |
| 29 // Return true on success; returning false halts the program. | 31 // Return true on success; returning false halts the program. |
| 30 | 32 |
| 31 bool initialise_render(int numChannels, int numMatrixFramesPerPeriod, | 33 bool initialise_render(int numMatrixChannels, int numAudioChannels, |
| 32 int numAudioFramesPerPeriod, float matrixSampleRate, | 34 int numMatrixFramesPerPeriod, |
| 33 float audioSampleRate, void *userData) | 35 int numAudioFramesPerPeriod, |
| 36 float matrixSampleRate, float audioSampleRate, | |
| 37 void *userData) | |
| 34 { | 38 { |
| 35 // Retrieve a parameter passed in from the initAudio() call | 39 // Retrieve a parameter passed in from the initAudio() call |
| 36 gFrequency = *(float *)userData; | 40 gFrequency = *(float *)userData; |
| 37 | 41 |
| 38 if(numMatrixFramesPerPeriod*2 != numAudioFramesPerPeriod) { | 42 if(numMatrixFramesPerPeriod == 0) { |
| 39 rt_printf("Error: this example needs the matrix enabled, running at half audio rate\n"); | 43 rt_printf("Error: this example needs the matrix enabled\n"); |
| 40 return false; | 44 return false; |
| 41 } | 45 } |
| 42 | 46 |
| 43 gInverseSampleRate = 1.0 / audioSampleRate; | 47 gMatrixChannels = numMatrixChannels; |
| 48 gInverseSampleRate = 1.0 / matrixSampleRate; | |
| 44 gPhase = 0.0; | 49 gPhase = 0.0; |
| 45 | 50 |
| 46 return true; | 51 return true; |
| 47 } | 52 } |
| 48 | 53 |
| 55 uint16_t *matrixIn, uint16_t *matrixOut) | 60 uint16_t *matrixIn, uint16_t *matrixOut) |
| 56 { | 61 { |
| 57 for(int n = 0; n < numMatrixFrames; n++) { | 62 for(int n = 0; n < numMatrixFrames; n++) { |
| 58 // Set LED to different phase for each matrix channel | 63 // Set LED to different phase for each matrix channel |
| 59 float relativePhase = 0.0; | 64 float relativePhase = 0.0; |
| 60 for(int channel = 0; channel < 8; channel++) { | 65 for(int channel = 0; channel < gMatrixChannels; channel++) { |
| 61 float out = kMinimumAmplitude + kAmplitudeRange * 0.5f * (1.0f + sinf(gPhase + relativePhase)); | 66 float out = kMinimumAmplitude + kAmplitudeRange * 0.5f * (1.0f + sinf(gPhase + relativePhase)); |
| 62 if(out > MATRIX_MAX) | 67 if(out > MATRIX_MAX) |
| 63 out = MATRIX_MAX; | 68 out = MATRIX_MAX; |
| 64 | 69 |
| 65 analogWrite(channel, n, out); | 70 matrixOut[n * gMatrixChannels + channel] = (uint16_t)out; |
| 71 //analogWrite(channel, n, out); | |
| 66 | 72 |
| 67 // Advance by pi/4 (1/8 of a full rotation) for each channel | 73 // Advance by pi/4 (1/8 of a full rotation) for each channel |
| 68 relativePhase += M_PI * 0.25; | 74 relativePhase += M_PI * 0.25; |
| 69 } | 75 } |
| 70 | 76 |
