Code sound on Ringo

Hi, Can someone please tell me how to code music and sound for Ringo?

-Daniel

Hey Daniel!

What do you mean exactly by ‘coding music and sound for Ringo’?

I’d be happy to help if you could specify it a bit more. :smiley:

Cheers,
Robert

Hello, So I am trying to code a game for my Ringo, and I noticed that there is no way in circuitblocks to code music or sound effects and right now I want to code some theme music.

-Thanks

Record your sound effect or theme music to a .wav file (16-bit, 44100 Hz, mono) and put the file on the SD card. Then you can use the sound library to play the file. Here is a very simple sketch that plays a .wav file over and over again:

#include <MAKERphone.h>

// Shared variables
MAKERphone mp;
MPTrack sound("");

void setup()
{
   mp.begin(1);
   sound.setRepeat(0);
   sound.setVolume(300);
   sound.reloadFile("/mysound.wav");
   addTrack(&sound);
}

void loop()
{
   mp.update();
   if(!sound.isPlaying())sound.play();
}
1 Like

Cool! Thanks a lot :grinning:

Hey,

Yeah, the sound option is not added in CB because it’s a bit too complex to do with blocks and some of the stuff didn’t work really well like we wanted them to.

You can also take a look at our GitHub page here and check out how is the sound done in some other games. :slight_smile:

Cheers,
Robert

Hey @Blaze,

I discussed some lessons learned while programming RingoSki in this thread which may be of some use to you:

https://community.circuitmess.com/t/ringoski-a-skiing-game-for-ringo/3396

Sound was one of the topics discussed, here is a quick recap:

SD Card I/O and Sound FX
Care must be taken when reading/writing to/from the SD card. The current MAKERphone library only allows you to have 4 MPTracks loaded at any given point in time.

I found out the hard way that reading/writing files to the SD card (for instance, when saving or reading a high score) also shares one of these 4 “slots” with MPTrack. By observing the serial output, I noticed the following error when this condition happened: vfs_fat: open: no free file descriptors. I was able to solve this problem by unloading all sounds whenever I needed to save or read the high score, followed by reloading the sounds after the file operation completed.

initSounds() function:

void initSounds()
{
  // Load menu sounds if at the menu
  if(activeLoop == MENULOOP)
  {
    sndMenuMusic = new MPTrack("/RingoSki/menu.wav");
    sndMenuMusic->setVolume(map(mp.mediaVolume, 0, 14, 100, 300));
    addTrack(sndMenuMusic);

    sndStart = new MPTrack("/RingoSki/start.wav");
    sndStart->setVolume(map(mp.mediaVolume, 0, 14, 100, 300));
    addTrack(sndStart);
  }
  else // Otherwise load normal game sounds
  {
    sndJump = new MPTrack("/RingoSki/jump.wav");
    sndJump->setVolume(map(mp.mediaVolume, 0, 14, 100, 300));
    addTrack(sndJump);

    sndSlide = new MPTrack("/RingoSki/speed.wav");
    sndSlide->setVolume(map(mp.mediaVolume, 0, 14, 100, 300));
    addTrack(sndSlide);

    sndCrash = new MPTrack("/RingoSki/crash.wav");
    sndCrash->setVolume(map(mp.mediaVolume, 0, 14, 100, 300));
    addTrack(sndCrash);

    sndGameOver = new MPTrack("/RingoSki/gameover.wav");
    sndGameOver->setVolume(map(mp.mediaVolume, 0, 14, 100, 300));
    addTrack(sndGameOver);
  }
}

reloadSounds() function:

void reloadSounds()
{
  closeSounds();
  initSounds();
}

closeSounds() function:

void closeSounds()
{
  for(int i = 0; i < 4; i++)
  {
    if(tracks[i] != NULL)
    {
      removeTrack(tracks[i]);
    }
  }
}

Another strange behavior I encountered with the sound FX is that sometimes they get randomly unloaded. If you try to play an unloaded MPTrack, your Ringo will crash. I deduced that this probably has something to do with what is happening in mp.update(), such as accessing the main menu, power save mode activating, an incoming call, etc. trying to use one of the 4 slots. I made a “safe” playSound() function to solve this problem:

void playSound(MPTrack *snd)
{
  // safety check (sometimes things go wrong with sounds loaded from SD card)
  if(snd == NULL || snd->getSize() <= 0)
  {
    reloadSounds();
  }

  snd->rewind();
  snd->play();
}

Oh and one last thing about sound FX: Ringo is very picky regarding what type of .wav file it will play. Keep them short in duration, otherwise they will playback very slowly. Also, it appears that the bitdepth must be 16 or less, higher bitdepths result in static.

Update from @robertCM regarding .wav properties: The ideal .wav is 16-bit , 44100 Hz, and mono for Ringo.

I hope this helps, if you have any more questions feel free to ask :slight_smile:

Thanks that is super helpful.