Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Record IR Signal from IR audio receiver, and record as wav file for sending through IR audio transmitter
from array import array
from struct import pack
import numpy as np
import wave
import pyaudio
import codeset
import subprocess
import signal, sys
THRESHOLD = 1500
CHUNK_SIZE = 1024
FORMAT = pyaudio.paInt16
RATE = 44100
def record():
"""
Record signal from the microphone and
return the data as an array of 1/0 (according on THRESHOLD).
"""
p = pyaudio.PyAudio()
""" take data as MONO, because data set is smaller """
stream = p.open(format=FORMAT, channels=1, rate=RATE,
input=True, output=True,
frames_per_buffer=CHUNK_SIZE)
num_silent = 0
rcv_started = False
r = array('h')
while 1:
rcv_data = array('h', stream.read(CHUNK_SIZE))
""" change it to 1 and 0 accroding to THRESHOLD """
rcv_data = [ (x>THRESHOLD) for x in rcv_data ]
silent = (sum(rcv_data) < 10 )
if silent and rcv_started:
num_silent += 1
elif not silent and not rcv_started:
rcv_started = True
if rcv_started :
r.extend(rcv_data)
if rcv_started and num_silent > 10:
break
sample_width = p.get_sample_size(FORMAT)
stream.stop_stream()
stream.close()
p.terminate()
return sample_width, r
def sigterm_handler(_signo, _stack_frame):
sys.exit(0)
def main() :
while True:
waveFileName = raw_input("Enter button name (q to quit) : ")
if waveFileName is 'q' :
break
sample_width, data = record()
duration = len(data)/float(RATE)
sineWav = (np.sin(2*np.pi*np.arange(RATE*duration)*19000/RATE)).astype(np.float32)
track1 = sineWav * data * 30000.0
track2 = track1 * -1
wf = wave.open(waveFileName+".wav", 'wb')
wf.setnchannels(2)
wf.setsampwidth(sample_width)
wf.setframerate(RATE)
for s, t in zip(track1, track2):
wf.writeframes(pack('h', int(s)))
wf.writeframes(pack('h', int(t)))
wf.close()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.