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.
This commit is contained in:
hackEns 2013-12-12 12:24:41 +01:00
parent 5a4fe91ce1
commit e6fbaa2ffd
3 changed files with 19 additions and 17 deletions

View File

@ -16,7 +16,7 @@
# ============================================================================= # =============================================================================
from sound4python import sound from sound4python import sound
from multiprocessing oimport Process from multiprocessing import Process
import math import math
import serial import serial
import sys import sys

View File

@ -41,24 +41,25 @@ def sound(itr,samprate=16000,autoscale=True,output=False):
waveWrite.setframerate(samprate) # 8kHz default waveWrite.setframerate(samprate) # 8kHz default
wroteFrames=False wroteFrames=False
# Not satisfied by the numpy implementation, buzzing and so...
#Let's try to create sound from NumPy vector #Let's try to create sound from NumPy vector
if( foundNumpy ): #if( foundNumpy ):
if( type(itr)==np.array ): # if( type(itr)==np.array ):
if( itr.ndim == 1 or itr.shape.count(1) == itr.ndim - 1 ): # if( itr.ndim == 1 or itr.shape.count(1) == itr.ndim - 1 ):
waveWrite.writeframes( (mult*itr.flatten()).astype(np.int16).tostring() ) # waveWrite.writeframes( (mult*itr.flatten()).astype(np.int16).tostring() )
wroteFrames=True # wroteFrames=True
else: #we have np, but the iterable isn't a vector # else: #we have np, but the iterable isn't a vector
waveWrite.writeframes( (mult*np.array(itr)).astype(np.int16).tostring() ) # waveWrite.writeframes( (mult*np.array(itr)).astype(np.int16).tostring() )
wroteFrames=True # wroteFrames=True
if( not wroteFrames and not foundNumpy ): #if( not wroteFrames and not foundNumpy ):
#python w/o np doesn't have "short"/"int16", "@h" is "native,aligned short" #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]) ) # BAD : waveWrite.writeframes( struct.pack(len(itr)*"@h",[int(mult*itm) for itm in itr]) )
writeValues = [] writeValues = []
for itm in itr: for itm in itr:
packed_value = struct.pack('h', int(mult*itm)) packed_value = struct.pack('h', int(mult*itm))
waveWrite.writeframes(packed_value) waveWrite.writeframes(packed_value)
wroteFrames=True wroteFrames=True
if( not wroteFrames ): if( not wroteFrames ):
print("E: Unable to create sound. Only 1D numpy arrays and numerical lists are supported.") print("E: Unable to create sound. Only 1D numpy arrays and numerical lists are supported.")

View File

@ -73,6 +73,7 @@ running = True
while running: while running:
char = getch() char = getch()
if char == "q": if char == "q":
print("Exiting...")
running = False running = False
continue continue
elif char == "a": elif char == "a":
@ -93,7 +94,7 @@ while running:
continue continue
print("Playing "+char.upper()) 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() processes[-1].start()
for i in processes: for i in processes: