How to use PWM play simply music?

Go To Last Post
20 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I want to write a C code and ask AVR play simply music,like Do,Re, Mi,Fa, So, La,Si, DO1....
I know it can be implemented by setting PWM frequency,however, I have no idea how to write the code. Is there anyone could help me write a simply code example? Thank you very much.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If the music is to be run from the AVR memory, I recommend starting with a lookup table containing a sine wave. This can be any frequency, but ideally it would be one of your do re mi sounds.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There's some simple "music" code in this project of mine.

Chuck Baird

"I wish I were dumber so I could be more certain about my opinions. It looks fun." -- Scott Adams

http://www.cbaird.org

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The frequencies of any two adjacent notes are related by a constant multiplier which is 1.05946309.

Ozhan KD
Knowledge is POWER

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

zbaird wrote:
There's some simple "music" code in this project of mine.

Sorry, Zbaird, I cannot open the link you provide, could you check it again?
Cheers

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you, Mogor, I love the code you offer me, but I am writing program with ICCAVR, so it is quite different from WINAVR, could you provide me the code of ICCAVR? Cheers

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quite different? Oh please.

Most likely there is nothing else WINAVR specific except the two include files. Just copy-paste and it should work.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm surprised this thread got to here without mentioning AVR335. The code with it is for IAR but was easily used with WinAVR so I've no reason to doubt that it would be just as easy to make it work with ICC.

Apart from anything else the AVR335 app note not only provides explained code but more importantly it explains the theory behind playing PCM samples with PWM.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you look up the topic:

Musical AVR - Melodies and 'stylophone' synth

There's some example code that plays tunes.

<º))))><

I am only one lab accident away from becoming a super villain.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

"quite different"?

ISR syntax
sei/cli method
data in flash support
data in EEPROM support
delay routines
sleep mode support
fuse/lock bit support in code (not in ICC)
power reduction support
inline Asm syntax
link to external Asm method
atomicity support

and other things I've likely forgotten.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Sorry, Zbaird, I cannot open the link you provide, could you check it again?

Works for me, clear to the download.

Chuck Baird

"I wish I were dumber so I could be more certain about my opinions. It looks fun." -- Scott Adams

http://www.cbaird.org

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ChaN has several projects that utilize PCM for audio generation.

Simple Sound Generator - gcc
Portable PCM player - gcc
Wavetable Melody Generator - assembler

Stan

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Quote:

"quite different"?

ISR syntax
sei/cli method
[...]

Cliff!

In general yes, but for the code at hand, linked to by 'mogor' there seems to be very little work to do to port it:
- It does not use interrupts
- It does not have data in flash (PROGMEM)
- IIRC there is not much difference between the avr-gcc and ICCAVR delay functions
etc..

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you are interested in making music, then you need a MIDI synthesizer. PWM doesn't make 'music', it makes audio square-wave tones at specific frequencies. Sine wave generators just make soft beeps at specifically programmed frequencies while using a lot of system cycles.

On eBay, MIDI synthesizer prices are rising about 20% a year for the same models, especially older cheaper synths. But you can still get a starter synth like the Yamaha FB01 for about $20-$30 or the EMu Proteus for $30-$50. Plastic keyboards with full sized keys and non-programmable MIDI-controlled voices are selling in the $20 - $50 range also.

Remember, you can always resell whatever you buy on eBay for about the same price that you paid for it. So anything that you buy is like a free instrument rental service with the only charge being the shipping cost and the eBay/PayPal charge. Shipping on full keyboards can be steep, but MIDI tone modules cost only about $15 to ship across the USA.

AVRs are useful for making custom MIDI controllers rather than being audio tone generators by themselves.

One area of music generation that has received no exploration is the synthesis of distorted guitar sounds using complex PWM. The signal that comes from a guitar fuzz box is nearly identical to that coming from a serial port. Fuzz boxes greatly amplifly the normal guitar signal and then clip the signal, making it into square waves. The analog comparator of the AVR could be used to make super 'Jimi Hendrix'-style distortion box by feeding the guitar into one channel and setting the other channel to be 50-100 millivolts above the no-signal guitar reference point.
Then timers could be used to measure the lengths of each high and low section of the comparator output and then multiplying and dividing the signal while mixing it with the original. A BOSS distortion feedbacker could be inplemented using software on a $1 AVR.
Lots of areas to explore there.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Simonetta wrote:
PWM doesn't make 'music', it makes audio square-wave tones at specific frequencies.

Have you listened to the example Fur Elise: mg.mpg provided by ChaN in his
Wavetable Melody Generator project? He uses high speed counter PWM available
in the ATtiny's to combine a melody lookup table, attack/sustain/envelope lookup
tables with a software multiplier to produce multi-voice music box quality sound.

It's quite a bit better than the square wave squawking that you describe
PWM as being limited to.

Stan

Edit - Corrected mysterious link change for file mg.mpg

Last Edited: Thu. Aug 4, 2011 - 05:21 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you, every one, I succeed in playing simply music, just one question, could anyone provide any GCC of "AVR plays the songs in SD card" ?
Thank you very much.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

pcfarmer wrote:
could anyone provide any GCC of "AVR plays the songs in SD card" ?

You haven't examined my first two links to ChaN's projects?
They both read audio files from MicroSD cards.

Stan

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

In general yes, but for the code at hand, linked to by 'mogor' there seems to be very little work to do to port it:
- It does not use interrupts
- It does not have data in flash (PROGMEM)
- IIRC there is not much difference between the avr-gcc and ICCAVR delay functions
etc..

Johan it was the general case I was talking about. I haven't even looked at the code to be ported in this case.

As for delay. I believe one is delay_ms() and the other I know is _delay_ms(), note the leading underscore.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The sound it works on my ATmega8535 :D

 

it's been a long time ago since those refference available. Oh i really the real beginner here.
 

 

Thanks Mogor.

Thanks all.

I like logic and such a programming :)