Jim says
All very cool.
In my office we talked about what this might be slow,
ways to test the different sorts of loops (loop(),
a for loop inside that, and so on) with micro() function.
We also glanced at the microcontroller toolchain,
in case it would be someday fun to start from stratch
for forth or something . Google (avr gcc), which
we saw on the arduino page as how they compiled their language.
Making Noise
This week was dedicated to making the Due actually make audible noise.
Last week I found that even the Due's 84 Mhz CPU is not fast enough to
generate tones on the fly (using arduino wrappers).
I did some research into what kind of optimization I could make and it appears
that similar techniques can be used even though the process is as ARM chip. I
wanted to test is on a stripped down version before committing to a whole
rewrite of the original script. The three things I was going to planned to
were:
- Switch all wave to lookup table.
- Figure out how to alter the prescaler setting.
- Possibly move some of the DAC into hardware.
First I switched to a lookup table for the wave generation, but it was
unexpectedly slow. I found out that it was due to the fact that I had set
the analogue resolution to 12-bits (which makes it far too slow). I bumped it
down to 8 bits and started seeing some nicer results. I also switched all
relevant numbers to byte (8 bit) types instead of ints (32 bit).
Looking into hardware DAC, I found myself
here. It was looking hopeful until I poked around in the manual for the processor and looked at the
port mapping. The ports are too scattered and would make a really ugly, kludgy mess.
I found
this article (which is a treasure trove) that helped me figure out how to change the prescaler register.
I decided to debug and fix the DAC pins. After getting those up an running, writing some look up table, and converting ints to bytes, I was able to get up to ~700Hz (double the last attempt). This is satisfactory for my purposes. Next week will be adding inputs, more look p tables, and possible other wave generators. Some further assembly optimizing may be needed. I may also have to bite the bullet and just make a gross resistor ladder to do my D-to-A conversions.
Here is the code:
/* Synth.ino
----------------------------------------------------------
Description:
A sketch to generate sound waves for audio processing
with an arduino Due.
State:
"in early developement" (AKA not functioning yet).
Something is slowing down the cycles to the point
where it just sounds like a speaker-pop. All
the coret values are beings processed, just not
quickly enough.
----------------------------------------------------------
References:
http://www.djerickson.com/arduino/
----------------------------------------------------------
Contact: ldavis@marlboro.edu
https://github.com/SafeCamp
https://safecamp.github.io/
----------------------------------------------------------
Logan Davis | 4/7/16 | Arduino 1.6.7
----------------------------------------------------------
*/
#define SQUARE 0
#define SAWTOOTH 1
const byte sawtooth[256] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78,
79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100,
101, 102, 103, 104, 105, 106, 107, 108, 109,
110, 111, 112, 113, 114, 115, 116, 117, 118,
119, 120, 121, 122, 123, 124, 125, 126, 127,
128, 129, 130, 131, 132, 133, 134, 135, 136,
137, 138, 139, 140, 141, 142, 143, 144, 145,
146, 147, 148, 149, 150, 151, 152, 153, 154,
155, 156, 157, 158, 159, 160, 161, 162, 163,
164, 165, 166, 167, 168, 169, 170, 171, 172,
173, 174, 175, 176, 177, 178, 179, 180, 181,
182, 183, 184, 185, 186, 187, 188, 189, 190,
191, 192, 193, 194, 195, 196, 197, 198, 199,
200, 201, 202, 203, 204, 205, 206, 207, 208,
209, 210, 211, 212, 213, 214, 215, 216, 217,
218, 219, 220, 221, 222, 223, 224, 225, 226,
227, 228, 229, 230, 231, 232, 233, 234, 235,
236, 237, 238, 239, 240, 241, 242, 243, 244,
245, 246, 247, 248, 249, 250, 251, 252, 253,
254, 255};
const byte square[256] = {255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0};
byte i = 0;
byte choice = 0;
byte current_wave[256];
void setup() {
analogWriteResolution(8);
memcpy(current_wave, square, 256);
REG_ADC_MR = (REG_ADC_MR & 0xFFF0FFFF) | 0x00020000;
}
void loop() {
analogWrite(DAC1, current_wave[i]);
i++;
}
void switchwave(byte choice){
switch(choice){
case SQUARE:
memcpy(current_wave, square, 256);
case SAWTOOTH:
memcpy(current_wave, sawtooth, 256);
}
}