def _playnn_with_ffplay(seg): #Play sound func PLAYER = get_player_name() with NamedTemporaryFile("w+b", suffix=".wav") as f: seg.export(f.name, "wav") subprocess.call( [PLAYER, "-nodisp", "-autoexit", "-hide_banner", f.name], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
def play_sound(self, seg): audio_player = get_player_name() with NamedTemporaryFile('w+b', suffix='.wav') as f: seg.export(f.name, 'wav') devnull = open(os.devnull, 'w') subprocess.call([ audio_player, "-nodisp", "-autoexit", "-hide_banner", f.name ], stdout=devnull, stderr=devnull)
def _play_with_ffplay_suppress(seg): # seg = seg - 10 PLAYER = get_player_name() with NamedTemporaryFile("w+b", suffix=".wav") as f: seg.export(f.name, "wav") devnull = open(os.devnull, 'w') subprocess.call( [PLAYER, "-nodisp", "-autoexit", "-hide_banner", f.name], stdout=devnull, stderr=devnull)
def _play_with_ffplay_suppress(seg): """ Play mp3 files without console output modified clone of original pydub code""" PLAYER = get_player_name() # create temporary mp3 file for audio output since "with NamedTemporaryFile("w+b", suffix=".mp3") as f:" # as used in original pydub code comes up with double back slash errors in Windows if os.path.exists(filepath + '.' + path_delim): with open(filepath + '.' + path_delim + str_play_sound_file, 'wb') as f: seg.export(f.name, "mp3") devnull = open(os.devnull, 'w') subprocess.call( [PLAYER, "-nodisp", "-autoexit", "-hide_banner", f.name], stdout=devnull, stderr=devnull)
class Player(threading.Thread): PLAYER = get_player_name() def __init__(self, clip: AudioSegment, start_pos: int, loop_time: int, callback: classmethod): # print({ # "clip length (ms)": len(clip), # "start pos (ms)": start_pos, # "loop time (ms)": loop_time, # "result play length (ms)": len(clip[start_pos:]) # }) self.clip = clip[start_pos:] self.stream = None """:type : Stream""" self.local_pos = start_pos self.loop_time = loop_time self.playing = False self.time_callback = callback super().__init__(target=self.play) def __play_with_pyaudio(self, seg: AudioSegment): self.stream = MUSIC.AUDIO.open( format=MUSIC.AUDIO.get_format_from_width(seg.sample_width), channels=seg.channels, rate=seg.frame_rate, output=True) self.playing = True # PLAYBACK LOOP for chunk in make_chunks(seg, MUSIC.BITE_SIZE_MS): if not self.playing: return if self.local_pos <= self.loop_time: self.time_callback() self.stream.write(chunk._data) # Done self.stop() def play(self): MUSIC.KILLERS.append(self.stop) self.__play_with_pyaudio(self.clip) def stop(self): if self.playing: self.playing = False
import sys, os from rsudp.raspberryshake import ConsumerThread from rsudp import printM, printW, printE from rsudp.test import TEST import subprocess try: from pydub.playback import play from pydub import AudioSegment, utils pydub_exists = True # avoids import error that arises between pydub 0.23.1 and 0.25.1 global PLAYER PLAYER = utils.get_player_name() TEST['d_pydub'][1] = True except ImportError as e: global ERR ERR = e pydub_exists = False class AlertSound(ConsumerThread): """ .. _pydub.AudioSegment: https://github.com/jiaaro/pydub/blob/master/API.markdown#audiosegment A consumer class that plays an alert sound when an ``ALARM`` message arrives on the queue. ``rsudp.c_alertsound.AlertSound.sound`` is a pydub.AudioSegment_ object and is passed from the client. :param sta: short term average (STA) duration in seconds. :type sta: bool or pydub.AudioSegment_ :param queue.Queue q: queue of data and messages sent by :class:`rsudp.c_consumer.Consumer`. """
import _thread import termios import select import signal import shutil import time import sys import tty import os from pydub import AudioSegment from pydub.utils import get_player_name import subprocess from tempfile import NamedTemporaryFile PLAYER = get_player_name() def play(seg): with NamedTemporaryFile('wb', suffix='.wav') as fd: devnull = open(os.devnull, 'w') seg.export(fd.name, 'wav') subprocess.call( [PLAYER, '-nodisp', '-autoexit', fd.name], stdout=devnull, stderr=devnull, ) REFRESH_RATE = 0.05 GOODBYE_DELAY = 0.2
class Sound(StatusObject): FFPLAY_PLAYER = get_player_name() def __init__(self, segment): super().__init__() self._segment = segment self._tmp_file = None self._popen = None def __del__(self): if self._popen: self._popen.kill() self._popen = None if self._tmp_file: self._tmp_file.close() def _create_tmp(self): self._tmp_file = NamedTemporaryFile("w+b", suffix=".wav") self._segment.export(self._tmp_file.name, "wav") def _create_popen(self): self._popen = subprocess.Popen([ self.FFPLAY_PLAYER, "-nodisp", "-autoexit", "-hide_banner", self._tmp_file.name ]) def play(self): if self.status() == STATUS.PLAYING: return elif self._status not in (STATUS.STOPPED, STATUS.PAUSED): raise Exception() if self._tmp_file is None: self._create_tmp() if self._popen is None: self._create_popen() elif self._status == STATUS.PAUSED: self._popen.send_signal(signal.SIGCONT) super().play() def wait(self, timeout=None): code = self._popen.wait(timeout=timeout) return code def poll(self): if self._popen: code = self._popen.poll() if code is not None: if code == signal.SIGSTOP: self._status = STATUS.PAUSED elif code == signal.SIGCONT: self._status = STATUS.PLAYING else: # code == signal.SIGTERM: self._status = STATUS.STOPPED return self._status def pause(self): if self._status != STATUS.PLAYING: raise Exception() self._popen.send_signal(signal.SIGSTOP) super().pause() def stop(self): if self._status != STATUS.PLAYING: raise Exception() if self._popen: self._popen.kill() self._popen = None if self._tmp_file: self._tmp_file.close() super().stop()
def _my_play_with_ffplay(seg): tmp = tempfile.NamedTemporaryFile("w+b", suffix=".wav", delete=False) seg.export(tmp.name, "wav") proc = subprocess.Popen( [get_player_name(), "-nodisp", "-autoexit", tmp.name]) running_sound_processes.append(proc)
def on_arm(arm, direction): print "Arm:", arm, "direction:", direction say_selected(None) current_menu = menu.MenuFactory.create("default_menu.json") print "Total menu items:", current_menu.get_len() print "Menu depth:", current_menu.get_depth() nav = menu.MenuNavigator(current_menu) im = InputManager() am = AudioFileManager("en", "/tmp/") PLAYER = get_player_name() def custom_play(filename): FNULL = open(os.devnull, 'w') subprocess.Popen([PLAYER, "-nodisp", "-autoexit", filename], stdout=FNULL, stderr=subprocess.STDOUT) def vibrate_one(pose): m.vibrate(1) def print_current_node(pose): print "Pose:", pose print "Current:", nav.get_selected().parent.name print "Selected:", nav.get_selected().name