Main Site Documentation

Playing MP3 songs form SD card


#1

I got one FEZ Music Shield http://www.ghielectronics.com/catalog/product/289 and the overview states: [italic]Few simple lines of code are plenty enough to play music from SD cards, USB memory stick or even stream audio over the network[/italic].

Can someone please supply me with a simple working example on how to play a normal 4Mb MP3 file from my FEZ Domino SD card?

:-[


#2

Geir, is this what you are looking for: http://code.tinyclr.com/project/54/mp3-shield/


#3

Not completely. The example only streams a small file that has been preloaded from resources. I need an example of reading a normal size MP3 file from an SD card.


#4

Do you know how to read files from SD? http://wiki.tinyclr.com/index.php?title=File_System

If so, all you need is this

while(file is not done)
{
ReadFromSD(…);
WriteTo MusicShield(…);
}


#5

This is what I have tried

    public class Program
    {
        static MusicShield musicShield = new MusicShield(SPI.SPI_module.SPI1, FEZ_Pin.Digital.An4, FEZ_Pin.Digital.An5, FEZ_Pin.Digital.Di4);
        public static void Main()
        {
            PersistentStorage sd = new PersistentStorage("SD");
            sd.MountFileSystem();

            while (true)
            {

                PlayStatement("1.mp3");

                Thread.Sleep(1000);
            }
        }

        public static void PlayStatement(string FileName)
        {
            int offset = 0;
            byte[] data = new byte[10 * 1024];
            musicShield.SetVolume(240, 240);

            FileStream fs = new FileStream("\\SD\\" + FileName , FileMode.Open, FileAccess.Read);
            fs.Seek(offset, SeekOrigin.Begin);

            int bytesRead = 0;
            bytesRead = fs.Read(data, 0, data.Length);

            while (bytesRead != 0)
            {
                musicShield.Play(data);
                while (musicShield.IsBusy) Thread.Sleep(10); 
                bytesRead = fs.Read(data, 0, data.Length); 
            }
            fs.Close();
            fs.Dispose();
            fs = null;
        }

    }

It plays the file… sort of. But there is a small glitch in the sound output when new data is read, so the quality is crap. How to fix this?


#6

did you tried using a smaller buffer?


#7

What is the bit-rate of the file you are playing? Lower bit-rate will make things a lot easier.

Also, if I remember correctly the internal buffer of the MP3 chip is 32bytes or something really small so reading 1K data maybe to long of a delay to the MP3 chip. Use smaller buffer like Eric suggested.


#8

Ok so I tried a lower bitrate 160kbps (the shield overview doesnt say anything about bitrate limitations). That did not help.

I tried with a buffersize from 16byte, 32byte, 512byte of 1k and got absoluttly nothing out of the speakers.
Tried with a buffer of 5k got some very copped up ugly sound.
With a 10k buffer Im able to hear what song it is, -but still crap (lots of chopping).


#9

I found an example that seems to work http://code.tinyclr.com/project/344/music-shield-extension-playing-streams/
But I cant figure out this line

musicShield.OnPlaybackStopped += new GHIElectronics.NETMF.FEZ.Shields.MusicShield.PlaybackStopped(musicShield_OnPlaybackStopped);

The compiler states: The name ‘musicShield_OnPlaybackStopped’ does not exist in the current context

Is there something missing in the example?


#10

Geir,

delete everything starting from +=, add again += and press tab twice.

Visual studio adds the missing code for you when you do the above.


#11

Thanks EriSan :slight_smile:


#12

Works now?


#13

Yes it works now.
Thanks for your help.