Monday, 15 February 2016

Play sound on Raspi 2

Team:    QRM, LamPi
Author:  Qichao Zhao

At the first three weeks, I was focus on how to play sound on Raspi 2. Pygame is a set of Python modules designed for writing games. This allows you to create fully featured games and multimedia programs in the python language. I just wrote the basic code and make sure codes could work.

The website about pygame installation guide here: When I tries to install pygame, I met some errors and it were fixed by the website

import pygame.mixer
from pygame.mixer import Sound

# different SER.Setup corresponding to different servo 
SER.Setup(1, SER.IN)
SER.Setup(2, SER.IN)
SER.Setup(3, SER.IN)
# initialize the mixer module

while True:

if (SER1 == True):
 SER = Sound("sound1.wav")
#prevent servo need some delay
sleep (0.1);  

if (SER2 == True):
 SER = Sound("sound2.wav")
#prevent servo need some delay
sleep (0.1);  

if (SER3 == True):
 SER = Sound("sound3.wav")
#prevent servo need some delay
sleep (0.1);  
# stop playback of all sound channels

When I first time to test it, there were no sounds here. Then I found The Raspberry Pi has two audio output modes which is HDMI and headphone jack. we can switch between these modes by amixer cest numid =3 1 it was work but some noise here. The pygame cannot play mp3 file, so I have to download wav sound file instead of mp3 file.
When I turned up volume of sound, it was no noise. It is good topic for further work.

During the last three weeks, I learned some basic operation for Raspi 2. I was very glad to cooperated and talked with team members. They were very friendly.

In the following weeks, I am plan to Sounds effects to play over the movements, Threaded class with play() and stop() method and Extend threaded class so it can modulate sound depending on servo movement distance.

No comments:

Post a Comment