Difference between revisions of "Playing multiple sounds at once"

From ESE205 Wiki
Jump to: navigation, search
(Pygame with Channels)
(Libraries tested)
Line 16: Line 16:
 
*multiprocessing
 
*multiprocessing
 
*multithreading
 
*multithreading
*swmixer
+
*pygame
*pygame  
 
  
 
= Pydub and Pygame=  
 
= Pydub and Pygame=  

Revision as of 09:23, 26 April 2019


Introduction

This tutorial will explain different methods attempted in getting multiple sounds to play concurrently using python.

Steps

  • Pick wav files desired to be played.
  • Load wav files in code and save it to a variable that can be called later.
  • Find a line of code that will play the sound.
  • Find a line of code that will allow the sound to be played while different notes are being played at the same time.

Libraries tested

  • pydub
  • multiprocessing
  • multithreading
  • pygame

Pydub and Pygame

These two libraries were used in finding the best way to play a sound by itself.

Using pydub:

from pydub import AudioSegment
from pydub.playback import play
sound = AudioSegment.from_wav('/home/pi/laserharp-sounds/samples/harpSound1.wav')
play(sound)


Using pygame:

import pygame
pygame.init()
pygame.mixer.init()
firstSound = pygame.mixer.music('/home/pi/laserharp-sounds/samples/ambi_dark.wav')
secondSound = pygame.mixer.music('/home/pi/laserharp-sounds/samples/ambi_choir.wav')
firstSound.play()
secondSound.play()

pygame.mixer.music is often used to produce sounds that are higher in quality. However, when trying to play multiple sounds at once, it is best to use pygame.mixer.Sound instead.

Multiprocessing and Multithreading

The main difference between multiprocessing and multithreading is that multiprocessing uses separate memory spaces while multithreading uses the same memory space. Therefore, it would be easier to share objects with multithreading. To use multiprocessing we need process as the object and to use multithread we need thread as the object. Notice below for the two sets of codes, we first have to define the two functions (to play the sounds for particular light levels). After defining the function, we then .append either the process or thread by setting the target="the name of the function defined" for all the different functions. Once all the threads or the processes are appended, we can then use .start() and .join() to execute the process. Using Multiprocessing:

from multiprocessing import Process
def play1():
 if(lightlevell > 800):
    s = pygame.mixer.sound.load('/home/pi/laserharp-sounds/1.wav')
    pygame.mixer.sound.play(-1)
def play2():
 if(lightlevel2 > 800):
    s = pygame.mixer.sound.load('/home/pi/laserharp-sounds/2.wav')
    pygame.mixer.sound.play(-1)
processes = []
if __name__=='__main__':
  processes.append(Process(target = play1))
  processes.append(Process(target = play2))
for process in processes:
  process.start()
for process in processes:
  process.join()

Using Multithreading:

from threading import Thread
def play1():
 if(lightlevell > 800):
    s = pygame.mixer.sound.load('/home/pi/laserharp-sounds/1.wav')
    pygame.mixer.sound.play(-1)
def play2():
 if(lightlevel2 > 800):
    s = pygame.mixer.sound.load('/home/pi/laserharp-sounds/2.wav')
    pygame.mixer.sound.play(-1)
threads = []
if __name__=='__main__':
  threads.append(Thread(target = play1))
  threads.append(Thread(target = play2))
for thread in threads:
  thread.start()
for thread in threads:
  thread.join()


Jave is generally the best programming language to use multiprocessing and multithreading.If you choose to use Java, this might be a useful link. It shows how to use Threads to play multiple sounds at once. - Java link

Pygame with Channels

#this is code from the final version of code used to program the Laser Harp. This is saying that if lasers 1 and/or 2 are broken, the wav file associated with that file will be played. 
import pygame
pygame.mixer.pre_init()
pygame.mixer.init()
pygame.init()
while true
if(light_level > 800):  # state 1
  pygame.mixer.Channel(0).play(pygame.mixer.Sound('/home/pi/laserharp-sounds/1.wav'))
if (light_level2 > 800):  # state 2
  pygame.mixer.Channel(1).play(pygame.mixer.Sound('/home/pi/laserharp-sounds/2.wav'))

We can define the channel number within the brackets and then follow with .play to play a particular sound file. Each sound file must be called from a different channel -- we can achieve this by assigning a different channel number to each sound file as shown above in the codes.

Final version of code

Link to project

Laser Harp