Added source code for this week

main
Mike Lynch 2024-10-13 10:10:50 +11:00
parent 298ea24ea7
commit 57e12d95fd
4 changed files with 459 additions and 0 deletions

View File

@ -0,0 +1,102 @@
#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[] = { 2, -1, -1, -1, 1, -1, -1, -1, 2, -1, -1, -1, 0, -1, -1, -1,
// 2, -1, -1, -1, 1, -1, -1, -1, 2, -1, -1, -1, 0, -1, -1, -1,
// 2, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
// 2, -1, -1, -1, 1, -1, -1, -1, 2, -1, -1, -1, 0, -1, -1, -1 };
// int tune2[] = { 8, -1, -1, -1, 8, -1, -1, -1, 8, -1, -1, -1, 8, -1, 8, -1,
// 8, -1, -1, -1, 8, -1, -1, -1, 8, -1, -1, -1, 8, -1, -1, -1,
// 8, -1, -1, -1, 8, -1, -1, -1, 8, -1, 8, -1, 8, -1, 8, -1,
// 8, -1, -1, -1, 8, -1, -1, -1, 8, -1, -1, -1, 8, -1, -1, -1 };
int tune2[] = { 15, -1, -1, -1, 13, -1, -1, -1, 11, -1, -1, -1, -1, -1, 10, -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 = 900;
// 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 - 1;
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);
}

View File

@ -0,0 +1,130 @@
// Better sequencer which uses interrupts
#include <Adafruit_MCP4728.h>
#include <Wire.h>
Adafruit_MCP4728 mcp;
#define nsamp 256
#define dacmax 256
byte waveform[nsamp];
unsigned int phase;
unsigned int length = 256;
int pd = 1;
int note = 0;
// int pitch[] = { 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7 };
// float dur[] = { 1, 0.5, 0.25, 0.1, 1, 0.5, 0.25, 0.1,1, 0.5, 0.25, 0.1,1, 0.5, 0.25, 0.1 };
int pitch[] = { 8, -1, -1, -1, 8,-1, -1, -1, -1,-1, 8, -1, -1, -1, 8, 8, };
float dur[] = { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 };
int phrase = 16;
int s;
int beat_m = 100; // fix me coordinate with timer code
bool noteon = false;
int beat = false;
long notestart, notedur;
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));
make_noise();
mcp.setSpeed(800000L);
note=0;
}
void make_noise() {
for (int i=0; i<nsamp; ++i){
waveform[i]=random(256);
}
}
ISR(TIMER1_COMPA_vect){ // called once every note
beat = true;
length = analogRead(A0) >> 4;
}
void loop() {
long now = millis();
if( beat ) {
beat = false;
if( pitch[s] > -1 ) {
notestart = millis();
notedur = round(beat_m * dur[s]);
noteOn();
noteon = true;
}
s += 1;
if( s == phrase ) {
s = 0;
}
} else {
if( noteon ) {
if( now - notestart > notedur ) {
noteOff();
noteon = false;
}
}
}
phase += 1;
if( phase >= length ) {
phase = 0;
}
mcp.setChannelValue(MCP4728_CHANNEL_A,waveform[phase] << 4);
}
void noteOn() {
if( note > -1 ) {
mcp.setChannelValue(MCP4728_CHANNEL_B, 4095);
}
}
void noteOff() {
mcp.setChannelValue(MCP4728_CHANNEL_B, 0);
}

102
17Noise/17Noise.ino 100644
View File

@ -0,0 +1,102 @@
#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[] = { 2, -1, -1, -1, 1, -1, -1, -1, 2, -1, -1, -1, 0, -1, -1, -1,
// 2, -1, -1, -1, 1, -1, -1, -1, 2, -1, -1, -1, 0, -1, -1, -1,
// 2, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
// 2, -1, -1, -1, 1, -1, -1, -1, 2, -1, -1, -1, 0, -1, -1, -1 };
// int tune2[] = { 8, -1, -1, -1, 8, -1, -1, -1, 8, -1, -1, -1, 8, -1, 8, -1,
// 8, -1, -1, -1, 8, -1, -1, -1, 8, -1, -1, -1, 8, -1, -1, -1,
// 8, -1, -1, -1, 8, -1, -1, -1, 8, -1, 8, -1, 8, -1, 8, -1,
// 8, -1, -1, -1, 8, -1, -1, -1, 8, -1, -1, -1, 8, -1, -1, -1 };
int tune2[] = { 15, -1, -1, -1, 13, -1, -1, -1, 11, -1, -1, -1, -1, -1, 10, -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 = 900;
// 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 - 1;
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);
}

View File

@ -0,0 +1,125 @@
// Better sequencer which uses interrupts
#include <Adafruit_MCP4728.h>
#include <Wire.h>
Adafruit_MCP4728 mcp;
float tuning[37];
float voltrange = 4.85; // measured this, probably not accurate
float octave = 4096.0 / voltrange; // number of DAC steps in an octave
int note = 0;
// int pitch[] = { 8, 4, 5, -1, 4, 3, -1, 2, 3, 4, -1, 6, 3, 4, 5, -1 };
// float dur[] = { 0.5,0.5,0.5,0.5,0.25,0.25,0.25,0.25,0.5,0.5,0.5,0.5,0.25,0.25,0.25,0.25,0.25, };
int pitch[] = { 12, -1, 11, -1, 12, -1, 15, -1, 12, -1, 11, -1, -1, -1, -1, -1 };
float dur[] = { 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0 };
int phrase = 16;
int s;
int beat_m = 1000; // fix me coordinate with timer code
bool noteon = false;
int beat = false;
long notestart, notedur;
void setup() {
Serial.begin(115200);
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 = 15624;
OCR1A = 48000;
// 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);
make_tuning(12);
note=0;
Serial.println("hello");
}
void make_tuning(int edo) {
float n0 = 0;
float edof = (float)edo;
for( int i = 0; i < 37; i++ ) {
tuning[i] = round(n0 + octave * (float)i / edof);
}
}
ISR(TIMER1_COMPA_vect){ // called once every note
beat = true;
}
void loop() {
long now = millis();
if( beat ) {
beat = false;
if( pitch[s] > -1 ) {
notestart = millis();
notedur = round(beat_m * dur[s]);
noteOn(pitch[s]);
noteon = true;
}
s += 1;
if( s == phrase ) {
s = 0;
}
} else {
if( noteon ) {
if( now - notestart > notedur ) {
noteOff();
noteon = false;
}
}
}
}
void noteOn(int note) {
if( note > -1 ) {
mcp.setChannelValue(MCP4728_CHANNEL_A, tuning[note]);
mcp.setChannelValue(MCP4728_CHANNEL_B, 4095);
}
}
void noteOff() {
Serial.println("off");
mcp.setChannelValue(MCP4728_CHANNEL_B, 0);
}