Added 9, 10 and 11

main
Mike Lynch 2024-10-12 11:35:16 +11:00
parent aa11f1cc25
commit 9542bbee1a
4 changed files with 253 additions and 7 deletions

104
097TET/097TET.ino 100644
View File

@ -0,0 +1,104 @@
// Basic demo for configuring the MCP4728 4-Channel 12-bit I2C DAC
#include <Adafruit_MCP4728.h>
#include <Wire.h>
Adafruit_MCP4728 mcp;
// 1 3 5 7 hexany log2
float tuning[22];
// bassline
//int pitch[] = { 1, 2, -1, 0, -1, 1, -1, 0, -1, 1, -1 };
//float dur[] = { 0.25, 0.25, 0.5, 0.25, 0.5, 0.25, 0.5, 0.25, 0.5, 0.25, 0.5 };
// kick
//int pitch[] = { 1, -1, 1, -1, 1, -1, 1, -1, 1 };
//float dur[] = { 0.25, 0.75, 0.25, 0.75, 0.25, 0.75, 0.25, 0.75};
int pitch[] = { 9, 7, 5, 3, 8, 6, 5, 3 };
float dur[] = { 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5 };
float gate = 0.25;
int bpm = 140;
int phrase = 8;
float beat_s = 60.0 / (float)bpm;
int beat_m = round(1000.0 * beat_s);
float voltrange = 4.85; // measured this, probably not accurate
float octave = 4096.0 / voltrange; // number of DAC steps in an octave
float mod_b = 3.141592653589793 / 4.0;
int s;
bool noteon = false;
long play_t;
long release_t;
long last_t;
long init_t;
void setup(void) {
Serial.begin(115200);
while (!Serial)
delay(10); // will pause Zero, Leonardo, etc until serial console opens
if (!mcp.begin(0x64)) {
Serial.println("Failed to find MCP4728 chip");
while (1) {
delay(10);
}
Serial.println("MCP4728 initialised");
}
mcp.setChannelValue(MCP4728_CHANNEL_B, 0);
float n0 = 0;
for( int i = 0; i < 22; i++ ) {
tuning[i] = round(n0 + octave * (float)i / 7.0);
}
s = 0;
release_t = 0;
play_t = 0;
last_t = millis();
init_t = last_t;
}
void loop() {
long now = millis();
if( noteon ) {
if( now - last_t > release_t ) {
noteOff();
last_t = now;
play_t = round(beat_m * dur[s] * (1.0 - gate));
noteon = false;
s += 1;
if( s == phrase ) {
s = 0;
}
}
} else {
if( now - last_t > play_t ) {
noteOn(pitch[s]);
last_t = now;
release_t = round(beat_m * dur[s] * gate);
noteon = true;
}
}
// sychronise this better
long mod_t = (now - init_t);
float mod = 0.5 + 0.5 * sin(mod_b * (float)mod_t / (float)beat_m);
mcp.setChannelValue(MCP4728_CHANNEL_C, round(4095.0 * mod));
}
void noteOn(int note) {
if( note > -1 ) {
mcp.setChannelValue(MCP4728_CHANNEL_A, tuning[note]);
mcp.setChannelValue(MCP4728_CHANNEL_B, 4095);
}
}
void noteOff() {
mcp.setChannelValue(MCP4728_CHANNEL_B, 0);
}

View File

@ -0,0 +1,49 @@
// Basic demo for configuring the MCP4728 4-Channel 12-bit I2C DAC
#include <Adafruit_MCP4728.h>
#include <Wire.h>
Adafruit_MCP4728 mcp;
int bpm = 98;
float beat_s = 60.0 / (float)bpm;
int beat_m = round(1000.0 * beat_s);
long init_t;
float pi = 3.141592653589793;
float mod_a = pi;
float mod_b = pi / 4.0;
float mod_c = pi / 3.0;
float mod_d = pi / 5.0;
void setup(void) {
Serial.begin(115200);
while (!Serial)
delay(10); // will pause Zero, Leonardo, etc until serial console opens
if (!mcp.begin(0x64)) {
Serial.println("Failed to find MCP4728 chip");
while (1) {
delay(10);
}
Serial.println("MCP4728 initialised");
}
init_t = millis();
mcp.setChannelValue(MCP4728_CHANNEL_B, 0);
}
void loop() {
long now = millis();
long mod_t = (now - init_t);
float tf = (float)mod_t / (float)beat_m;
uint16_t a = round(4095.0 * (0.5 + 0.5 * sin(mod_a * tf)));
uint16_t b = round(4095.0 * (0.5 + 0.5 * sin(mod_b * tf)));
uint16_t c = round(4095.0 * (0.5 + 0.5 * sin(mod_c * tf)));
uint16_t d = round(4095.0 * (0.5 + 0.5 * sin(mod_d * tf)));
mcp.fastWrite(a, b, c, d);
}

View File

@ -0,0 +1,93 @@
// Very basic Karplus-Strong
// based on this explanation http://sites.music.columbia.edu/cmc/MusicAndComputers/chapter4/04_09.php
#include <Adafruit_MCP4728.h>
#include <Wire.h>
Adafruit_MCP4728 mcp;
#define nsamp 256
#define dacmax 256
unsigned int phase;
unsigned int length;
unsigned int repeats;
int note = 0;
int freqs[] = { 256, 112, 102, 96, 84, 76, 64, 56, 50, 48, 42, 38, 34, 32 };
int tune2[] = { 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1 };
byte waveform[nsamp];
int pd = 30;
void setup() {
cli();
//set timer1 interrupt at 1Hz
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;//initialize counter value to 0
// set compare match register for 1hz increments
//OCR1A = 15624;// = (16*10^6) / (1*1024) - 1 (must be <65536)
//OCR1A = 7812;// = (16*10^6) / (1*1024) - 1 (must be <65536)
OCR1A = 800;
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS10 and CS12 bits for 1024 escaler
TCCR1B |= (1 << CS12) | (1 << CS10);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei();
if (!mcp.begin(0x64)) {
while (1) {
delay(100);
}
}
randomSeed(analogRead(A0));
mcp.setSpeed(800000L);
phase=0;
note=0;
}
ISR(TIMER1_COMPA_vect){//timer1
if( tune2[note] > -1 ) {
length=freqs[tune2[note]];
for (int i=0; i<nsamp; ++i){
waveform[i]=random(256);
}
}
note++;
if( note > 15 ) {
note = 0;
}
pd = analogRead(A0) >> 4;
}
void loop() {
byte s1, s2;
int p0 = phase;
int p1 = phase - pd;
if( p1 < 0 ) {
p1 += length;
}
phase += 1;
if( phase >= length ) {
phase = 0;
}
s1 = waveform[p1];
s2 = waveform[p0];
waveform[p0] = ( s1 & s2 ) + ((s1 ^ s2) >> 1);
mcp.fastWrite(s1 << 4, 0, 0, 0);
}

14
TODO.md
View File

@ -2,10 +2,10 @@
Sunday 6 October Sunday 6 October
6 - Wavetable updates s 6 - Wavetable updates
7 - Change a sequence while it's playing m 7 - Change a sequence while it's playing
8 - Sound clouds t 8 - Sound clouds
9 - 7TET groove w 9 - 7TET groove
10 - Drum pattern t 10 - Drum pattern
11 - FM f 11 - FM
12 - Karplus-Strong s 12 - Karplus-Strong