Produce music from PIC

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

Re: Produce music from PIC

Postby yonghui » Mon Jan 24, 2011 12:23 am

hi,

PWM of pic is good DAC, there is possibility for voice playing too with not too high sampling rate with PIC. and microchip also provide some library for audio sampling and playback with dspic and pic32.
for voice playback it will need analog amplifier. harder compare to digital audio tones, which can be amplified by single transistor.


regards,
yh
thanks&regards,
yh
yonghui
Professional
 
Posts: 732
Joined: Mon Sep 28, 2009 3:27 pm

Re: Produce music from PIC

Postby ABSF » Tue Jan 25, 2011 6:07 am

yonghui WROTE:hi,
PWM of pic is good DAC, there is possibility for voice playing too with not too high sampling rate with PIC. and microchip also provide some library for audio sampling and playback with dspic and pic32.
for voice playback it will need analog amplifier. harder compare to digital audio tones, which can be amplified by single transistor.

regards,
yh


I connot comment much before I tried out the program. For better volume I can always connect a LM386 to it. I have music program from other micros, one of them is a dual-voice music without using any music synthesizers and sounds very nice. I just want to compare the sounds from both and see if I can translate the dual-voice program to PIC assembly.

As for voice playback, can you also record the voice sample on the PIC. Or you have to get the wave files from PC? I just need short pharses like "Good Morning" or "Happy New Year" from the PIC and I think that would be cool. Some cars also have this built in when you open the door or start the engine. Wondering how they did it.

Allen

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: Produce music from PIC

Postby Brian Griffin » Tue Jan 25, 2011 10:01 am

There are many methods of producing music on a microcontroller.

One of them is the classic, interrupt-based square wave.

If you need a 'chime' effect, some program modifications must be done, and a RC system at the output.

Or else, you might need a DDS (digital direct synthesis), but for 8-bit microcontrollers, assembly must be used instead to check on the timings. For polyphonic, multi-channel DDS can be used. Just take note that DDS is very CPU intensive and a higher end microcontroller must be used.

As for voice playback, can you also record the voice sample on the PIC. Or you have to get the wave files from PC? I just need short pharses like "Good Morning" or "Happy New Year" from the PIC and I think that would be cool. Some cars also have this built in when you open the door or start the engine. Wondering how they did it.


Playing a sampled sound is more straightforward - if you want to access the SD-Card, get the SD-card library from Microchip, or using the onboard MikroC library. The file format is a *.wav because it is uncompressed. Once the *.wav data is obtained (as a start, use mono, 8khz and 8bit), each byte is fed into a circular buffer and then sent to the PWM register or a DAC. Make sure the PWM frequency is much higher than the sampling rate.
PIC - UIC00B from Cytron (replacement for my broken PICKit 2), Pickit 3, MikroC for PIC
dsPIC - MikroC for dsPIC, mikromedia board (dsPIC33)
AVR - AVR Dragon
Parallax - Prop tool
User avatar
Brian Griffin
Enthusiast
 
Posts: 403
Joined: Mon Jan 17, 2011 9:36 am

Re: Produce music from PIC

Postby ABSF » Wed Jan 26, 2011 9:20 am

Or else, you might need a DDS (digital direct synthesis), but for 8-bit microcontrollers, assembly must be used instead to check on the timings. For polyphonic, multi-channel DDS can be used. Just take note that DDS is very CPU intensive and a higher end microcontroller must be used.


Thanks Brian, DDS is a new term for me. I did google for it but I didn't read them fully cause there are so much infomations. Especially multi-channel DDS for polyphonic sounds. Which PIC do you recommend for doing multi-channel DDS? I thought the handphones are using ARM chips as one of my English friends told me. That's why the handphones have plenty of bandwidth to do so many things and still be able to play beautiful sounds. I will take a closer look at the multi-channel DDS after the CNY.

Playing a sampled sound is more straightforward - if you want to access the SD-Card, get the SD-card library from Microchip, or using the onboard MikroC library. The file format is a *.wav because it is uncompressed. Once the *.wav data is obtained (as a start, use mono, 8khz and 8bit), each byte is fed into a circular buffer and then sent to the PWM register or a DAC. Make sure the PWM frequency is much higher than the sampling rate.


Now you mentioned SD card makes me remember that PIC only has few hundred bytes of RAM and EEprom. So to play sound files it looks like adding a SD card is a neccessity. But cant we just grab a 32K PIC (16K words) and store the sound in the data statements like DTI (arrays of retlw). Or was it too slow? And how about the file system? Can the PIC read FAT formated files in the SD cards without teaching it filing system of PC DOS?

I will take a look at the Mikro C compiler and its library and try out some of the C sound programs. I have used PICBasic Pro and I know its sound commands are easy to use. I'll learn them step by step but only after the CNY cause I have a lot of repair works waiting for me to finish. The Laney Bass Guitar amplifiers are giving me a real headache.

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: Produce music from PIC

Postby Brian Griffin » Wed Jan 26, 2011 10:17 am

ABSF WROTE:Thanks Brian, DDS is a new term for me. I did google for it but I didn't read them fully cause there are so much infomations. Especially multi-channel DDS for polyphonic sounds. Which PIC do you recommend for doing multi-channel DDS? I thought the handphones are using ARM chips as one of my English friends told me. That's why the handphones have plenty of bandwidth to do so many things and still be able to play beautiful sounds. I will take a closer look at the multi-channel DDS after the CNY.


You can use a casual PIC16F microcontroller for all that, but if you need to simulate the decay effect, you might need some very tricky fast multiply routines, or use a PIC18F for hardware multiply. The decay effect is an e^-x, so you need to convert them into a table of fixed point values, and then multiply it to a wavetable (example, sine, squarewave or trianglewave). If you need to write DDS algorithm in C (I suppose it's easier!), try the dsPIC as it has mul/div instructions.

Elm-chan http://elm-chan.org/works/mxb/report.html did a little music box using multichannel DDS using AVR. However, it is written entirely in assembly because he/she had to do it precisely, as a few cycles are lost during the interrupt routine and the exact frequency must be calculated.

The handphones might not need to do all the DDS in software because they have already a built-in codec, I guess. Like the iPhone, the processor contains audio/video codecs which simplify development process.

ABSF WROTE:Now you mentioned SD card makes me remember that PIC only has few hundred bytes of RAM and EEprom. So to play sound files it looks like adding a SD card is a neccessity. But cant we just grab a 32K PIC (16K words) and store the sound in the data statements like DTI (arrays of retlw). Or was it too slow? And how about the file system? Can the PIC read FAT formated files in the SD cards without teaching it filing system of PC DOS?

I will take a look at the Mikro C compiler and its library and try out some of the C sound programs. I have used PICBasic Pro and I know its sound commands are easy to use. I'll learn them step by step but only after the CNY cause I have a lot of repair works waiting for me to finish. The Laney Bass Guitar amplifiers are giving me a real headache.

Allen


You can use a 32k or a 64k PIC for all that, but a short uncompressed WAV file is very huge and it is not encouraged to do so. The file system drivers (FAT16/32) in the microcontroller is already chewing a lot of memory also. And wait, you need like 1.5k above RAM for playing wav files. Reserve a 512 bytes buffer for it.

The MikroC sound libraries are not much - it's just a square wave and all the beeps.

To answer the question "Can the PIC read FAT formated files in the SD cards without teaching it filing system of PC DOS?", you need to write the algorithm for that, or for more lesser hassles, use the Microchip's FAT16/32 libraries or the onboard MikroC. :D
PIC - UIC00B from Cytron (replacement for my broken PICKit 2), Pickit 3, MikroC for PIC
dsPIC - MikroC for dsPIC, mikromedia board (dsPIC33)
AVR - AVR Dragon
Parallax - Prop tool
User avatar
Brian Griffin
Enthusiast
 
Posts: 403
Joined: Mon Jan 17, 2011 9:36 am

Re: Produce music from PIC

Postby ABSF » Tue Feb 08, 2011 5:31 pm

Elm-chan http://elm-chan.org/works/mxb/report.html did a little music box using multichannel DDS using AVR. However, it is written entirely in assembly because he/she had to do it precisely, as a few cycles are lost during the interrupt routine and the exact frequency must be calculated.


The sound from the ATtiny45 is terrific. Couldnt believe an DIP8 chip can do that. I must try it out some day.

You can use a 32k or a 64k PIC for all that, but a short uncompressed WAV file is very huge and it is not encouraged to do so. The file system drivers (FAT16/32) in the microcontroller is already chewing a lot of memory also. And wait, you need like 1.5k above RAM for playing wav files. Reserve a 512 bytes buffer for it.

The MikroC sound libraries are not much - it's just a square wave and all the beeps.

To answer the question "Can the PIC read FAT formated files in the SD cards without teaching it filing system of PC DOS?", you need to write the algorithm for that, or for more lesser hassles, use the Microchip's FAT16/32 libraries or the onboard MikroC.


I took a look at AN1045 and looks like FAT16 can only be implemented on PIC18F and above with 16 bit architecture. How do you read the SD card? Using the Pigtail daughter card to SPI converter or connect SD directly to you 18F ports ? What is the biggest SD can it support? How many wires from SD cards are you interfacing besides SDO, SDI, SCK and /CS ?

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: Produce music from PIC

Postby ABSF » Tue Feb 08, 2011 6:20 pm

Brian Griffin WROTE:
To answer the question "Can the PIC read FAT formated files in the SD cards without teaching it filing system of PC DOS?", you need to write the algorithm for that, or for more lesser hassles, use the Microchip's FAT16/32 libraries or the onboard MikroC. :D


I just found another FAT file system that can be implemented on 8 bit family PIC. I guess you already knew it as it is from the ELM-chan website where you show me the ATtiny music:

http://elm-chan.org/fsw/ff/00index_p.html
http://www.edaboard.com/thread75721.html

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: Produce music from PIC

Postby Brian Griffin » Tue Feb 08, 2011 8:53 pm

ABSF WROTE:
Brian Griffin WROTE:
To answer the question "Can the PIC read FAT formated files in the SD cards without teaching it filing system of PC DOS?", you need to write the algorithm for that, or for more lesser hassles, use the Microchip's FAT16/32 libraries or the onboard MikroC. :D


I just found another FAT file system that can be implemented on 8 bit family PIC. I guess you already knew it as it is from the ELM-chan website where you show me the ATtiny music:

http://elm-chan.org/fsw/ff/00index_p.html
http://www.edaboard.com/thread75721.html

Allen


I've seen that too. It's a simple and nifty piece of code. MikroC and CCS provides FAT libraries, but please take note that the code can be very very large. You need a large program space and RAM here.

Parallax's Propeller (another kind of microcontroller) has complete FAT16/32 libraries in the OBEX (library of programs for Propeller) which can provide access to SD card without breaking a sweat.
PIC - UIC00B from Cytron (replacement for my broken PICKit 2), Pickit 3, MikroC for PIC
dsPIC - MikroC for dsPIC, mikromedia board (dsPIC33)
AVR - AVR Dragon
Parallax - Prop tool
User avatar
Brian Griffin
Enthusiast
 
Posts: 403
Joined: Mon Jan 17, 2011 9:36 am

Re: Produce music from PIC

Postby lockman_akim » Sat Feb 12, 2011 11:44 am

you can produce sound by using PWM..generate a pwm and connect it to the buzzer..play around with the duty cycle..and you will get variuos of tone..
lockman_akim
Novice
 
Posts: 22
Joined: Sun Aug 29, 2010 11:50 pm

Previous

Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 6 guests

cron