10 examples of 'play wav files python' in Python

Every line of 'play wav files python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
37def play_file(wavfile):
38 f = open(wavfile, "rb")
39 wav = audioio.WaveFile(f)
40 a.play(wav)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

11def play_file(wavfile):
12 audio_file = open(wavfile, "rb")
13 wav = audioio.WaveFile(audio_file)
14 speaker.play(wav)
15 while speaker.playing:
16 pass
36def play_wav(cfn):
37
38 global wav16_dir, tts, corpus
39
40 wavfn = '%s/%s/%s.wav' % (wav16_dir, corpus, cfn)
41
42 with open(wavfn) as wavf:
43 wav = wavf.read()
44
45 tts.play_wav(wav, async=True)
20def vocoderplayfile(self):
21 """ Simil vocoder effect """
22 raise NotImplementedError
57def play_wav(self, wav_path):
58 """Play audio from the given WAV file.
59
60 The file should be mono and small enough to load into memory.
61 Args:
62 wav_path: path to the wav file
63 """
64 with wave.open(wav_path, 'r') as wav:
65 if wav.getnchannels() != 1:
66 raise ValueError(wav_path + ' is not a mono file')
67
68 frames = wav.readframes(wav.getnframes())
69 self.play_bytes(frames, wav.getframerate(), wav.getsampwidth())
38def play_audio_file(fname=DETECT_DING):
39
40
41 with open("queue.json", "a") as myfile:
42 myfile.write('{"actions":{"0":{"Module":"Listener","Action":"start_listener","Params":{}}}}')
43 #myfile.write('{"actions":{"0":{"Module":"Speaker","Action":"Speak","Params":{"text":"Yes"}},"1":{"Module":"Listener","Action":"start_listener","Params":{}}}}')
44
45 sys.exit()
189def do_playback():
190
191 global recording, buf
192
193 buf = StringIO()
194
195 wf = wave.open(buf, 'wb')
196 wf.setnchannels(1)
197 wf.setsampwidth(2)
198 wf.setframerate(SAMPLE_RATE)
199
200 packed_audio = struct.pack('%sh' % len(recording), *recording)
201 wf.writeframes(packed_audio)
202
203 player.play(buf.getvalue())
204
205 buf.close()
152def play_sound(filename):
153 """Play the given sound file using the `afplay` command line utility."""
154 subprocess.Popen(['afplay', filename])
38def play_audio_p(path):
39 path = os.path.realpath(path)
40 subprocess.call([player_name, path])
47def _linux_wav_play(filename):
48 for _ in ("aplay", "paplay", "play"):
49 if not os.system("%s '%s' 2>/dev/null" % (_, filename)):
50 return
51
52 import ctypes
53
54 PA_STREAM_PLAYBACK = 1
55 PA_SAMPLE_S16LE = 3
56 BUFFSIZE = 1024
57
58 class struct_pa_sample_spec(ctypes.Structure):
59 _fields_ = [("format", ctypes.c_int), ("rate", ctypes.c_uint32), ("channels", ctypes.c_uint8)]
60
61 pa = ctypes.cdll.LoadLibrary("libpulse-simple.so.0")
62
63 wave_file = wave.open(filename, "rb")
64
65 pa_sample_spec = struct_pa_sample_spec()
66 pa_sample_spec.rate = wave_file.getframerate()
67 pa_sample_spec.channels = wave_file.getnchannels()
68 pa_sample_spec.format = PA_SAMPLE_S16LE
69
70 error = ctypes.c_int(0)
71
72 pa_stream = pa.pa_simple_new(None, filename, PA_STREAM_PLAYBACK, None, "playback", ctypes.byref(pa_sample_spec), None, None, ctypes.byref(error))
73 if not pa_stream:
74 raise Exception("Could not create pulse audio stream: %s" % pa.strerror(ctypes.byref(error)))
75
76 while True:
77 latency = pa.pa_simple_get_latency(pa_stream, ctypes.byref(error))
78 if latency == -1:
79 raise Exception("Getting latency failed")
80
81 buf = wave_file.readframes(BUFFSIZE)
82 if not buf:
83 break
84
85 if pa.pa_simple_write(pa_stream, buf, len(buf), ctypes.byref(error)):
86 raise Exception("Could not play file")
87
88 wave_file.close()
89
90 if pa.pa_simple_drain(pa_stream, ctypes.byref(error)):
91 raise Exception("Could not simple drain")
92
93 pa.pa_simple_free(pa_stream)

Related snippets