diff examples/03-Analog/analog-input/render.cpp @ 542:3016638b4da2 prerelease

Analog examples updated
author Robert Jack <robert.h.jack@gmail.com>
date Fri, 24 Jun 2016 13:00:31 +0100
parents bfcbeb437869
children
line wrap: on
line diff
--- a/examples/03-Analog/analog-input/render.cpp	Fri Jun 24 02:29:05 2016 +0100
+++ b/examples/03-Analog/analog-input/render.cpp	Fri Jun 24 13:00:31 2016 +0100
@@ -23,27 +23,33 @@
 
 
 #include <Bela.h>
-#include <rtdk.h>
 #include <cmath>
 
 float gPhase;
 float gInverseSampleRate;
 int gAudioFramesPerAnalogFrame;
 
-// These settings are carried over from main.cpp
-// Setting global variables is an alternative approach
-// to passing a structure to userData in setup()
-
-extern int gSensorInputFrequency;
-extern int gSensorInputAmplitude;
+// Set the analog channels to read from
+int gSensorInputFrequency = 0;
+int gSensorInputAmplitude = 1;
 
 bool setup(BelaContext *context, void *userData)
 {
+	
+	// Check if analog channels are enabled
 	if(context->analogFrames == 0 || context->analogFrames > context->audioFrames) {
 		rt_printf("Error: this example needs analog enabled, with 4 or 8 channels\n");
 		return false;
 	}
 
+	// Check that we have the same number of inputs and outputs.
+	if(context->audioInChannels != context->audioOutChannels ||
+			context->analogInChannels != context-> analogOutChannels){
+		printf("Error: for this project, you need the same number of input and output channels.\n");
+		return false;
+	}
+
+	// Useful calculations
 	gAudioFramesPerAnalogFrame = context->audioFrames / context->analogFrames;
 	gInverseSampleRate = 1.0 / context->audioSampleRate;
 	gPhase = 0.0;
@@ -61,16 +67,18 @@
 
 	for(unsigned int n = 0; n < context->audioFrames; n++) {
 		if(!(n % gAudioFramesPerAnalogFrame)) {
-			// Even audio samples: update frequency and amplitude from the matrix
+			// On even audio samples: read analog inputs and update frequency and amplitude
 			frequency = map(analogRead(context, n/gAudioFramesPerAnalogFrame, gSensorInputFrequency), 0, 1, 100, 1000);
 			amplitude = analogRead(context, n/gAudioFramesPerAnalogFrame, gSensorInputAmplitude);
 		}
 
 		float out = amplitude * sinf(gPhase);
 
-		for(unsigned int channel = 0; channel < context->audioOutChannels; channel++)
-			context->audioOut[n * context->audioChannels + channel] = out;
+		for(unsigned int channel = 0; channel < context->audioOutChannels; channel++) {
+			audioWrite(context, n, channel, out);
+		}
 
+		// Update and wrap phase of sine tone
 		gPhase += 2.0 * M_PI * frequency * gInverseSampleRate;
 		if(gPhase > 2.0 * M_PI)
 			gPhase -= 2.0 * M_PI;
@@ -90,10 +98,10 @@
 -------------------------
 
 This sketch produces a sine tone, the frequency and amplitude of which are 
-affected by data received on the analog pins. Before looping through each audio 
-frame, we declare a value for the frequency and amplitude of our sine tone 
-(line 55); we adjust these values by taking in data from analog sensors 
-(for example potentiometers) with `analogRead()`.
+modulated by data received on the analog input pins. Before looping through each audio 
+frame, we declare a value for the `frequency` and `amplitude` of our sine tone; 
+we adjust these values by taking in data from analog sensors (for example potentiometers)
+with `analogRead()`.
 
 - connect a 10K pot to 3.3V and GND on its 1st and 3rd pins.
 - connect the 2nd middle pin of the pot to analogIn 0.
@@ -113,8 +121,4 @@
 }
 ````
 
-Note that the pin numbers are stored in the variables `gAnalogInputFrequency` and 
-`gAnalogInputAmplitude`. These are declared in the main.cpp file; if you look in 
-that file you will see that they have the values of 0 and 1. Bear in mind that 
-these are analog input pins which is a specific header!
 */