beeps update

This commit is contained in:
Jack Humbert 2015-08-31 12:39:53 -04:00
parent 91176d854b
commit ed384bd437
7 changed files with 202 additions and 104 deletions

View file

@ -117,7 +117,7 @@ OPT_DEFS += -DBOOTLOADER_SIZE=4096
# comment out to disable the options.
#
BOOTMAGIC_ENABLE = yes # Virtual DIP switch configuration(+1000)
# MOUSEKEY_ENABLE = yes # Mouse keys(+4700)
MOUSEKEY_ENABLE = yes # Mouse keys(+4700)
EXTRAKEY_ENABLE = yes # Audio control and System control(+450)
CONSOLE_ENABLE = yes # Console for debug(+400)
COMMAND_ENABLE = yes # Commands for debug and configuration

View file

@ -5,7 +5,6 @@
#include <avr/io.h>
#define PI 3.14159265
#define CHANNEL OCR1C
void delay_us(int count) {
while(count--) {
@ -16,91 +15,17 @@ void delay_us(int count) {
int voices = 0;
double frequency = 0;
int volume = 0;
int position = 0;
double frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
bool sliding = false;
#define RANGE 1000
volatile int i=0; //elements of the wave
void beeps() {
// DDRB |= (1<<7);
// PORTB &= ~(1<<7);
// // Use full 16-bit resolution.
// ICR1 = 0xFFFF;
// // I could write a wall of text here to explain... but TL;DW
// // Go read the ATmega32u4 datasheet.
// // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
// // Pin PB7 = OCR1C (Timer 1, Channel C)
// // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
// // (i.e. start high, go low when counter matches.)
// // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
// // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
// TCCR1A = _BV(COM1C1) | _BV(WGM11); // = 0b00001010;
// TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
// // Turn off PWM control on PB7, revert to output low.
// // TCCR1A &= ~(_BV(COM1C1));
// // CHANNEL = ((1 << level) - 1);
// // Turn on PWM control of PB7
// TCCR1A |= _BV(COM1C1);
// // CHANNEL = level << OFFSET | 0x0FFF;
// // CHANNEL = 0b1010101010101010;
// float x = 12;
// float y = 24;
// float length = 50;
// float scale = 1;
// // int f1 = 1000000/440;
// // int f2 = 1000000/880;
// // for (uint32_t i = 0; i < length * 1000; i++) {
// // // int frequency = 1/((sin(PI*2*i*scale*pow(2, x/12.0))*.5+1 + sin(PI*2*i*scale*pow(2, y/12.0))*.5+1) / 2);
// // ICR1 = f1; // Set max to the period
// // OCR1C = f1 >> 1; // Set compare to half the period
// // // _delay_us(10);
// // }
// int frequency = 1000000/440;
// ICR1 = frequency; // Set max to the period
// OCR1C = frequency >> 1; // Set compare to half the period
// _delay_us(500000);
// TCCR1A &= ~(_BV(COM1C1));
// CHANNEL = 0;
play_notes();
// play_note(55*pow(2, 0/12.0), 1);
// play_note(55*pow(2, 12/12.0), 1);
// play_note(55*pow(2, 24/12.0), 1);
// play_note(55*pow(2, 0/12.0), 1);
// play_note(55*pow(2, 12/12.0), 1);
// play_note(55*pow(2, 24/12.0), 1);
// play_note(0, 4);
// play_note(55*pow(2, 0/12.0), 8);
// play_note(55*pow(2, 12/12.0), 4);
// play_note(55*pow(2, 10/12.0), 4);
// play_note(55*pow(2, 12/12.0), 8);
// play_note(55*pow(2, 10/12.0), 4);
// play_note(55*pow(2, 7/12.0), 2);
// play_note(55*pow(2, 8/12.0), 2);
// play_note(55*pow(2, 7/12.0), 16);
// play_note(0, 4);
// play_note(55*pow(2, 3/12.0), 8);
// play_note(55*pow(2, 5/12.0), 4);
// play_note(55*pow(2, 7/12.0), 4);
// play_note(55*pow(2, 7/12.0), 8);
// play_note(55*pow(2, 5/12.0), 4);
// play_note(55*pow(2, 3/12.0), 4);
// play_note(55*pow(2, 2/12.0), 16);
play_notes();
}
void send_freq(double freq, int vol) {
@ -114,6 +39,7 @@ void stop_all_notes() {
TCCR3A = 0;
TCCR3B = 0;
frequency = 0;
volume = 0;
for (int i = 0; i < 8; i++) {
frequencies[i] = 0;
@ -135,21 +61,28 @@ void stop_note(double freq) {
}
}
voices--;
if (voices < 0)
voices = 0;
if (voices == 0) {
TCCR3A = 0;
TCCR3B = 0;
frequency = 0;
volume = 0;
} else {
double freq = frequencies[voices - 1];
int vol = volumes[voices - 1];
if (frequency < freq) {
sliding = true;
for (double f = frequency; f <= freq; f += ((freq - frequency) / 500.0)) {
send_freq(f, vol);
}
sliding = false;
} else if (frequency > freq) {
sliding = true;
for (double f = frequency; f >= freq; f -= ((frequency - freq) / 500.0)) {
send_freq(f, vol);
}
sliding = false;
}
send_freq(freq, vol);
frequency = freq;
@ -157,6 +90,115 @@ void stop_note(double freq) {
}
}
void init_notes() {
// TCCR1A = (1 << COM1A1) | (0 << COM1A0) | (1 << WGM11) | (1 << WGM10);
// TCCR1B = (1 << COM1B1) | (0 << COM1A0) | (1 << WGM13) | (1 << WGM12) | (0 << CS12) | (0 << CS11) | (1 << CS10);
// DDRC |= (1<<6);
// TCCR3A = (1 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30);
// TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (0 << CS31) | (1 << CS30);
// ICR3 = 0xFFFF;
// OCR3A = (int)((float)wave[i]*ICR3/RANGE); //go to next array element
// cli();
// /* Enable interrupt on timer2 == 127, with clk/8 prescaler. At 16MHz,
// this gives a timer interrupt at 15625Hz. */
// TIMSK3 = (1 << OCIE3A);
// /* clear/reset timer on match */
// // TCCR3A = 1<<WGM31 | 0<<WGM30; CTC mode, reset on match
// // TCCR3B = 0<<CS32 | 1<<CS31 | 0<<CS30; /* clk, /8 prescaler */
// TCCR3A = (1 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30);
// TCCR3B = (0 << WGM33) | (0 << WGM32) | (0 << CS32) | (0 << CS31) | (1 << CS30);
// TCCR1A = (1 << COM1A1) | (0 << COM1A0) | (1 << WGM11) | (0 << WGM10);
// TCCR1B = (1 << WGM12) | (0 << CS12) | (0 << CS11) | (1 << CS10);
// // SPCR = 0x50;
// // SPSR = 0x01;
// DDRC |= (1<<6);
// // ICR3 = 0xFFFF;
// // OCR3A=80;
// PORTC |= (1<<6);
// sei();
}
// #define highByte(c) ((c >> 8) & 0x00FF)
// #define lowByte(c) (c & 0x00FF)
ISR(TIMER3_COMPA_vect) {
if (ICR3 > 0 && !sliding) {
switch (position) {
case 0: {
int duty = (((double)F_CPU) / (frequency));
ICR3 = duty; // Set max to the period
OCR3A = duty >> 1; // Set compare to half the period
break;
}
case 1: {
int duty = (((double)F_CPU) / (frequency*2));
ICR3 = duty; // Set max to the period
OCR3A = duty >> 1; // Set compare to half the period
break;
}
case 2: {
int duty = (((double)F_CPU) / (frequency*3));
ICR3 = duty; // Set max to the period
OCR3A = duty >> 1; // Set compare to half the period
break;
}
}
position = (position + 1) % 3;
}
// /* OCR2A has been cleared, per TCCR2A above */
// // OCR3A = 127;
// // pos1 += incr1;
// // pos2 += incr2;
// // pos3 += incr3;
// // sample = sinewave[highByte(pos1)] + sinewave[highByte(pos2)] + sinewave[highByte(pos3)];
// // OCR3A = sample;
// OCR3A=pgm_read_byte(&sinewave[pos1]);
// pos1++;
// // PORTC &= ~(1<<6);
// /* buffered, 1x gain, active mode */
// // SPDR = highByte(sample) | 0x70;
// // while (!(SPSR & (1<<SPIF)));
// // SPDR = lowByte(sample);
// // while (!(SPSR & (1<<SPIF)));
// // PORTC |= (1<<6);
}
void loop() {
}
// ISR(TIMER1_COMPA_vect)
// {
// // if (i<(sizeof(wave)/sizeof(int))) //don't exceed ends of vector... sizeof(wave)
// if (i<pow(2, 10)) //don't exceed ends of vector... sizeof(wave)
// {
// OCR3A = (int)((float)wave[i]*ICR3/RANGE); //go to next array element
// // int x = 1;
// // int y = 5;
// // OCR3A = (int) (round(sin(i*440*pow(2, x/12.0))*.5+.5 + sin(i*440*pow(2, y/12.0))*.5+.5) / 2 * ICR3);
// i++; //increment
// }
// else i=0; //reset
// }
void play_note(double freq, int vol) {
if (freq > 0) {

View file

@ -8,4 +8,5 @@ void beeps();
void true_note(float x, float y, float length);
void play_note(double freq, int vol);
void stop_note(double freq);
void stop_all_notes();
void stop_all_notes();
void init_notes();

View file

@ -20,6 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <lufa.h>
uint8_t starting_note = 0x0C;
int offset = 7;
void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
{
@ -31,28 +32,77 @@ void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
}
}
if (record->event.key.col == (MATRIX_COLS - 1) && record->event.key.row == (MATRIX_ROWS - 1) && record->event.pressed) {
starting_note++;
midi_send_cc(&midi_device, 0, 0x7B, 0);
midi_send_cc(&midi_device, 1, 0x7B, 0);
midi_send_cc(&midi_device, 2, 0x7B, 0);
midi_send_cc(&midi_device, 3, 0x7B, 0);
midi_send_cc(&midi_device, 4, 0x7B, 0);
if (record->event.key.col == (MATRIX_COLS - 1) && record->event.key.row == (MATRIX_ROWS - 1)) {
if (record->event.pressed) {
starting_note++;
play_note(((double)261.6)*pow(2.0, -1.0)*pow(2.0,(starting_note + SCALE[0 + offset])/12.0+(MATRIX_ROWS - 1)), 0xC);
midi_send_cc(&midi_device, 0, 0x7B, 0);
midi_send_cc(&midi_device, 1, 0x7B, 0);
midi_send_cc(&midi_device, 2, 0x7B, 0);
midi_send_cc(&midi_device, 3, 0x7B, 0);
midi_send_cc(&midi_device, 4, 0x7B, 0);
return;
} else {
stop_note(((double)261.6)*pow(2.0, -1.0)*pow(2.0,(starting_note + SCALE[0 + offset])/12.0+(MATRIX_ROWS - 1)));
// stop_all_notes();
return;
}
}
if (record->event.key.col == (MATRIX_COLS - 2) && record->event.key.row == (MATRIX_ROWS - 1) && record->event.pressed) {
starting_note--;
if (record->event.key.col == (MATRIX_COLS - 2) && record->event.key.row == (MATRIX_ROWS - 1)) {
if (record->event.pressed) {
starting_note--;
play_note(((double)261.6)*pow(2.0, -1.0)*pow(2.0,(starting_note + SCALE[0 + offset])/12.0+(MATRIX_ROWS - 1)), 0xC);
midi_send_cc(&midi_device, 0, 0x7B, 0);
midi_send_cc(&midi_device, 1, 0x7B, 0);
midi_send_cc(&midi_device, 2, 0x7B, 0);
midi_send_cc(&midi_device, 3, 0x7B, 0);
midi_send_cc(&midi_device, 4, 0x7B, 0);
return;
} else {
stop_note(((double)261.6)*pow(2.0, -1.0)*pow(2.0,(starting_note + SCALE[0 + offset])/12.0+(MATRIX_ROWS - 1)));
// stop_all_notes();
return;
}
}
if (record->event.key.col == (MATRIX_COLS - 3) && record->event.key.row == (MATRIX_ROWS - 1) && record->event.pressed) {
offset++;
midi_send_cc(&midi_device, 0, 0x7B, 0);
midi_send_cc(&midi_device, 1, 0x7B, 0);
midi_send_cc(&midi_device, 2, 0x7B, 0);
midi_send_cc(&midi_device, 3, 0x7B, 0);
midi_send_cc(&midi_device, 4, 0x7B, 0);
// stop_all_notes();
for (int i = 0; i <= 7; i++) {
play_note(((double)261.6)*pow(2.0, -1.0)*pow(2.0,(starting_note + SCALE[i + offset])/12.0+(MATRIX_ROWS - 1)), 0xC);
_delay_us(80000);
stop_note(((double)261.6)*pow(2.0, -1.0)*pow(2.0,(starting_note + SCALE[i + offset])/12.0+(MATRIX_ROWS - 1)));
_delay_us(8000);
}
return;
}
if (record->event.key.col == (MATRIX_COLS - 4) && record->event.key.row == (MATRIX_ROWS - 1) && record->event.pressed) {
offset--;
midi_send_cc(&midi_device, 0, 0x7B, 0);
midi_send_cc(&midi_device, 1, 0x7B, 0);
midi_send_cc(&midi_device, 2, 0x7B, 0);
midi_send_cc(&midi_device, 3, 0x7B, 0);
midi_send_cc(&midi_device, 4, 0x7B, 0);
// stop_all_notes();
for (int i = 0; i <= 7; i++) {
play_note(((double)261.6)*pow(2.0, -1.0)*pow(2.0,(starting_note + SCALE[i + offset])/12.0+(MATRIX_ROWS - 1)), 0xC);
_delay_us(80000);
stop_note(((double)261.6)*pow(2.0, -1.0)*pow(2.0,(starting_note + SCALE[i + offset])/12.0+(MATRIX_ROWS - 1)));
_delay_us(8000);
}
return;
}
if (record->event.pressed) {
midi_send_noteon(&midi_device, record->event.key.row, starting_note + SCALE[record->event.key.col], 127);
play_note(((double)261.6)*pow(2.0, 2.0)*pow(2.0,SCALE[record->event.key.col]/12.0+(record->event.key.row)), 0xF);
// midi_send_noteon(&midi_device, record->event.key.row, starting_note + SCALE[record->event.key.col], 127);
play_note(((double)261.6)*pow(2.0, -1.0)*pow(2.0,(starting_note + SCALE[record->event.key.col + offset])/12.0+(MATRIX_ROWS - record->event.key.row)), 0xF);
} else {
midi_send_noteoff(&midi_device, record->event.key.row, starting_note + SCALE[record->event.key.col], 127);
stop_note(((double)261.6)*pow(2.0, 2.0)*pow(2.0,SCALE[record->event.key.col]/12.0+(record->event.key.row)));
// midi_send_noteoff(&midi_device, record->event.key.row, starting_note + SCALE[record->event.key.col], 127);
stop_note(((double)261.6)*pow(2.0, -1.0)*pow(2.0,(starting_note + SCALE[record->event.key.col + offset])/12.0+(MATRIX_ROWS - record->event.key.row)));
}
}

View file

@ -23,7 +23,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define CHNL(note, channel) (note + (channel << 8))
#define SCALE (int []){ 0, 2, 4, 5, 7, 9, 11, 12, 14, 16, 17, 19, 21, 23, 24, 26, 28, 29, 31, 33, 35, 36}
#define SCALE (int []){ 0 + (12*0), 2 + (12*0), 4 + (12*0), 5 + (12*0), 7 + (12*0), 9 + (12*0), 11 + (12*0), \
0 + (12*1), 2 + (12*1), 4 + (12*1), 5 + (12*1), 7 + (12*1), 9 + (12*1), 11 + (12*1), \
0 + (12*2), 2 + (12*2), 4 + (12*2), 5 + (12*2), 7 + (12*2), 9 + (12*2), 11 + (12*2), \
0 + (12*3), 2 + (12*3), 4 + (12*3), 5 + (12*3), 7 + (12*3), 9 + (12*3), 11 + (12*3), \
0 + (12*4), 2 + (12*4), 4 + (12*4), 5 + (12*4), 7 + (12*4), 9 + (12*4), 11 + (12*4), }
#define N_CN1 (0x600C + (12 * -1) + 0 )
#define N_CN1S (0x600C + (12 * -1) + 1 )

View file

@ -40,7 +40,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
{ MIDI12 },
{ MIDI12 },
{ MIDI12 },
{M(0), KC_LCTL, KC_LALT, KC_LGUI, FUNC(2), KC_SPC, KC_SPC, FUNC(1), MIDI, MIDI, MIDI, MIDI}
{M(0), KC_MS_L, KC_MS_D, KC_MS_U, KC_MS_R, KC_SPC, KC_SPC, FUNC(1), MIDI, MIDI, MIDI, MIDI}
}
};

View file

@ -829,6 +829,7 @@ int main(void)
midi_register_cc_callback(&midi_device, cc_callback);
midi_register_sysex_callback(&midi_device, sysex_callback);
init_notes();
// midi_send_cc(&midi_device, 0, 1, 2);
// midi_send_cc(&midi_device, 15, 1, 0);
// midi_send_noteon(&midi_device, 0, 64, 127);
@ -837,13 +838,13 @@ int main(void)
/* wait for USB startup & debug output */
while (USB_DeviceState != DEVICE_STATE_Configured) {
#if defined(INTERRUPT_CONTROL_ENDPOINT)
;
#else
// while (USB_DeviceState != DEVICE_STATE_Configured) {
// #if defined(INTERRUPT_CONTROL_ENDPOINT)
// ;
// #else
USB_USBTask();
#endif
}
// #endif
// }
print("USB configured.\n");
/* init modules */