annotate examples/basic_passthru/render.cpp @ 424:9614e2f4b76e prerelease

Makefile now supports a QUIET flag which silences some of the outputs (currently only the one from STOP). QUIET is enabled by default for runide (or any of the QUIET_TARGETS). Added target for stop to runide
author Giulio Moro <giuliomoro@yahoo.it>
date Thu, 16 Jun 2016 14:34:18 +0100
parents 9dc5a0ccad25
children
rev   line source
andrewm@13 1 /*
robert@372 2 ____ _____ _ _
robert@372 3 | __ )| ____| | / \
robert@372 4 | _ \| _| | | / _ \
robert@372 5 | |_) | |___| |___ / ___ \
robert@372 6 |____/|_____|_____/_/ \_\.io
robert@372 7
robert@372 8 */
robert@372 9
robert@372 10 /*
andrewm@13 11 * render.cpp
andrewm@13 12 *
andrewm@13 13 * Created on: Oct 24, 2014
andrewm@13 14 * Author: parallels
andrewm@13 15 */
andrewm@13 16
robert@372 17 /**
robert@372 18 \example 1_basic_audio_analog_passthrough
robert@372 19
robert@372 20 Audio and analog passthrough: input to output
robert@372 21 -----------------------------------------
robert@372 22
robert@372 23 This sketch demonstrates how to read from and write to the audio and analog input and output buffers.
robert@372 24
robert@372 25 In `render()` you'll see a nested for loop structure. You'll see this in all Bela projects.
robert@372 26 The first for loop cycles through `audioFrames`, the second through
robert@372 27 `audioChannels` (in this case left 0 and right 1).
robert@372 28
robert@372 29 You can access any information about current audio and sensor settings you can do the following:
robert@372 30 `context->name_of_item`. For example `context->audioChannels` returns current number of channels,
robert@372 31 `context->audioFrames` returns the current number of audio frames,
robert@372 32 `context->audioSampleRate` returns the audio sample rate.
robert@372 33
robert@372 34 You can look at all the information you can access in ::BeagleRTContext.
robert@372 35
robert@372 36 Reading and writing from the audio buffers
robert@372 37 ------------------------------------------
robert@372 38
robert@372 39 The simplest way to read samples from the audio input buffer is with
robert@372 40 `audioRead()` which we pass three arguments: context, current audio
robert@372 41 frame and current channel. In this example we have
robert@372 42 `audioRead(context, n, ch)` where both `n` and `ch` are provided by
robert@372 43 the nested for loop structure.
robert@372 44
robert@372 45 We can write samples to the audio output buffer in a similar way using
robert@372 46 `audioWrite()`. This has a fourth argument which is the value of the output.
robert@372 47 For example `audioWrite(context, n, ch, value_to_output)`.
robert@372 48
robert@372 49 Reading and writing from the analog buffers
robert@372 50 -------------------------------------------
robert@372 51
robert@372 52 The same is true for `analogRead()` and `analogWrite()`.
robert@372 53
robert@372 54 Note that for the analog channels we write to and read from the buffers in a separate set
robert@372 55 of nested for loops. This is because the they are sampled at half audio rate by default.
robert@372 56 The first of these for loops cycles through `analogFrames`, the second through
robert@372 57 `analogChannels`.
robert@372 58
robert@372 59 By setting `audioWriteFrame(context, n, ch, audioReadFrame(context, n, ch))` and
robert@372 60 `analogWrite(context, n, ch, analogReadFrame(context, n, ch))` we have a simple
robert@372 61 passthrough of audio input to output and analog input to output.
robert@372 62
robert@372 63
robert@372 64 It is also possible to address the buffers directly, for example:
robert@372 65 `context->audioOut[n * context->audioChannels + ch]`.
robert@372 66 */
andrewm@13 67
giuliomoro@301 68 #include <Bela.h>
andrewm@13 69 #include <rtdk.h>
andrewm@13 70
andrewm@56 71 // setup() is called once before the audio rendering starts.
andrewm@13 72 // Use it to perform any initialisation and allocation which is dependent
andrewm@13 73 // on the period size or sample rate.
andrewm@13 74 //
andrewm@13 75 // userData holds an opaque pointer to a data structure that was passed
andrewm@13 76 // in from the call to initAudio().
andrewm@13 77 //
andrewm@13 78 // Return true on success; returning false halts the program.
andrewm@13 79
giuliomoro@301 80 bool setup(BelaContext *context, void *userData)
andrewm@13 81 {
andrewm@13 82 // Nothing to do here...
andrewm@13 83 return true;
andrewm@13 84 }
andrewm@13 85
andrewm@13 86 // render() is called regularly at the highest priority by the audio engine.
andrewm@13 87 // Input and output are given from the audio hardware and the other
andrewm@13 88 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
andrewm@13 89 // will be 0.
andrewm@13 90
giuliomoro@301 91 void render(BelaContext *context, void *userData)
andrewm@13 92 {
andrewm@13 93 // Simplest possible case: pass inputs through to outputs
andrewm@52 94 for(unsigned int n = 0; n < context->audioFrames; n++) {
giuliomoro@180 95 for(unsigned int ch = 0; ch < context->audioChannels; ch++){
giuliomoro@180 96 // Two equivalent ways to write this code
giuliomoro@180 97
giuliomoro@180 98 // The long way, using the buffers directly:
giuliomoro@180 99 // context->audioOut[n * context->audioChannels + ch] =
giuliomoro@180 100 // context->audioIn[n * context->audioChannels + ch];
giuliomoro@180 101
giuliomoro@180 102 // Or using the macros:
andrewm@308 103 audioWrite(context, n, ch, audioRead(context, n, ch));
giuliomoro@180 104 }
andrewm@13 105 }
andrewm@13 106
giuliomoro@180 107 // Same with analog channelss
giuliomoro@180 108 for(unsigned int n = 0; n < context->analogFrames; n++) {
giuliomoro@180 109 for(unsigned int ch = 0; ch < context->analogChannels; ch++) {
giuliomoro@180 110 // Two equivalent ways to write this code
giuliomoro@180 111
giuliomoro@180 112 // The long way, using the buffers directly:
giuliomoro@180 113 // context->analogOut[n * context->analogChannels + ch] = context->analogIn[n * context->analogChannels + ch];
giuliomoro@180 114
giuliomoro@180 115 // Or using the macros:
andrewm@308 116 analogWrite(context, n, ch, analogRead(context, n, ch));
giuliomoro@180 117 }
giuliomoro@180 118 }
andrewm@13 119 }
andrewm@13 120
andrewm@56 121 // cleanup() is called once at the end, after the audio has stopped.
andrewm@56 122 // Release any resources that were allocated in setup().
andrewm@13 123
giuliomoro@301 124 void cleanup(BelaContext *context, void *userData)
andrewm@13 125 {
andrewm@13 126
andrewm@13 127 }