From e6fbaa2ffd4a3513c09edd920058ef86575d4906 Mon Sep 17 00:00:00 2001 From: hackEns Date: Thu, 12 Dec 2013 12:24:41 +0100 Subject: [PATCH] Correction of annoying cracks Due to the fact that we cut the sound at n seconds and not at the end of the period. Sidenote : dropped numpy part in sound4python, not happy with it. --- Chorizo player/chorizo.py | 2 +- Chorizo player/sound4python.py | 31 ++++++++++++++++--------------- Chorizo player/test_player.py | 3 ++- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/Chorizo player/chorizo.py b/Chorizo player/chorizo.py index 9ebbfa6..cbb3534 100755 --- a/Chorizo player/chorizo.py +++ b/Chorizo player/chorizo.py @@ -16,7 +16,7 @@ # ============================================================================= from sound4python import sound -from multiprocessing oimport Process +from multiprocessing import Process import math import serial import sys diff --git a/Chorizo player/sound4python.py b/Chorizo player/sound4python.py index fff9450..f035d7b 100644 --- a/Chorizo player/sound4python.py +++ b/Chorizo player/sound4python.py @@ -41,24 +41,25 @@ def sound(itr,samprate=16000,autoscale=True,output=False): waveWrite.setframerate(samprate) # 8kHz default wroteFrames=False + # Not satisfied by the numpy implementation, buzzing and so... #Let's try to create sound from NumPy vector - if( foundNumpy ): - if( type(itr)==np.array ): - if( itr.ndim == 1 or itr.shape.count(1) == itr.ndim - 1 ): - waveWrite.writeframes( (mult*itr.flatten()).astype(np.int16).tostring() ) - wroteFrames=True - else: #we have np, but the iterable isn't a vector - waveWrite.writeframes( (mult*np.array(itr)).astype(np.int16).tostring() ) - wroteFrames=True - if( not wroteFrames and not foundNumpy ): + #if( foundNumpy ): + # if( type(itr)==np.array ): + # if( itr.ndim == 1 or itr.shape.count(1) == itr.ndim - 1 ): + # waveWrite.writeframes( (mult*itr.flatten()).astype(np.int16).tostring() ) + # wroteFrames=True + # else: #we have np, but the iterable isn't a vector + # waveWrite.writeframes( (mult*np.array(itr)).astype(np.int16).tostring() ) + # wroteFrames=True + #if( not wroteFrames and not foundNumpy ): #python w/o np doesn't have "short"/"int16", "@h" is "native,aligned short" # BAD : waveWrite.writeframes( struct.pack(len(itr)*"@h",[int(mult*itm) for itm in itr]) ) - writeValues = [] - for itm in itr: - packed_value = struct.pack('h', int(mult*itm)) - waveWrite.writeframes(packed_value) - - wroteFrames=True + writeValues = [] + for itm in itr: + packed_value = struct.pack('h', int(mult*itm)) + waveWrite.writeframes(packed_value) + + wroteFrames=True if( not wroteFrames ): print("E: Unable to create sound. Only 1D numpy arrays and numerical lists are supported.") diff --git a/Chorizo player/test_player.py b/Chorizo player/test_player.py index 4eacbe0..fc81f8c 100755 --- a/Chorizo player/test_player.py +++ b/Chorizo player/test_player.py @@ -73,6 +73,7 @@ running = True while running: char = getch() if char == "q": + print("Exiting...") running = False continue elif char == "a": @@ -93,7 +94,7 @@ while running: continue print("Playing "+char.upper()) - processes.append(Process(target=play_wave, args=(frequency, 0.2))) + processes.append(Process(target=play_wave, args=(frequency, math.floor(0.2 * frequency + 1) / frequency))) processes[-1].start() for i in processes: