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
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
45 """
46 Check that the mixer's initialized and initialize it if not.
47
48 """
49
50
51 check_pygame()
52 from pygame import init, mixer
53
54 mixer.pre_init(frequency=44100)
55 init()
56
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
69
70 smp_array = numpy.array(zip(samples,samples)).astype(numpy.int16)
71
72
73 snd = make_sound(smp_array)
74
75 channel = snd.play()
76
77
78 channel.set_endevent(USEREVENT)
79
80 if wait_for_end:
81
82 event.wait()
83