Page 1 of 1

How to play a sound (music)? Not ogg or mp3 s!

Posted: Sun Jun 30, 2013 9:07 am
by cspxo
I want to do some very simple programming probably in Python. I want to play a list of musical notes in the form of note name, duration eg. A, whole beat; G, quarter etc. Or some numerical representation eg. 440Hz, 50ms ...
I am sure most BASIC interpreters had this feature back in the day. I presume I need an external module? I thought finding this information would have been straight forward but all I can find are endless mp3 players or something overly complex and overkill like Mingus but I don't know anything about the later so I'm not passing judgement.
Any thoughts anyone?

Re: How to play a sound (music)? Not ogg or mp3 s!

Posted: Sun Jun 30, 2013 11:31 am
by toxibunny
It's a good question, but it's not something I've really looked into too deeply. The Pi doesn't have any hardware sound generators, so for musical results, you'll have to use either some sort of softsynth or a bank of samples.

IMHO, using samples would be easiest - you could just put a load of samples in a list or dictionary, and then play them when needed. I know pygame could do the playback side fairly easily... As for getting a full range of samples, you could try googling for 'sample pack' or 'midi wav sample pack' or possibly 'wav soundfont' or something like that.

There are a few python libraries for soft synth type stuff, but they look pretty complicated and are probably quite resource-heavy...

Let us know how you get on, anyway.. :)

Re: How to play a sound (music)? Not ogg or mp3 s!

Posted: Wed Jul 03, 2013 9:01 am
by micahcowan
http://askubuntu.com/questions/202355/h ... ing-python

In the example on this page, you'd need to "sudo apt-get install python-pyaudio". It gives a very, very simple program to generate a 1KHz tone for five seconds. You could perhaps turn it into a function to use for your purposes. When I run it (on my laptop; I didn't test it on a R-Pi), I get a number of harmless informational warnings, but it works (provided I have pyaudio installed).

The WAVE value is being used in this program to indicate the frequency that should be played. The big ugly line that uses Math.sin generates a buffer filled with one second's worth of an 8-bit sine-wave PCM tone at the frequency given in WAVE, played at a sampling rate of RATE (16000).

Not really a great example of Python coding, there, but it may be enough to give you a start.

Re: How to play a sound (music)? Not ogg or mp3 s!

Posted: Wed Jul 03, 2013 5:17 pm
by cspxo
Thanks toxibunny & micahcowan for your replies.

As pointed out, and of course as I was forgetting, the BEEB Mod.B had the TI SN76489 (and the CBM64 had it's SID). No SOUND or ENVELOPE commands for the Pi :(

.wav samples are fun to play with but not what I originally had in mind. The PyAudio method is somewhat unwieldy and I doubt my humble talents stretch to writing functions to reproduce the 'sounds of the Eighties' like I remember.

I will continue to experiment. I wonder if anyone fancies putting out an actual, physical, hardware sound chip add-on?

Re: How to play a sound (music)? Not ogg or mp3 s!

Posted: Wed Jul 03, 2013 6:20 pm
by PiGraham
pygame.midi, maybe?

Re: How to play a sound (music)? Not ogg or mp3 s!

Posted: Wed Jul 03, 2013 11:19 pm
by Raspberry wino
The RPi does have an audio output, where does the signal come from? Does it mean there is a simple D/A on board? Can it be controlled?

Re: How to play a sound (music)? Not ogg or mp3 s!

Posted: Thu Jul 04, 2013 12:25 am
by jackokring
The mini jack or HDMI have the sound. Yes it can be controlled, but due to kernel protection modes, the alsa driver is used. You have to access alsa. The "Advanced Linux Sound Architecture (ALSA)" plays samples. Dynamic sound generation is very possible, but not a noob thing (needs or helps if in C).

Yes a SID-o-matic apt-get would be nice.

EDIT: I think the default makes 8 alsa (should be ALSA as it's an acronym), ports in the boot sequence. Nothing in the boot sequence is printed for fun. This does not mean 8 channel polyphonic, it means 8 apps can sound at once. Polyphony is a matter of app choice. A simplistic app may use all 8 ALSA ports.

Re: How to play a sound (music)? Not ogg or mp3 s!

Posted: Tue Sep 24, 2013 11:45 am
by cspxo
Um, yeah ... this > http://www.cl.cam.ac.uk/projects/raspbe ... index.html

What I said, well not exactly but close! Seriously, what a fabulous piece of work (I knew someone, somewhere would be doing this).

All credit and thanks to the creators of Sonic Pi, I wish them and their pupils the best and much enjoyment of this super tool. Now I'm off to experiment with it as well.