Monday, 15 February 2016

Play sound on Raspi 2

Team:    QRM, LamPi
Author:  Qichao Zhao


Overview
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: http://www.pygame.org/install.html. When I tries to install pygame, I met some errors and it were fixed by the website https://www.raspberrypi.org/forums/viewtopic.php?f=32&t=33157&p=332140&hilit=croston%2bpygame#p284266




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
pygame.mixer.init()

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
pygame.mixer.stop()
   

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.
Source: https://www.raspberrypi.org/documentation/configuration/audio-config.md

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