My high school electronics class decided to buy some arduino uno kits, which I must say are extremely cool. Enough about that, right now in the class we're experimenting with the piezo buzzer (it looks like this). We learned about creating songs using the piezo buzzer. Our teacher told us to be "creative". What better way to be creative than to use "Firework" by Katy Perry.
Using some creative liberties, I found a nice piano piece of this song (link here). Now I'm a piano player (I took AP Music theory), and the problem I'm having is that I'm only able to play one note only the piezo buzzer. Is it possible to play the song on a piezo buzzer so it sounds like it's being played on a piano (or at least close to). I mean like the bass and treble clef notes are played simultaneously on the buzzer.
I understand that it involves phase shifts and adding frequencies of notes, but how do you translate this into code for a piezo buzzer? If you could post some example code that would be greatly appreciated. If not, could you explain it in the clearest way possible. I'm not a master at programming, but I'm not a beginner either.
Say you want a 100Hz tone. 100Hz means the output repeats every 1/100 of a second, or 10ms. So tone(PIN,100) will set a timer interrupt to be called every 5ms. The first time the interrupt is called, it sets the output low, and returns to whatever your program was doing. Next time it's called it sets the output high. Thus every 5ms the output changes, and you get a square wave at 50% duty cycle, which means the output is on for precisely half the time.
This is all very well, but most audio waveforms are not square waves. If you want to play two square wave tones simultaneously, or even to control the volume of a single square wave tone, you need to be able to output more values than just "on" and "off".
The good news is that there's a trick you can use called pulse width modulation (commonly abbreviated PWM). The idea is that you may only be able to set your output to one of two values, but you can do so really fast. Humans can hear audio frequencies up to about 20kHz. If you diddle your output faster than that, say at 200kHz (well within the capabilities of the Arduino, which is clocked at 16MHz), you don't hear the individual output transitions, but the average value over a longer period.
Imagine generating a 200kHz tone with tone(). It's way too high to hear, but the average value is halfway between on and off (50% duty cycle, remember?). So we now have three possible output values: on, off, and halfway. This is enough to allow us to play two square waves simultaneously:
High-quality audio requires many more values than this. CDs store 16-bit audio, which means there are 65536 possible values. And while we will not get CD-quality audio out of an Arduino, we can get more output values by choosing a duty cycle other than 50%. In fact, the Arduino has hardware to to this for us.
Meet analogWrite(). This fakes varying output levels using the Arduino's built-in PWM hardware. The bad news is that the PWM frequency is typically 500Hz, which is fine for dimming an LED but way too low for audio. So we have to program hardware registers ourself.
Of course, once you've got PWM output working the fun is just beginning.With multiple voices you can't rely on interrupts to set the frequency your waveforms, you have to stretch them yourself. Knowledge of basic digital signal processing (DSP) will come in jolly handy. You'll need tight code to generate audio data from within an interrupt handler, and then you'll need a playroutine to trigger the right notes at the right time. The sky's the limit!
This plays a four-voice tune. I have only an Arduino Leonardo (well, Pro Micro) to test it on, so you may need to change PIN according to which pin is hooked up to TIMER1A (if I'm reading correctly it's pin 9 on an Uno and pin 11 on a Mega). You don't get a choice of which pin to use, sadly.
Hopefully it gives you some idea of the possiblities, and a potential starting point for your own tune. Happy to explain anything that's unclear, and also thankyou for giving me an excuse to write this :)
I also have the exact same issue on my cometlake i9-10900T with UHD630. Audio only after both HDMI and DP are connected. It doesn't work if I connect DP after HDMI. Audio is detected only if both are connected at boot or if booted with DP and HDMI connected later. If booted with HDMI and then DP connected, no sound.
Thanks for pointing this out. I had followed all the details in this guide to get my HDMI display working. But only display got to work and unfortunately no HDMI audio. If you have both DP and HDMI connected then it works.
I have a Gigabyte Z490M Gaming X with ALC1200, a 10700K processor with integrated UHD 630 and OpenCore as boot loader. I was in the similar situation: to not be able to get Audio working via DP connection without plugging the HDMI cable too.
The downside is that I don't get my hack working only via HDMI cable, but is not important for me. If only HDMI cable is connected I don't have image on the screen. But, if the DP cable is plugged, the HDMI video is working too... So 2 monitors can be used, but the one connected to DP should be the main one.
My motherboard is MSI Z490 carboon. Recently, I sold my RX590, using a 10700K core display. There is a very strange problem when using HDMI and DP dual screens. Dell U2718Q uses HMDI and DP alone sometimes to have audio output, sometimes not. AOC U277B is connected to HMDI and DP 100% audio output separately. However, AOC U277B connects to HDMI, Dell U2718Q connects to DP 100% audio output. Is this related to the display? Or is it another reason?
795a8134c1