Package jazzparser :: Package harmonical :: Module output
[hide private]
[frames] | no frames]

Source Code for Module jazzparser.harmonical.output

 1  """Audio output for the harmonical. 
 2   
 3  Routines for preparing and using audio output. These rely on PyGame,  
 4  which is an optional dependency. 
 5   
 6  """ 
 7  """ 
 8  ============================== License ======================================== 
 9   Copyright (C) 2008, 2010-12 University of Edinburgh, Mark Granroth-Wilding 
10    
11   This file is part of The Jazz Parser. 
12    
13   The Jazz Parser is free software: you can redistribute it and/or modify 
14   it under the terms of the GNU General Public License as published by 
15   the Free Software Foundation, either version 3 of the License, or 
16   (at your option) any later version. 
17    
18   The Jazz Parser is distributed in the hope that it will be useful, 
19   but WITHOUT ANY WARRANTY; without even the implied warranty of 
20   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
21   GNU General Public License for more details. 
22    
23   You should have received a copy of the GNU General Public License 
24   along with The Jazz Parser.  If not, see <http://www.gnu.org/licenses/>. 
25   
26  ============================ End license ====================================== 
27   
28  """ 
29  __author__ = "Mark Granroth-Wilding <mark.granroth-wilding@ed.ac.uk>" 
30   
31  from jazzparser.utils.base import load_from_optional_package, \ 
32              load_optional_package 
33  from jazzparser.harmonical.files import SoundFile 
34  from StringIO import StringIO 
35  import wave 
36   
37 -def check_pygame():
38 """ 39 Checks that PyGame can be imported and outputs an error if not. 40 41 """ 42 load_optional_package('pygame', 'PyGame', "outputing audio")
43
44 -def init_mixer():
45 """ 46 Check that the mixer's initialized and initialize it if not. 47 48 """ 49 # Check PyGame is available 50 # This will raise an error if it's not 51 check_pygame() 52 from pygame import init, mixer 53 # Set the mixer settings before initializing everything 54 mixer.pre_init(frequency=44100) 55 init()
56
57 -def play_audio(samples, wait_for_end=False):
58 """ 59 Uses PyGame to play the audio samples given as a list of samples. 60 61 """ 62 init_mixer() 63 from pygame.mixer import music 64 from pygame.sndarray import make_sound 65 from pygame import USEREVENT, event 66 import numpy 67 68 # Prepare the wave data to play 69 # Make it stereo and the right number format 70 smp_array = numpy.array(zip(samples,samples)).astype(numpy.int16) 71 72 # Generate the sound object from the sample array 73 snd = make_sound(smp_array) 74 # Set it playing 75 channel = snd.play() 76 # Set an event to be triggered when the music ends, so we can 77 # wait for it if necessary 78 channel.set_endevent(USEREVENT) 79 80 if wait_for_end: 81 # Wait until the music finishes 82 event.wait()
83