Python echo

This adds a echo to a wav file. The file also gets louder, because the result is not normalized. (sample + 0.8*Othersample v.s. sample+0.8*Othersample/1.8). This is my first real experiment with DSP.  Maybe More Later(tm). Comments are welcome. (no registration necessary / pre-moderated by me)

"""
Echo. Usage: python_Echo InpuFile.wav OutputFile.wav Delay_in_Seconds
0.15 seconds gives a believable echo. Pass a file multiple times trough it or modify source for multiple echo's

Only works with 1ch WAV files. To be improved Later(tm).
"""
import sys
import numpy as np
import wave
import struct

if len(sys.argv) != 4:
        print "Usage: python_Echo InpuFile.wav OutputFile.wav Delay_in_Seconds"
        exit()
        
delay = float(sys.argv[3])

if delay > 2:
        print "weirdly long delay. Try < 2"
        exit()
        
w= wave.open(sys.argv[1])
framerate = w.getframerate()
frames = w.getnframes()
channels = w.getnchannels()
width = w.getsampwidth()
print("sampling rate:", framerate, "Hz, length:", frames, "samples,",
"channels:", channels, "sample width:", width, "bytes")
    
data = w.readframes(frames)
sig = np.frombuffer(data, dtype='<i2').reshape(-1, channels)

echo_output = wave.open(sys.argv[2], 'w')
echo_output.setparams((channels, width, framerate, 0, 'NONE', 'not compressed')) #output file matches input file params

values = []

for i in range(0, frames):
        if i > delay*framerate:
                value = sig[i] + 0.8*sig[i-delay*framerate] #add echo
        else:
                value = sig[i]
        packed_value = struct.pack('h', value) #this failes for 2 channel files.
        values.append(packed_value)

value_str = ''.join(values)
echo_output.writeframes(value_str)

outsig = np.frombuffer(value_str, dtype='<i2').reshape(-1, channels)

echo_output.close()
print "done"

Used sources:
https://docs.python.org/2/library/wave.html
http://stackoverflow.com/questions/2060628/reading-wav-files-in-pythonhttp://soledadpenades.com/2009/10/29/fastest-way-to-generate-wav-files-in-python-using-the-wave-module/

And something about DSP from a dead-tree book.

Geef een reactie

Vul je gegevens in of klik op een icoon om in te loggen.

WordPress.com logo

Je reageert onder je WordPress.com account. Log uit / Bijwerken )

Twitter-afbeelding

Je reageert onder je Twitter account. Log uit / Bijwerken )

Facebook foto

Je reageert onder je Facebook account. Log uit / Bijwerken )

Google+ photo

Je reageert onder je Google+ account. Log uit / Bijwerken )

Verbinden met %s