desu
desu

Reputation: 455

Arduino play midi files

I've assembled 8-bit DAC and connected it to my Arduino. To my DAC I connected speaker. And now I want to know how play midi files. I found a lot of info but practically all of them using some shields. The best I found was this. After reading it I copy-paste some code so it became

#include <avr/pgmspace.h>
byte sample[] PROGMEM = {/*midi here*/};
int sampleSize = (sizeof(sample)-1);
int nextdata;
int sampleNUM=0;//current index

ISR(TIMER2_COMPA_vect) {
    nextdata = 127;
    nextdata += (127-pgm_read_byte_near(sample+sampleNUM));
    if (nextdata > 255){nextdata = 255;}
    else if(nextdata < 0){nextdata=0;}
    PORTA = nextdata;
    if (sampleNUM == sampleSize){sampleNUM = 0;}
    else {sampleNUM += 1;}
}

void setup() {
    DDRA = 0xFF;
    cli();
    TCCR2A = 0;// set entire TCCR2A register to 0
    TCCR2B = 0;// same for TCCR2B
    OCR2A = 249;// = (1/44100) / ((1/(16*10^6))*8) - 1
    TCCR2B |= (1 << WGM12);
    TCCR2B |= (1 << CS11);   
    TIMSK2 |= (1 << OCIE2A);
    sei();//allow interrupts
}
void loop() {}

I've tried to convert music using apps from tutorial or even using that samples from tutorial but it generates only white noise

Upvotes: 1

Views: 766

Answers (1)

Florian
Florian

Reputation: 378

I expect your DAC cannot drive a normal speaker load. (without any amplification)

Further, you need a basic software-synthesizer (as @CL stated) - though I disagree, Arduino is able to handle that. Although, you don't really need a DAC.

Just use a low pass filtered pwm signal (you definetly need amplification here - a single transistor can do the trick), where the pwm base frequency has to be >2 times higher than the frequency of the tone you want to produce. The speaker itsself (mechanically, and electronically) may already be a sufficient filter, if the pwm base frequency is high

Upvotes: 0

Related Questions