This music synthesizer accepts user input via a keyboard using a PS/2 interface. The user has the ability to control two parameters: waveforms and octaves. Waveforms are generated by means of the direct digital synthesis algorithm. The user may select from six waveforms: sine, square, sawtooth, triangle, EKG, and sinc. The synthesizer covers the seven octaves present on an 88-key piano in addition to the two incomplete octaves for a total of nine octaves.

Dependencies:   mbed PS2

Committer:
lbaddam3
Date:
Mon Feb 28 16:55:00 2011 +0000
Revision:
0:175ad26d17ce

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lbaddam3 0:175ad26d17ce 1 /***************************************************************************/
lbaddam3 0:175ad26d17ce 2 /* mbed_synth.c */
lbaddam3 0:175ad26d17ce 3 /* */
lbaddam3 0:175ad26d17ce 4 /* ----------------------------------------------------------------------- */
lbaddam3 0:175ad26d17ce 5 /* This file defines the lookup tables for sine, square, sawtooth, reverse */
lbaddam3 0:175ad26d17ce 6 /* reverse sawtooth, triangle, ekg, and sinc waveforms. This file also */
lbaddam3 0:175ad26d17ce 7 /* defines the functions needed to implement an interrupt-based DDS */
lbaddam3 0:175ad26d17ce 8 /* algorithm. */
lbaddam3 0:175ad26d17ce 9 /* */
lbaddam3 0:175ad26d17ce 10 /* ----------------------------------------------------------------------- */
lbaddam3 0:175ad26d17ce 11 /* The lookup tables are to be read based on the desired frequency by the */
lbaddam3 0:175ad26d17ce 12 /* user of the synthesizer (waveFreq). The waveFreq variable will be */
lbaddam3 0:175ad26d17ce 13 /* used to create what's known as a frequency tuning word (freqTun). This */
lbaddam3 0:175ad26d17ce 14 /* variable inn turn is used to increment the 32-bit phase accumulator */
lbaddam3 0:175ad26d17ce 15 /* (phaseAcc) every 40kHz generated by an interrupt. */
lbaddam3 0:175ad26d17ce 16 /* The calculation of freqTun is as follows: */
lbaddam3 0:175ad26d17ce 17 /* */
lbaddam3 0:175ad26d17ce 18 /* freqTun = ((waveFreq * SAMP_NUM) * (SHFT_FCTR / SAMP_FREQ)) */
lbaddam3 0:175ad26d17ce 19 /* */
lbaddam3 0:175ad26d17ce 20 /* In this instance, SAMP_NUM is 256, SAMP_FREQ is 40000 and the shift */
lbaddam3 0:175ad26d17ce 21 /* factor (SHFT_FCTR) is determined by setting the least significant bit */
lbaddam3 0:175ad26d17ce 22 /* of the lookup address to 1. As the top 8 bits of the phase accumulator */
lbaddam3 0:175ad26d17ce 23 /* serve as the lookup address. This means SHFT_FCTR must be equal to */
lbaddam3 0:175ad26d17ce 24 /* 0x01000000. */
lbaddam3 0:175ad26d17ce 25 /* */
lbaddam3 0:175ad26d17ce 26 /* ----------------------------------------------------------------------- */
lbaddam3 0:175ad26d17ce 27 /* Credit goes to John Luciani for his interrupt-based approach to the DDS */
lbaddam3 0:175ad26d17ce 28 /* algorithm. It helped massively with this project! */
lbaddam3 0:175ad26d17ce 29 /* */
lbaddam3 0:175ad26d17ce 30 /* Find more about John's project here: */
lbaddam3 0:175ad26d17ce 31 /* <http://wiblocks.luciani.org/docs/app-notes/nb1a-nco.html> */
lbaddam3 0:175ad26d17ce 32 /* */
lbaddam3 0:175ad26d17ce 33 /* Credit goes to Jesper Hansen for his wavetables (sine, square, */
lbaddam3 0:175ad26d17ce 34 /* sawtooth, reverse sawtooth, and triangle). */
lbaddam3 0:175ad26d17ce 35 /* */
lbaddam3 0:175ad26d17ce 36 /* Find out more about Jesper's project here: */
lbaddam3 0:175ad26d17ce 37 /* <http://www.myplace.nu/avr/minidds/index.htm> */
lbaddam3 0:175ad26d17ce 38 /* */
lbaddam3 0:175ad26d17ce 39 /* Credit also goes to SPman for his modifications (ekg wavetable). */
lbaddam3 0:175ad26d17ce 40 /* */
lbaddam3 0:175ad26d17ce 41 /* Find out more about SPman's project here: */
lbaddam3 0:175ad26d17ce 42 /* <http://www.scienceprog.com/avr-dds-signal-generator-v20/> */
lbaddam3 0:175ad26d17ce 43 /* */
lbaddam3 0:175ad26d17ce 44 /* ----------------------------------------------------------------------- */
lbaddam3 0:175ad26d17ce 45 /* Author: Ron Grier, Lakshmi Baddam */
lbaddam3 0:175ad26d17ce 46 /* Target: nxp lpc1768 */
lbaddam3 0:175ad26d17ce 47 /* */
lbaddam3 0:175ad26d17ce 48 /***************************************************************************/
lbaddam3 0:175ad26d17ce 49 #include "mbed_synth.h"
lbaddam3 0:175ad26d17ce 50
lbaddam3 0:175ad26d17ce 51 /* Globals */
lbaddam3 0:175ad26d17ce 52 volatile uint16_t waveFreq; // Frequency of waveform
lbaddam3 0:175ad26d17ce 53 volatile uint32_t phaseAcc; // Used to store phase accumulators
lbaddam3 0:175ad26d17ce 54 volatile uint32_t freqTun; // Frequency tuning words
lbaddam3 0:175ad26d17ce 55 volatile uint8_t waveIndex; // Top 8 bits of phase accumulator
lbaddam3 0:175ad26d17ce 56 const uint8_t *waveTable; // Ptr to wavetables
lbaddam3 0:175ad26d17ce 57 Wave myWave = SAWTOOTH;
lbaddam3 0:175ad26d17ce 58 volatile Bool rnd; // Rounding to next sample can reduce SNR
lbaddam3 0:175ad26d17ce 59 volatile Bool sendVals; // When to send next value(s) to DAC(s) set by ISR
lbaddam3 0:175ad26d17ce 60 volatile Bool waveChange; // Whether waveform has changed
lbaddam3 0:175ad26d17ce 61 volatile Bool noise;
lbaddam3 0:175ad26d17ce 62 volatile Wave newWave; // New waveform
lbaddam3 0:175ad26d17ce 63 volatile Bool freqChange; // Whether frequency has changed
lbaddam3 0:175ad26d17ce 64 volatile uint16_t newFreq; // New frequency
lbaddam3 0:175ad26d17ce 65 volatile Bool noteOn = FALSE;
lbaddam3 0:175ad26d17ce 66 Ticker synth_tick;
lbaddam3 0:175ad26d17ce 67 AnalogOut synthPin(p18);
lbaddam3 0:175ad26d17ce 68 PS2Keyboard ps2kb(p12, p11); // CLK, DAT
lbaddam3 0:175ad26d17ce 69 int octave = 4; // Default to 4th octave
lbaddam3 0:175ad26d17ce 70
lbaddam3 0:175ad26d17ce 71 /* Values for Sine Wave */
lbaddam3 0:175ad26d17ce 72 const uint8_t sinVals[SAMP_NUM] = {
lbaddam3 0:175ad26d17ce 73 0x80,0x83,0x86,0x89,0x8c,0x8f,0x92,0x95,
lbaddam3 0:175ad26d17ce 74 0x98,0x9c,0x9f,0xa2,0xa5,0xa8,0xab,0xae,
lbaddam3 0:175ad26d17ce 75 0xb0,0xb3,0xb6,0xb9,0xbc,0xbf,0xc1,0xc4,
lbaddam3 0:175ad26d17ce 76 0xc7,0xc9,0xcc,0xce,0xd1,0xd3,0xd5,0xd8,
lbaddam3 0:175ad26d17ce 77 0xda,0xdc,0xde,0xe0,0xe2,0xe4,0xe6,0xe8,
lbaddam3 0:175ad26d17ce 78 0xea,0xec,0xed,0xef,0xf0,0xf2,0xf3,0xf5,
lbaddam3 0:175ad26d17ce 79 0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfc,
lbaddam3 0:175ad26d17ce 80 0xfd,0xfe,0xfe,0xff,0xff,0xff,0xff,0xff,
lbaddam3 0:175ad26d17ce 81 0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfe,
lbaddam3 0:175ad26d17ce 82 0xfd,0xfc,0xfc,0xfb,0xfa,0xf9,0xf8,0xf7,
lbaddam3 0:175ad26d17ce 83 0xf6,0xf5,0xf3,0xf2,0xf0,0xef,0xed,0xec,
lbaddam3 0:175ad26d17ce 84 0xea,0xe8,0xe6,0xe4,0xe2,0xe0,0xde,0xdc,
lbaddam3 0:175ad26d17ce 85 0xda,0xd8,0xd5,0xd3,0xd1,0xce,0xcc,0xc9,
lbaddam3 0:175ad26d17ce 86 0xc7,0xc4,0xc1,0xbf,0xbc,0xb9,0xb6,0xb3,
lbaddam3 0:175ad26d17ce 87 0xb0,0xae,0xab,0xa8,0xa5,0xa2,0x9f,0x9c,
lbaddam3 0:175ad26d17ce 88 0x98,0x95,0x92,0x8f,0x8c,0x89,0x86,0x83,
lbaddam3 0:175ad26d17ce 89 0x80,0x7c,0x79,0x76,0x73,0x70,0x6d,0x6a,
lbaddam3 0:175ad26d17ce 90 0x67,0x63,0x60,0x5d,0x5a,0x57,0x54,0x51,
lbaddam3 0:175ad26d17ce 91 0x4f,0x4c,0x49,0x46,0x43,0x40,0x3e,0x3b,
lbaddam3 0:175ad26d17ce 92 0x38,0x36,0x33,0x31,0x2e,0x2c,0x2a,0x27,
lbaddam3 0:175ad26d17ce 93 0x25,0x23,0x21,0x1f,0x1d,0x1b,0x19,0x17,
lbaddam3 0:175ad26d17ce 94 0x15,0x13,0x12,0x10,0x0f,0x0d,0x0c,0x0a,
lbaddam3 0:175ad26d17ce 95 0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x03,
lbaddam3 0:175ad26d17ce 96 0x02,0x01,0x01,0x00,0x00,0x00,0x00,0x00,
lbaddam3 0:175ad26d17ce 97 0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,
lbaddam3 0:175ad26d17ce 98 0x02,0x03,0x03,0x04,0x05,0x06,0x07,0x08,
lbaddam3 0:175ad26d17ce 99 0x09,0x0a,0x0c,0x0d,0x0f,0x10,0x12,0x13,
lbaddam3 0:175ad26d17ce 100 0x15,0x17,0x19,0x1b,0x1d,0x1f,0x21,0x23,
lbaddam3 0:175ad26d17ce 101 0x25,0x27,0x2a,0x2c,0x2e,0x31,0x33,0x36,
lbaddam3 0:175ad26d17ce 102 0x38,0x3b,0x3e,0x40,0x43,0x46,0x49,0x4c,
lbaddam3 0:175ad26d17ce 103 0x4f,0x51,0x54,0x57,0x5a,0x5d,0x60,0x63,
lbaddam3 0:175ad26d17ce 104 0x67,0x6a,0x6d,0x70,0x73,0x76,0x79,0x7c
lbaddam3 0:175ad26d17ce 105 };
lbaddam3 0:175ad26d17ce 106
lbaddam3 0:175ad26d17ce 107 /* Values for Square Wave */
lbaddam3 0:175ad26d17ce 108 const uint8_t sqVals[SAMP_NUM] = {
lbaddam3 0:175ad26d17ce 109 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
lbaddam3 0:175ad26d17ce 110 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
lbaddam3 0:175ad26d17ce 111 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
lbaddam3 0:175ad26d17ce 112 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
lbaddam3 0:175ad26d17ce 113 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
lbaddam3 0:175ad26d17ce 114 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
lbaddam3 0:175ad26d17ce 115 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
lbaddam3 0:175ad26d17ce 116 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
lbaddam3 0:175ad26d17ce 117 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
lbaddam3 0:175ad26d17ce 118 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
lbaddam3 0:175ad26d17ce 119 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
lbaddam3 0:175ad26d17ce 120 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
lbaddam3 0:175ad26d17ce 121 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
lbaddam3 0:175ad26d17ce 122 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
lbaddam3 0:175ad26d17ce 123 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
lbaddam3 0:175ad26d17ce 124 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
lbaddam3 0:175ad26d17ce 125 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
lbaddam3 0:175ad26d17ce 126 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
lbaddam3 0:175ad26d17ce 127 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
lbaddam3 0:175ad26d17ce 128 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
lbaddam3 0:175ad26d17ce 129 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
lbaddam3 0:175ad26d17ce 130 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
lbaddam3 0:175ad26d17ce 131 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
lbaddam3 0:175ad26d17ce 132 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
lbaddam3 0:175ad26d17ce 133 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
lbaddam3 0:175ad26d17ce 134 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
lbaddam3 0:175ad26d17ce 135 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
lbaddam3 0:175ad26d17ce 136 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
lbaddam3 0:175ad26d17ce 137 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
lbaddam3 0:175ad26d17ce 138 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
lbaddam3 0:175ad26d17ce 139 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
lbaddam3 0:175ad26d17ce 140 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
lbaddam3 0:175ad26d17ce 141 };
lbaddam3 0:175ad26d17ce 142
lbaddam3 0:175ad26d17ce 143 /* Values for Sawtooth Wave */
lbaddam3 0:175ad26d17ce 144 const uint8_t sawVals[SAMP_NUM] = {
lbaddam3 0:175ad26d17ce 145 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
lbaddam3 0:175ad26d17ce 146 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
lbaddam3 0:175ad26d17ce 147 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,
lbaddam3 0:175ad26d17ce 148 0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,
lbaddam3 0:175ad26d17ce 149 0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,
lbaddam3 0:175ad26d17ce 150 0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,
lbaddam3 0:175ad26d17ce 151 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,
lbaddam3 0:175ad26d17ce 152 0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,
lbaddam3 0:175ad26d17ce 153 0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,
lbaddam3 0:175ad26d17ce 154 0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,
lbaddam3 0:175ad26d17ce 155 0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,
lbaddam3 0:175ad26d17ce 156 0x58,0x59,0x5a,0x5b,0x5c,0x5d,0x5e,0x5f,
lbaddam3 0:175ad26d17ce 157 0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,
lbaddam3 0:175ad26d17ce 158 0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,
lbaddam3 0:175ad26d17ce 159 0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,
lbaddam3 0:175ad26d17ce 160 0x78,0x79,0x7a,0x7b,0x7c,0x7d,0x7e,0x7f,
lbaddam3 0:175ad26d17ce 161 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,
lbaddam3 0:175ad26d17ce 162 0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,
lbaddam3 0:175ad26d17ce 163 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,
lbaddam3 0:175ad26d17ce 164 0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,
lbaddam3 0:175ad26d17ce 165 0xa0,0xa1,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,
lbaddam3 0:175ad26d17ce 166 0xa8,0xa9,0xaa,0xab,0xac,0xad,0xae,0xaf,
lbaddam3 0:175ad26d17ce 167 0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,
lbaddam3 0:175ad26d17ce 168 0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0xbe,0xbf,
lbaddam3 0:175ad26d17ce 169 0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,
lbaddam3 0:175ad26d17ce 170 0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf,
lbaddam3 0:175ad26d17ce 171 0xd0,0xd1,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,
lbaddam3 0:175ad26d17ce 172 0xd8,0xd9,0xda,0xdb,0xdc,0xdd,0xde,0xdf,
lbaddam3 0:175ad26d17ce 173 0xe0,0xe1,0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,
lbaddam3 0:175ad26d17ce 174 0xe8,0xe9,0xea,0xeb,0xec,0xed,0xee,0xef,
lbaddam3 0:175ad26d17ce 175 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
lbaddam3 0:175ad26d17ce 176 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
lbaddam3 0:175ad26d17ce 177 };
lbaddam3 0:175ad26d17ce 178
lbaddam3 0:175ad26d17ce 179 /* Values for Reverse Sawtooth Wave */
lbaddam3 0:175ad26d17ce 180 const uint8_t revSawVals[SAMP_NUM] = {
lbaddam3 0:175ad26d17ce 181 0xff,0xfe,0xfd,0xfc,0xfb,0xfa,0xf9,0xf8,
lbaddam3 0:175ad26d17ce 182 0xf7,0xf6,0xf5,0xf4,0xf3,0xf2,0xf1,0xf0,
lbaddam3 0:175ad26d17ce 183 0xef,0xee,0xed,0xec,0xeb,0xea,0xe9,0xe8,
lbaddam3 0:175ad26d17ce 184 0xe7,0xe6,0xe5,0xe4,0xe3,0xe2,0xe1,0xe0,
lbaddam3 0:175ad26d17ce 185 0xdf,0xde,0xdd,0xdc,0xdb,0xda,0xd9,0xd8,
lbaddam3 0:175ad26d17ce 186 0xd7,0xd6,0xd5,0xd4,0xd3,0xd2,0xd1,0xd0,
lbaddam3 0:175ad26d17ce 187 0xcf,0xce,0xcd,0xcc,0xcb,0xca,0xc9,0xc8,
lbaddam3 0:175ad26d17ce 188 0xc7,0xc6,0xc5,0xc4,0xc3,0xc2,0xc1,0xc0,
lbaddam3 0:175ad26d17ce 189 0xbf,0xbe,0xbd,0xbc,0xbb,0xba,0xb9,0xb8,
lbaddam3 0:175ad26d17ce 190 0xb7,0xb6,0xb5,0xb4,0xb3,0xb2,0xb1,0xb0,
lbaddam3 0:175ad26d17ce 191 0xaf,0xae,0xad,0xac,0xab,0xaa,0xa9,0xa8,
lbaddam3 0:175ad26d17ce 192 0xa7,0xa6,0xa5,0xa4,0xa3,0xa2,0xa1,0xa0,
lbaddam3 0:175ad26d17ce 193 0x9f,0x9e,0x9d,0x9c,0x9b,0x9a,0x99,0x98,
lbaddam3 0:175ad26d17ce 194 0x97,0x96,0x95,0x94,0x93,0x92,0x91,0x90,
lbaddam3 0:175ad26d17ce 195 0x8f,0x8e,0x8d,0x8c,0x8b,0x8a,0x89,0x88,
lbaddam3 0:175ad26d17ce 196 0x87,0x86,0x85,0x84,0x83,0x82,0x81,0x80,
lbaddam3 0:175ad26d17ce 197 0x7f,0x7e,0x7d,0x7c,0x7b,0x7a,0x79,0x78,
lbaddam3 0:175ad26d17ce 198 0x77,0x76,0x75,0x74,0x73,0x72,0x71,0x70,
lbaddam3 0:175ad26d17ce 199 0x6f,0x6e,0x6d,0x6c,0x6b,0x6a,0x69,0x68,
lbaddam3 0:175ad26d17ce 200 0x67,0x66,0x65,0x64,0x63,0x62,0x61,0x60,
lbaddam3 0:175ad26d17ce 201 0x5f,0x5e,0x5d,0x5c,0x5b,0x5a,0x59,0x58,
lbaddam3 0:175ad26d17ce 202 0x57,0x56,0x55,0x54,0x53,0x52,0x51,0x50,
lbaddam3 0:175ad26d17ce 203 0x4f,0x4e,0x4d,0x4c,0x4b,0x4a,0x49,0x48,
lbaddam3 0:175ad26d17ce 204 0x47,0x46,0x45,0x44,0x43,0x42,0x41,0x40,
lbaddam3 0:175ad26d17ce 205 0x3f,0x3e,0x3d,0x3c,0x3b,0x3a,0x39,0x38,
lbaddam3 0:175ad26d17ce 206 0x37,0x36,0x35,0x34,0x33,0x32,0x31,0x30,
lbaddam3 0:175ad26d17ce 207 0x2f,0x2e,0x2d,0x2c,0x2b,0x2a,0x29,0x28,
lbaddam3 0:175ad26d17ce 208 0x27,0x26,0x25,0x24,0x23,0x22,0x21,0x20,
lbaddam3 0:175ad26d17ce 209 0x1f,0x1e,0x1d,0x1c,0x1b,0x1a,0x19,0x18,
lbaddam3 0:175ad26d17ce 210 0x17,0x16,0x15,0x14,0x13,0x12,0x11,0x10,
lbaddam3 0:175ad26d17ce 211 0x0f,0x0e,0x0d,0x0c,0x0b,0x0a,0x09,0x08,
lbaddam3 0:175ad26d17ce 212 0x07,0x06,0x05,0x04,0x03,0x02,0x01,0x00
lbaddam3 0:175ad26d17ce 213 };
lbaddam3 0:175ad26d17ce 214
lbaddam3 0:175ad26d17ce 215 /* Values for Triangle Wave */
lbaddam3 0:175ad26d17ce 216 const uint8_t triVals[SAMP_NUM] = {
lbaddam3 0:175ad26d17ce 217 0x00,0x02,0x04,0x06,0x08,0x0a,0x0c,0x0e,
lbaddam3 0:175ad26d17ce 218 0x10,0x12,0x14,0x16,0x18,0x1a,0x1c,0x1e,
lbaddam3 0:175ad26d17ce 219 0x20,0x22,0x24,0x26,0x28,0x2a,0x2c,0x2e,
lbaddam3 0:175ad26d17ce 220 0x30,0x32,0x34,0x36,0x38,0x3a,0x3c,0x3e,
lbaddam3 0:175ad26d17ce 221 0x40,0x42,0x44,0x46,0x48,0x4a,0x4c,0x4e,
lbaddam3 0:175ad26d17ce 222 0x50,0x52,0x54,0x56,0x58,0x5a,0x5c,0x5e,
lbaddam3 0:175ad26d17ce 223 0x60,0x62,0x64,0x66,0x68,0x6a,0x6c,0x6e,
lbaddam3 0:175ad26d17ce 224 0x70,0x72,0x74,0x76,0x78,0x7a,0x7c,0x7e,
lbaddam3 0:175ad26d17ce 225 0x80,0x82,0x84,0x86,0x88,0x8a,0x8c,0x8e,
lbaddam3 0:175ad26d17ce 226 0x90,0x92,0x94,0x96,0x98,0x9a,0x9c,0x9e,
lbaddam3 0:175ad26d17ce 227 0xa0,0xa2,0xa4,0xa6,0xa8,0xaa,0xac,0xae,
lbaddam3 0:175ad26d17ce 228 0xb0,0xb2,0xb4,0xb6,0xb8,0xba,0xbc,0xbe,
lbaddam3 0:175ad26d17ce 229 0xc0,0xc2,0xc4,0xc6,0xc8,0xca,0xcc,0xce,
lbaddam3 0:175ad26d17ce 230 0xd0,0xd2,0xd4,0xd6,0xd8,0xda,0xdc,0xde,
lbaddam3 0:175ad26d17ce 231 0xe0,0xe2,0xe4,0xe6,0xe8,0xea,0xec,0xee,
lbaddam3 0:175ad26d17ce 232 0xf0,0xf2,0xf4,0xf6,0xf8,0xfa,0xfc,0xfe,
lbaddam3 0:175ad26d17ce 233 0xff,0xfd,0xfb,0xf9,0xf7,0xf5,0xf3,0xf1,
lbaddam3 0:175ad26d17ce 234 0xef,0xef,0xeb,0xe9,0xe7,0xe5,0xe3,0xe1,
lbaddam3 0:175ad26d17ce 235 0xdf,0xdd,0xdb,0xd9,0xd7,0xd5,0xd3,0xd1,
lbaddam3 0:175ad26d17ce 236 0xcf,0xcf,0xcb,0xc9,0xc7,0xc5,0xc3,0xc1,
lbaddam3 0:175ad26d17ce 237 0xbf,0xbd,0xbb,0xb9,0xb7,0xb5,0xb3,0xb1,
lbaddam3 0:175ad26d17ce 238 0xaf,0xaf,0xab,0xa9,0xa7,0xa5,0xa3,0xa1,
lbaddam3 0:175ad26d17ce 239 0x9f,0x9d,0x9b,0x99,0x97,0x95,0x93,0x91,
lbaddam3 0:175ad26d17ce 240 0x8f,0x8f,0x8b,0x89,0x87,0x85,0x83,0x81,
lbaddam3 0:175ad26d17ce 241 0x7f,0x7d,0x7b,0x79,0x77,0x75,0x73,0x71,
lbaddam3 0:175ad26d17ce 242 0x6f,0x6f,0x6b,0x69,0x67,0x65,0x63,0x61,
lbaddam3 0:175ad26d17ce 243 0x5f,0x5d,0x5b,0x59,0x57,0x55,0x53,0x51,
lbaddam3 0:175ad26d17ce 244 0x4f,0x4f,0x4b,0x49,0x47,0x45,0x43,0x41,
lbaddam3 0:175ad26d17ce 245 0x3f,0x3d,0x3b,0x39,0x37,0x35,0x33,0x31,
lbaddam3 0:175ad26d17ce 246 0x2f,0x2f,0x2b,0x29,0x27,0x25,0x23,0x21,
lbaddam3 0:175ad26d17ce 247 0x1f,0x1d,0x1b,0x19,0x17,0x15,0x13,0x11,
lbaddam3 0:175ad26d17ce 248 0x0f,0x0f,0x0b,0x09,0x07,0x05,0x03,0x01
lbaddam3 0:175ad26d17ce 249 };
lbaddam3 0:175ad26d17ce 250
lbaddam3 0:175ad26d17ce 251 /* Values for EKG Wave */
lbaddam3 0:175ad26d17ce 252 const uint8_t ekgVals[SAMP_NUM] = {
lbaddam3 0:175ad26d17ce 253 0x49,0x4a,0x4b,0x4b,0x4a,0x49,0x49,0x49,
lbaddam3 0:175ad26d17ce 254 0x49,0x48,0x47,0x45,0x44,0x43,0x43,0x43,
lbaddam3 0:175ad26d17ce 255 0x44,0x44,0x43,0x41,0x3e,0x3d,0x3b,0x39,
lbaddam3 0:175ad26d17ce 256 0x38,0x37,0x37,0x36,0x36,0x36,0x37,0x37,
lbaddam3 0:175ad26d17ce 257 0x37,0x37,0x37,0x37,0x36,0x35,0x33,0x32,
lbaddam3 0:175ad26d17ce 258 0x31,0x31,0x34,0x3d,0x4d,0x65,0x84,0xa9,
lbaddam3 0:175ad26d17ce 259 0xcf,0xee,0xff,0xfe,0xea,0xc6,0x9a,0x6d,
lbaddam3 0:175ad26d17ce 260 0x44,0x25,0x11,0x05,0x00,0x01,0x06,0x0d,
lbaddam3 0:175ad26d17ce 261 0x14,0x1c,0x24,0x2d,0x34,0x39,0x3d,0x40,
lbaddam3 0:175ad26d17ce 262 0x41,0x42,0x43,0x44,0x44,0x45,0x46,0x47,
lbaddam3 0:175ad26d17ce 263 0x47,0x47,0x47,0x47,0x47,0x47,0x47,0x48,
lbaddam3 0:175ad26d17ce 264 0x48,0x48,0x49,0x49,0x4a,0x4b,0x4b,0x4e,
lbaddam3 0:175ad26d17ce 265 0x4d,0x4e,0x4f,0x50,0x51,0x52,0x53,0x54,
lbaddam3 0:175ad26d17ce 266 0x56,0x58,0x5B,0x5D,0x60,0x62,0x64,0x66,
lbaddam3 0:175ad26d17ce 267 0x68,0x6b,0x6d,0x70,0x73,0x74,0x79,0x7b,
lbaddam3 0:175ad26d17ce 268 0x7d,0x7e,0x7f,0x7f,0x7f,0x7f,0x7f,0x7e,
lbaddam3 0:175ad26d17ce 269 0x7d,0x7c,0x79,0x77,0x74,0x71,0x6d,0x69,
lbaddam3 0:175ad26d17ce 270 0x66,0x62,0x5f,0x5c,0x59,0x57,0x54,0x51,
lbaddam3 0:175ad26d17ce 271 0x4f,0x4d,0x4e,0x4b,0x4a,0x49,0x48,0x46,
lbaddam3 0:175ad26d17ce 272 0x45,0x44,0x43,0x43,0x43,0x44,0x44,0x44,
lbaddam3 0:175ad26d17ce 273 0x45,0x45,0x45,0x45,0x45,0x45,0x45,0x46,
lbaddam3 0:175ad26d17ce 274 0x47,0x48,0x49,0x49,0x4a,0x4a,0x4b,0x4b,
lbaddam3 0:175ad26d17ce 275 0x4b,0x4b,0x4b,0x4b,0x4a,0x4a,0x49,0x49,
lbaddam3 0:175ad26d17ce 276 0x49,0x49,0x48,0x48,0x48,0x47,0x47,0x47,
lbaddam3 0:175ad26d17ce 277 0x47,0x47,0x47,0x47,0x46,0x46,0x46,0x45,
lbaddam3 0:175ad26d17ce 278 0x45,0x45,0x45,0x45,0x46,0x46,0x46,0x45,
lbaddam3 0:175ad26d17ce 279 0x44,0x44,0x43,0x43,0x43,0x43,0x42,0x42,
lbaddam3 0:175ad26d17ce 280 0x42,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
lbaddam3 0:175ad26d17ce 281 0x41,0x40,0x40,0x3f,0x3f,0x40,0x40,0x41,
lbaddam3 0:175ad26d17ce 282 0x41,0x41,0x41,0x41,0x41,0x41,0x40,0x40,
lbaddam3 0:175ad26d17ce 283 0x40,0x40,0x40,0x40,0x40,0x40,0x41,0x41,
lbaddam3 0:175ad26d17ce 284 0x41,0x42,0x43,0x44,0x45,0x47,0x48,0x49
lbaddam3 0:175ad26d17ce 285 };
lbaddam3 0:175ad26d17ce 286
lbaddam3 0:175ad26d17ce 287 /* Values for Sinc Wave */
lbaddam3 0:175ad26d17ce 288 const uint8_t sincVals[SAMP_NUM] = {
lbaddam3 0:175ad26d17ce 289 0x2d,0x2f,0x31,0x32,0x32,0x32,0x31,0x30,
lbaddam3 0:175ad26d17ce 290 0x2e,0x2d,0x2b,0x2a,0x29,0x29,0x29,0x2a,
lbaddam3 0:175ad26d17ce 291 0x2c,0x2e,0x2f,0x31,0x32,0x33,0x33,0x32,
lbaddam3 0:175ad26d17ce 292 0x30,0x2e,0x2c,0x2a,0x29,0x28,0x28,0x28,
lbaddam3 0:175ad26d17ce 293 0x2a,0x2c,0x2e,0x30,0x32,0x33,0x34,0x34,
lbaddam3 0:175ad26d17ce 294 0x32,0x31,0x2e,0x2c,0x2a,0x28,0x27,0x27,
lbaddam3 0:175ad26d17ce 295 0x27,0x29,0x2b,0x2e,0x31,0x33,0x35,0x35,
lbaddam3 0:175ad26d17ce 296 0x35,0x33,0x31,0x2e,0x2b,0x28,0x26,0x25,
lbaddam3 0:175ad26d17ce 297 0x25,0x26,0x28,0x2b,0x2e,0x32,0x35,0x37,
lbaddam3 0:175ad26d17ce 298 0x38,0x37,0x35,0x32,0x2e,0x2a,0x26,0x23,
lbaddam3 0:175ad26d17ce 299 0x22,0x21,0x23,0x26,0x2a,0x2f,0x34,0x38,
lbaddam3 0:175ad26d17ce 300 0x3b,0x3c,0x3b,0x39,0x34,0x2e,0x28,0x22,
lbaddam3 0:175ad26d17ce 301 0x1e,0x1b,0x1b,0x1d,0x22,0x29,0x31,0x39,
lbaddam3 0:175ad26d17ce 302 0x41,0x46,0x48,0x47,0x42,0x3a,0x2e,0x21,
lbaddam3 0:175ad26d17ce 303 0x15,0x0a,0x02,0x00,0x04,0x0f,0x21,0x3a,
lbaddam3 0:175ad26d17ce 304 0x57,0x78,0x9a,0xba,0xd6,0xec,0xfa,0xff,
lbaddam3 0:175ad26d17ce 305 0xfa,0xec,0xd6,0xba,0x9a,0x78,0x57,0x3a,
lbaddam3 0:175ad26d17ce 306 0x21,0x0f,0x04,0x00,0x02,0x0a,0x15,0x21,
lbaddam3 0:175ad26d17ce 307 0x2e,0x3a,0x42,0x47,0x48,0x46,0x41,0x39,
lbaddam3 0:175ad26d17ce 308 0x31,0x29,0x22,0x1d,0x1b,0x1b,0x1e,0x22,
lbaddam3 0:175ad26d17ce 309 0x28,0x2e,0x34,0x39,0x3b,0x3c,0x3b,0x38,
lbaddam3 0:175ad26d17ce 310 0x34,0x2f,0x2a,0x26,0x23,0x21,0x22,0x23,
lbaddam3 0:175ad26d17ce 311 0x26,0x2a,0x2e,0x32,0x35,0x37,0x38,0x37,
lbaddam3 0:175ad26d17ce 312 0x35,0x32,0x2e,0x2b,0x28,0x26,0x25,0x25,
lbaddam3 0:175ad26d17ce 313 0x26,0x28,0x2b,0x2e,0x31,0x33,0x35,0x35,
lbaddam3 0:175ad26d17ce 314 0x35,0x33,0x31,0x2e,0x2b,0x29,0x27,0x27,
lbaddam3 0:175ad26d17ce 315 0x27,0x28,0x2a,0x2c,0x2e,0x31,0x32,0x34,
lbaddam3 0:175ad26d17ce 316 0x34,0x33,0x32,0x30,0x2e,0x2c,0x2a,0x28,
lbaddam3 0:175ad26d17ce 317 0x28,0x28,0x29,0x2a,0x2c,0x2e,0x30,0x32,
lbaddam3 0:175ad26d17ce 318 0x33,0x33,0x32,0x31,0x2f,0x2e,0x2c,0x2a,
lbaddam3 0:175ad26d17ce 319 0x29,0x29,0x29,0x2a,0x2b,0x2d,0x2e,0x30,
lbaddam3 0:175ad26d17ce 320 0x31,0x32,0x32,0x32,0x31,0x2f,0x2d,0x2d
lbaddam3 0:175ad26d17ce 321 };
lbaddam3 0:175ad26d17ce 322
lbaddam3 0:175ad26d17ce 323 /* Notes */
lbaddam3 0:175ad26d17ce 324 const uint16_t notes[OCT_NUM][NOTE_NUM] = { // Array of Notes
lbaddam3 0:175ad26d17ce 325 {C_0, Cs_0, D_0, Ds_0, E_0, F_0, Fs_0, G_0, Gs_0, A_0, As_0, B_0},
lbaddam3 0:175ad26d17ce 326 {C_1, Cs_1, D_1, Ds_1, E_1, F_1, Fs_1, G_1, Gs_1, A_1, As_1, B_1},
lbaddam3 0:175ad26d17ce 327 {C_2, Cs_2, D_2, Ds_2, E_2, F_2, Fs_2, G_2, Gs_2, A_2, As_2, B_2},
lbaddam3 0:175ad26d17ce 328 {C_3, Cs_3, D_3, Ds_3, E_3, F_3, Fs_3, G_3, Gs_3, A_3, As_3, B_3},
lbaddam3 0:175ad26d17ce 329 {C_4, Cs_4, D_4, Ds_4, E_4, F_4, Fs_4, G_4, Gs_4, A_4, As_4, B_4},
lbaddam3 0:175ad26d17ce 330 {C_5, Cs_5, D_5, Ds_5, E_5, F_5, Fs_5, G_5, Gs_5, A_5, As_5, B_5},
lbaddam3 0:175ad26d17ce 331 {C_6, Cs_6, D_6, Ds_6, E_6, F_6, Fs_6, G_6, Gs_6, A_6, As_6, B_6},
lbaddam3 0:175ad26d17ce 332 {C_7, Cs_7, D_7, Ds_7, E_7, F_7, Fs_7, G_7, Gs_7, A_7, As_7, B_7},
lbaddam3 0:175ad26d17ce 333 {C_8, Cs_8, D_8, Ds_8, E_8, F_8, Fs_8, G_8, Gs_8, A_8, As_8, B_8}
lbaddam3 0:175ad26d17ce 334 };
lbaddam3 0:175ad26d17ce 335
lbaddam3 0:175ad26d17ce 336 /* Function Definitions */
lbaddam3 0:175ad26d17ce 337
lbaddam3 0:175ad26d17ce 338 /**
lbaddam3 0:175ad26d17ce 339 * This function adds the frequency tuning word present @ index to the
lbaddam3 0:175ad26d17ce 340 * phase accumulator present @ index.
lbaddam3 0:175ad26d17ce 341 *
lbaddam3 0:175ad26d17ce 342 * param: index - the channel number of oscillator
lbaddam3 0:175ad26d17ce 343 */
lbaddam3 0:175ad26d17ce 344 //inline void acc_inc(const uint8_t index) {
lbaddam3 0:175ad26d17ce 345 void acc_inc(void) {
lbaddam3 0:175ad26d17ce 346 phaseAcc += freqTun;
lbaddam3 0:175ad26d17ce 347 }
lbaddam3 0:175ad26d17ce 348
lbaddam3 0:175ad26d17ce 349 /**
lbaddam3 0:175ad26d17ce 350 * This function resets the phase accumulator for the oscillator present
lbaddam3 0:175ad26d17ce 351 * @ index. Should be called when switching frequencies.
lbaddam3 0:175ad26d17ce 352 *
lbaddam3 0:175ad26d17ce 353 * param: index - the channel number of oscillator
lbaddam3 0:175ad26d17ce 354 */
lbaddam3 0:175ad26d17ce 355 //inline void acc_reset(const uint8_t index) {
lbaddam3 0:175ad26d17ce 356 void acc_reset(void) {
lbaddam3 0:175ad26d17ce 357 phaseAcc = 0;
lbaddam3 0:175ad26d17ce 358 }
lbaddam3 0:175ad26d17ce 359
lbaddam3 0:175ad26d17ce 360 /**
lbaddam3 0:175ad26d17ce 361 * This function calculates the frequency tuning word of the oscillator
lbaddam3 0:175ad26d17ce 362 * present @ index.
lbaddam3 0:175ad26d17ce 363 *
lbaddam3 0:175ad26d17ce 364 * param: index - the channel number of oscillator
lbaddam3 0:175ad26d17ce 365 */
lbaddam3 0:175ad26d17ce 366 //inline void set_wave(const Wave waveType, const uint8_t index) {
lbaddam3 0:175ad26d17ce 367 void set_wave(const Wave waveType) {
lbaddam3 0:175ad26d17ce 368 switch(waveType) {
lbaddam3 0:175ad26d17ce 369 case SINE:
lbaddam3 0:175ad26d17ce 370 waveTable = sinVals;
lbaddam3 0:175ad26d17ce 371 break;
lbaddam3 0:175ad26d17ce 372 case SQUARE:
lbaddam3 0:175ad26d17ce 373 waveTable = sqVals;
lbaddam3 0:175ad26d17ce 374 break;
lbaddam3 0:175ad26d17ce 375 case SAWTOOTH:
lbaddam3 0:175ad26d17ce 376 waveTable = sawVals;
lbaddam3 0:175ad26d17ce 377 break;
lbaddam3 0:175ad26d17ce 378 case REV_SAWTOOTH:
lbaddam3 0:175ad26d17ce 379 waveTable = revSawVals;
lbaddam3 0:175ad26d17ce 380 break;
lbaddam3 0:175ad26d17ce 381 case TRIANGLE:
lbaddam3 0:175ad26d17ce 382 waveTable = triVals;
lbaddam3 0:175ad26d17ce 383 break;
lbaddam3 0:175ad26d17ce 384 case EKG:
lbaddam3 0:175ad26d17ce 385 waveTable = ekgVals;
lbaddam3 0:175ad26d17ce 386 break;
lbaddam3 0:175ad26d17ce 387 case SINC:
lbaddam3 0:175ad26d17ce 388 waveTable = sincVals;
lbaddam3 0:175ad26d17ce 389 break;
lbaddam3 0:175ad26d17ce 390 default:
lbaddam3 0:175ad26d17ce 391 break;
lbaddam3 0:175ad26d17ce 392 }
lbaddam3 0:175ad26d17ce 393 }
lbaddam3 0:175ad26d17ce 394
lbaddam3 0:175ad26d17ce 395 /**
lbaddam3 0:175ad26d17ce 396 * This function calculates the frequency tuning word of the oscillator
lbaddam3 0:175ad26d17ce 397 */
lbaddam3 0:175ad26d17ce 398 void set_freq_tun(void) {
lbaddam3 0:175ad26d17ce 399 freqTun = ((((uint32_t)waveFreq) * SAMP_NUM) *
lbaddam3 0:175ad26d17ce 400 (SHFT_FCTR / SAMP_FREQ));
lbaddam3 0:175ad26d17ce 401 }
lbaddam3 0:175ad26d17ce 402
lbaddam3 0:175ad26d17ce 403 /**
lbaddam3 0:175ad26d17ce 404 * This function sets the frequency of the oscillator. In
lbaddam3 0:175ad26d17ce 405 * addition, this function calculates the corresponding frequency tuning word.
lbaddam3 0:175ad26d17ce 406 *
lbaddam3 0:175ad26d17ce 407 * param: freq - frequency of waveform
lbaddam3 0:175ad26d17ce 408 */
lbaddam3 0:175ad26d17ce 409 //inline void set_freq(const uint16_t freq, const uint8_t index) {
lbaddam3 0:175ad26d17ce 410 void set_freq(const uint16_t freq) {
lbaddam3 0:175ad26d17ce 411 acc_reset();
lbaddam3 0:175ad26d17ce 412 waveFreq = freq;
lbaddam3 0:175ad26d17ce 413 set_freq_tun();
lbaddam3 0:175ad26d17ce 414 }
lbaddam3 0:175ad26d17ce 415
lbaddam3 0:175ad26d17ce 416 /**
lbaddam3 0:175ad26d17ce 417 * This function sends the current waveform value for each channel
lbaddam3 0:175ad26d17ce 418 * to the appropriate output port. Checks are made to ensure frequency
lbaddam3 0:175ad26d17ce 419 * and waveform values are up to date.
lbaddam3 0:175ad26d17ce 420 */
lbaddam3 0:175ad26d17ce 421 void send_vals(void) {
lbaddam3 0:175ad26d17ce 422 if (freqChange == TRUE) {
lbaddam3 0:175ad26d17ce 423 set_freq(newFreq); // Set new frequency
lbaddam3 0:175ad26d17ce 424 freqChange = FALSE; // Reset freqChange
lbaddam3 0:175ad26d17ce 425 }
lbaddam3 0:175ad26d17ce 426
lbaddam3 0:175ad26d17ce 427 if (waveChange == TRUE) {
lbaddam3 0:175ad26d17ce 428 set_wave(newWave); // Set new waveform
lbaddam3 0:175ad26d17ce 429 waveChange = FALSE; // Reset waveChange
lbaddam3 0:175ad26d17ce 430 }
lbaddam3 0:175ad26d17ce 431
lbaddam3 0:175ad26d17ce 432 acc_inc(); // Increment phase accumulator
lbaddam3 0:175ad26d17ce 433 waveIndex = phaseAcc >> 24; // Get address into wavetable
lbaddam3 0:175ad26d17ce 434
lbaddam3 0:175ad26d17ce 435 if (noteOn == TRUE)
lbaddam3 0:175ad26d17ce 436 synthPin = waveTable[waveIndex]/((float)(SAMP_NUM-1));
lbaddam3 0:175ad26d17ce 437 }
lbaddam3 0:175ad26d17ce 438
lbaddam3 0:175ad26d17ce 439 void init_synth(void) {
lbaddam3 0:175ad26d17ce 440 // Setup initial waveforms
lbaddam3 0:175ad26d17ce 441 acc_reset(); // Set all accumulators to 0 initially
lbaddam3 0:175ad26d17ce 442 set_wave(myWave); // Set all waveforms to sine wave
lbaddam3 0:175ad26d17ce 443 set_freq(notes[octave][A_NOTE]);
lbaddam3 0:175ad26d17ce 444 rnd = waveChange = freqChange = FALSE;
lbaddam3 0:175ad26d17ce 445 }
lbaddam3 0:175ad26d17ce 446
lbaddam3 0:175ad26d17ce 447 int main(void) {
lbaddam3 0:175ad26d17ce 448 PS2Keyboard::keyboard_event_t evt_kb;
lbaddam3 0:175ad26d17ce 449 init_synth(); // Initialize synth
lbaddam3 0:175ad26d17ce 450
lbaddam3 0:175ad26d17ce 451 int samp_us = 1.0/SAMP_FREQ*1e6; // Determine sample period in microseconds
lbaddam3 0:175ad26d17ce 452
lbaddam3 0:175ad26d17ce 453 synth_tick.attach_us(&send_vals, samp_us); // Call send_vals every samp_us
lbaddam3 0:175ad26d17ce 454 while (1) { // Loop here forever
lbaddam3 0:175ad26d17ce 455 // Process keys and determine which waveform, octave, and note should be
lbaddam3 0:175ad26d17ce 456 // played
lbaddam3 0:175ad26d17ce 457 if (ps2kb.processing(&evt_kb)) {
lbaddam3 0:175ad26d17ce 458 if (!evt_kb.type) {
lbaddam3 0:175ad26d17ce 459 switch(evt_kb.scancode[0]) {
lbaddam3 0:175ad26d17ce 460 case 0xe0:
lbaddam3 0:175ad26d17ce 461 switch(evt_kb.scancode[1]) {
lbaddam3 0:175ad26d17ce 462 case Right_key:
lbaddam3 0:175ad26d17ce 463 ++octave;
lbaddam3 0:175ad26d17ce 464 break;
lbaddam3 0:175ad26d17ce 465 case Left_key:
lbaddam3 0:175ad26d17ce 466 --octave;
lbaddam3 0:175ad26d17ce 467 break;
lbaddam3 0:175ad26d17ce 468 default:
lbaddam3 0:175ad26d17ce 469 printf("Invalid Octave Entry\n\r");
lbaddam3 0:175ad26d17ce 470 break;
lbaddam3 0:175ad26d17ce 471 }
lbaddam3 0:175ad26d17ce 472 case A_key:
lbaddam3 0:175ad26d17ce 473 set_freq(notes[octave][C_NOTE]);
lbaddam3 0:175ad26d17ce 474 noteOn = TRUE;
lbaddam3 0:175ad26d17ce 475 break;
lbaddam3 0:175ad26d17ce 476 case W_key:
lbaddam3 0:175ad26d17ce 477 set_freq(notes[octave][Cs_NOTE]);
lbaddam3 0:175ad26d17ce 478 noteOn = TRUE;
lbaddam3 0:175ad26d17ce 479 break;
lbaddam3 0:175ad26d17ce 480 case S_key:
lbaddam3 0:175ad26d17ce 481 set_freq(notes[octave][D_NOTE]);
lbaddam3 0:175ad26d17ce 482 noteOn = TRUE;
lbaddam3 0:175ad26d17ce 483 break;
lbaddam3 0:175ad26d17ce 484 case E_key:
lbaddam3 0:175ad26d17ce 485 set_freq(notes[octave][Ds_NOTE]);
lbaddam3 0:175ad26d17ce 486 noteOn = TRUE;
lbaddam3 0:175ad26d17ce 487 break;
lbaddam3 0:175ad26d17ce 488 case D_key:
lbaddam3 0:175ad26d17ce 489 set_freq(notes[octave][E_NOTE]);
lbaddam3 0:175ad26d17ce 490 noteOn = TRUE;
lbaddam3 0:175ad26d17ce 491 break;
lbaddam3 0:175ad26d17ce 492 case F_key:
lbaddam3 0:175ad26d17ce 493 set_freq(notes[octave][F_NOTE]);
lbaddam3 0:175ad26d17ce 494 noteOn = TRUE;
lbaddam3 0:175ad26d17ce 495 break;
lbaddam3 0:175ad26d17ce 496 case T_key:
lbaddam3 0:175ad26d17ce 497 set_freq(notes[octave][Fs_NOTE]);
lbaddam3 0:175ad26d17ce 498 noteOn = TRUE;
lbaddam3 0:175ad26d17ce 499 break;
lbaddam3 0:175ad26d17ce 500 case J_key:
lbaddam3 0:175ad26d17ce 501 set_freq(notes[octave][G_NOTE]);
lbaddam3 0:175ad26d17ce 502 noteOn = TRUE;
lbaddam3 0:175ad26d17ce 503 break;
lbaddam3 0:175ad26d17ce 504 case I_key:
lbaddam3 0:175ad26d17ce 505 set_freq(notes[octave][Gs_NOTE]);
lbaddam3 0:175ad26d17ce 506 noteOn = TRUE;
lbaddam3 0:175ad26d17ce 507 break;
lbaddam3 0:175ad26d17ce 508 case K_key:
lbaddam3 0:175ad26d17ce 509 set_freq(notes[octave][A_NOTE]);
lbaddam3 0:175ad26d17ce 510 noteOn = TRUE;
lbaddam3 0:175ad26d17ce 511 break;
lbaddam3 0:175ad26d17ce 512 case O_key:
lbaddam3 0:175ad26d17ce 513 set_freq(notes[octave][As_NOTE]);
lbaddam3 0:175ad26d17ce 514 noteOn = TRUE;
lbaddam3 0:175ad26d17ce 515 break;
lbaddam3 0:175ad26d17ce 516 case L_key:
lbaddam3 0:175ad26d17ce 517 set_freq(notes[octave][B_NOTE]);
lbaddam3 0:175ad26d17ce 518 noteOn = TRUE;
lbaddam3 0:175ad26d17ce 519 break;
lbaddam3 0:175ad26d17ce 520 case Sc_key:
lbaddam3 0:175ad26d17ce 521 if (octave < OCT_NUM-2) {
lbaddam3 0:175ad26d17ce 522 set_freq(notes[octave+1][C_NOTE]);
lbaddam3 0:175ad26d17ce 523 noteOn = TRUE;
lbaddam3 0:175ad26d17ce 524 break;
lbaddam3 0:175ad26d17ce 525 }
lbaddam3 0:175ad26d17ce 526 case Plus_key:
lbaddam3 0:175ad26d17ce 527 switch (myWave){
lbaddam3 0:175ad26d17ce 528 case SINE:
lbaddam3 0:175ad26d17ce 529 myWave = SQUARE;
lbaddam3 0:175ad26d17ce 530 break;
lbaddam3 0:175ad26d17ce 531 case SQUARE:
lbaddam3 0:175ad26d17ce 532 myWave = SAWTOOTH;
lbaddam3 0:175ad26d17ce 533 break;
lbaddam3 0:175ad26d17ce 534 case SAWTOOTH:
lbaddam3 0:175ad26d17ce 535 myWave = TRIANGLE;
lbaddam3 0:175ad26d17ce 536 break;
lbaddam3 0:175ad26d17ce 537 case TRIANGLE:
lbaddam3 0:175ad26d17ce 538 myWave = EKG;
lbaddam3 0:175ad26d17ce 539 break;
lbaddam3 0:175ad26d17ce 540 case EKG:
lbaddam3 0:175ad26d17ce 541 myWave = SINC;
lbaddam3 0:175ad26d17ce 542 break;
lbaddam3 0:175ad26d17ce 543 case SINC:
lbaddam3 0:175ad26d17ce 544 myWave = SINC;
lbaddam3 0:175ad26d17ce 545 break;
lbaddam3 0:175ad26d17ce 546 default:
lbaddam3 0:175ad26d17ce 547 break;
lbaddam3 0:175ad26d17ce 548 }
lbaddam3 0:175ad26d17ce 549 set_wave(myWave);
lbaddam3 0:175ad26d17ce 550 break;
lbaddam3 0:175ad26d17ce 551 case Minus_key:
lbaddam3 0:175ad26d17ce 552 switch (myWave){
lbaddam3 0:175ad26d17ce 553 case SINE:
lbaddam3 0:175ad26d17ce 554 myWave = SINE;
lbaddam3 0:175ad26d17ce 555 break;
lbaddam3 0:175ad26d17ce 556 case SQUARE:
lbaddam3 0:175ad26d17ce 557 myWave = SINE;
lbaddam3 0:175ad26d17ce 558 break;
lbaddam3 0:175ad26d17ce 559 case SAWTOOTH:
lbaddam3 0:175ad26d17ce 560 myWave = SQUARE;
lbaddam3 0:175ad26d17ce 561 break;
lbaddam3 0:175ad26d17ce 562 case TRIANGLE:
lbaddam3 0:175ad26d17ce 563 myWave = SAWTOOTH;
lbaddam3 0:175ad26d17ce 564 break;
lbaddam3 0:175ad26d17ce 565 case EKG:
lbaddam3 0:175ad26d17ce 566 myWave = TRIANGLE;
lbaddam3 0:175ad26d17ce 567 break;
lbaddam3 0:175ad26d17ce 568 case SINC:
lbaddam3 0:175ad26d17ce 569 myWave = EKG;
lbaddam3 0:175ad26d17ce 570 break;
lbaddam3 0:175ad26d17ce 571 default:
lbaddam3 0:175ad26d17ce 572 break;
lbaddam3 0:175ad26d17ce 573 }
lbaddam3 0:175ad26d17ce 574 set_wave(myWave);
lbaddam3 0:175ad26d17ce 575 break;
lbaddam3 0:175ad26d17ce 576 default:
lbaddam3 0:175ad26d17ce 577 printf("Invalid Key Entry\n\r");
lbaddam3 0:175ad26d17ce 578 break;
lbaddam3 0:175ad26d17ce 579 }
lbaddam3 0:175ad26d17ce 580 }
lbaddam3 0:175ad26d17ce 581 else {
lbaddam3 0:175ad26d17ce 582 noteOn = FALSE;
lbaddam3 0:175ad26d17ce 583 }
lbaddam3 0:175ad26d17ce 584 if (octave < 0)
lbaddam3 0:175ad26d17ce 585 octave = 0;
lbaddam3 0:175ad26d17ce 586 else if (octave > OCT_NUM-1)
lbaddam3 0:175ad26d17ce 587 octave = OCT_NUM-1;
lbaddam3 0:175ad26d17ce 588 }
lbaddam3 0:175ad26d17ce 589 }
lbaddam3 0:175ad26d17ce 590
lbaddam3 0:175ad26d17ce 591 return 0;
lbaddam3 0:175ad26d17ce 592 }