robert@464: /* robert@464: ____ _____ _ _ robert@464: | __ )| ____| | / \ robert@464: | _ \| _| | | / _ \ robert@464: | |_) | |___| |___ / ___ \ robert@464: |____/|_____|_____/_/ \_\ robert@464: robert@464: The platform for ultra-low latency audio and sensor processing robert@464: robert@464: http://bela.io robert@464: robert@464: A project of the Augmented Instruments Laboratory within the robert@464: Centre for Digital Music at Queen Mary University of London. robert@464: http://www.eecs.qmul.ac.uk/~andrewm robert@464: robert@464: (c) 2016 Augmented Instruments Laboratory: Andrew McPherson, robert@464: Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack, robert@464: Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved. robert@464: robert@464: The Bela software is distributed under the GNU Lesser General Public License robert@464: (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt robert@464: */ robert@464: robert@464: robert@464: #include robert@464: #include robert@464: robert@464: float gPhase; robert@464: float gInverseSampleRate; robert@464: int gAudioFramesPerAnalogFrame; robert@464: robert@542: // Set the analog channels to read from robert@542: int gSensorInputFrequency = 0; robert@542: int gSensorInputAmplitude = 1; robert@464: robert@464: bool setup(BelaContext *context, void *userData) robert@464: { robert@542: robert@542: // Check if analog channels are enabled robert@464: if(context->analogFrames == 0 || context->analogFrames > context->audioFrames) { robert@464: rt_printf("Error: this example needs analog enabled, with 4 or 8 channels\n"); robert@464: return false; robert@464: } robert@464: robert@542: // Check that we have the same number of inputs and outputs. robert@542: if(context->audioInChannels != context->audioOutChannels || robert@542: context->analogInChannels != context-> analogOutChannels){ robert@542: printf("Error: for this project, you need the same number of input and output channels.\n"); robert@542: return false; robert@542: } robert@542: robert@542: // Useful calculations robert@464: gAudioFramesPerAnalogFrame = context->audioFrames / context->analogFrames; robert@464: gInverseSampleRate = 1.0 / context->audioSampleRate; robert@464: gPhase = 0.0; robert@464: robert@464: return true; robert@464: } robert@464: robert@464: void render(BelaContext *context, void *userData) robert@464: { robert@464: float frequency = 440.0; robert@464: float amplitude = 0.8; robert@464: robert@464: // There are twice as many audio frames as matrix frames since audio sample rate robert@464: // is twice as high robert@464: robert@464: for(unsigned int n = 0; n < context->audioFrames; n++) { robert@464: if(!(n % gAudioFramesPerAnalogFrame)) { robert@542: // On even audio samples: read analog inputs and update frequency and amplitude robert@464: frequency = map(analogRead(context, n/gAudioFramesPerAnalogFrame, gSensorInputFrequency), 0, 1, 100, 1000); robert@464: amplitude = analogRead(context, n/gAudioFramesPerAnalogFrame, gSensorInputAmplitude); robert@464: } robert@464: robert@464: float out = amplitude * sinf(gPhase); robert@464: robert@542: for(unsigned int channel = 0; channel < context->audioOutChannels; channel++) { robert@542: audioWrite(context, n, channel, out); robert@542: } robert@464: robert@542: // Update and wrap phase of sine tone robert@464: gPhase += 2.0 * M_PI * frequency * gInverseSampleRate; robert@464: if(gPhase > 2.0 * M_PI) robert@464: gPhase -= 2.0 * M_PI; robert@464: } robert@464: } robert@464: robert@464: void cleanup(BelaContext *context, void *userData) robert@464: { robert@464: robert@464: } robert@464: robert@464: robert@464: /** robert@500: \example analog-input/render.cpp robert@464: robert@464: Connecting potentiometers robert@464: ------------------------- robert@464: robert@464: This sketch produces a sine tone, the frequency and amplitude of which are robert@542: modulated by data received on the analog input pins. Before looping through each audio robert@542: frame, we declare a value for the `frequency` and `amplitude` of our sine tone; robert@542: we adjust these values by taking in data from analog sensors (for example potentiometers) robert@542: with `analogRead()`. robert@464: robert@464: - connect a 10K pot to 3.3V and GND on its 1st and 3rd pins. robert@464: - connect the 2nd middle pin of the pot to analogIn 0. robert@464: - connect another 10K pot in the same way but with the middle pin connected to analogIn 1. robert@464: robert@464: The important thing to notice is that audio is sampled twice as often as analog robert@464: data. The audio sampling rate is 44.1kHz (44100 frames per second) and the robert@524: analog sampling rate is 22.05kHz (22050 frames per second). Notice that we are robert@524: processing the analog data and updating frequency and amplitude only on every robert@524: second audio sample, since the analog sampling rate is half that of the audio. robert@524: robert@524: ```` robert@524: if(!(n % gAudioFramesPerAnalogFrame)) { robert@524: // Even audio samples: update frequency and amplitude from the matrix robert@524: frequency = map(analogRead(context, n/gAudioFramesPerAnalogFrame, gSensorInputFrequency), 0, 1, 100, 1000); robert@524: amplitude = analogRead(context, n/gAudioFramesPerAnalogFrame, gSensorInputAmplitude); robert@524: } robert@524: ```` robert@464: robert@464: */