light-controlled sine generator

Based on arduino, coded for Beijing artist Mengqi’s liquid palace programmable synthesiser.

#include <MozziGuts.h>
#include <Oscil.h> // oscillator template
#include <tables/sin2048_int8.h> // sine table for oscillator
#include <mozzi_midi.h>

const byte LDRPin[2] = {A0, A5};
const byte potPin[2] = {A1, A4};
const byte buttPin[4] = {8, A2, A3, 2};
const byte LEDPin[2] = {6, 5};
const byte scale[25] = {0, 2, 4, 7, 9, 12, 14, 12, 16, 19, 21, };
int volume = 0;
int note = 0;

// use: Oscil <table_size, update_rate> oscilName (wavetable), look in .h file of table #included above
Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin(SIN2048_DATA);

void setup() {
for (int i = 0; i < 2; i++) {
pinMode(LEDPin[i], OUTPUT);
}
for (int i = 0; i < 4; i++) {
pinMode(buttPin[i], INPUT_PULLUP);
}
startMozzi(); // uses the default control rate of 64, defined in mozzi_config.h
}

// map(inputValue, input_begin, input_end, output_begin, output_end)
// 0,2,4,7,9
void updateControl() {
int lr = mozziAnalogRead(LDRPin[0]);
int note = map(lr, 0, 1023, 0, 24);
int oct = note / 5;
int noteclass = note % 5;
int final = scale[noteclass] + oct * 12 + 48;
int freq = mtof(final); //midinote to frequency
aSin.setFreq(freq);

volume = map(mozziAnalogRead(potPin[1]), 0, 1023, 14, 0);
}
int updateAudio() {
// this would make more sense with a higher resolution signal
// but still benefits from using HIFI to avoid the 16kHz pwm noise
return (aSin.next() << 6) >> volume; // 8 bits scaled up to 14 bits
}

void loop() {
audioHook(); // required here
}

Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.