简体   繁体   English

Arduino 播放 MIDI 文件

[英]Arduino play midi files

I've assembled 8-bit DAC and connected it to my Arduino.我已经组装了 8 位 DAC 并将其连接到我的 Arduino。 To my DAC I connected speaker.我连接到我的 DAC 扬声器。 And now I want to know how play midi files.现在我想知道如何播放midi文件。 I found a lot of info but practically all of them using some shields.我找到了很多信息,但几乎所有信息都使用了一些盾牌。 The best I found wasthis .我发现的最好的是这个 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我尝试使用教程中的应用程序或什至使用教程中的示例来转换音乐,但它只产生白噪声

I expect your DAC cannot drive a normal speaker load.我希望您的 DAC 无法驱动正常的扬声器负载。 (without any amplification) (无任何放大)

Further, you need a basic software-synthesizer (as @CL stated) - though I disagree, Arduino is able to handle that.此外,您需要一个基本的软件合成器(如@CL 所述)-尽管我不同意,但 Arduino 能够处理。 Although, you don't really need a DAC.虽然,您实际上并不需要 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.只需使用低通滤波 pwm 信号(您在这里肯定需要放大 - 单个晶体管可以解决问题),其中 pwm 基频必须比您想要产生的音调频率高 2 倍以上。 The speaker itsself (mechanically, and electronically) may already be a sufficient filter, if the pwm base frequency is high如果 pwm 基频很高,扬声器本身(机械和电子)可能已经是一个足够的滤波器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM