cspxo
Posts: 14
Joined: Mon Dec 31, 2012 6:54 pm

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

Sun Jun 30, 2013 9:07 am

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?

toxibunny
Posts: 1382
Joined: Thu Aug 18, 2011 9:21 pm

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

Sun Jun 30, 2013 11:31 am

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.. :)
note: I may or may not know what I'm talking about...

micahcowan
Posts: 3
Joined: Wed Jul 03, 2013 7:18 am

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

Wed Jul 03, 2013 9:01 am

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.

cspxo
Posts: 14
Joined: Mon Dec 31, 2012 6:54 pm

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

Wed Jul 03, 2013 5:17 pm

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?

PiGraham
Posts: 3926
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

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

Wed Jul 03, 2013 6:20 pm

pygame.midi, maybe?

Raspberry wino
Posts: 49
Joined: Mon Mar 05, 2012 11:48 am

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

Wed Jul 03, 2013 11:19 pm

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?

User avatar
jackokring
Posts: 816
Joined: Tue Jul 31, 2012 8:27 am
Location: London, UK
Contact: ICQ

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

Thu Jul 04, 2013 12:25 am

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.
Pi[NFA]=B256R0USB CL4SD8GB Raspbian Stock.
Pi[Work]=A+256 CL4SD8GB Raspbian Stock.
My favourite constant 1.65056745028

cspxo
Posts: 14
Joined: Mon Dec 31, 2012 6:54 pm

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

Tue Sep 24, 2013 11:45 am

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.

Return to “General programming discussion”