Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting two methods at the same time in Python

I am trying to run two methods at the same time in Python. One of them plays a sound and the other one records it. Both methods work fine but I could not figure it out how to start them at the same time both multiprocessing and threading were tried. Whereas I am almost sure now that it can't be solved with threading.

def listen_to_audio()

def play_audio()

Any ideas? (They don't have to finish at the same time but they should both start within a second.)

That is the code, sorry for not posting it in the beginning:

import pyaudio
import wave
import sys
import time
from math import *
from getopt import *
import threading

def make_sin(f0=1000.,ampl=30000,rate=22050,length=5.):
  a = 2. * pi * f0/rate              
  n = int(rate * length)
  wav=''
  for i in range(0, n):
    f = int(ampl*sin(a*i))
    wav += chr(f & 0xFF) + chr((f & 0xFF00) >> 8)
  return wav



def play_audio(forHowLong):
    data = make_sin(f0=1000.,ampl=30000,rate=22050,length=5.)

    p = pyaudio.PyAudio() #sets up portaudio system
    stream = p.open(format=p.get_format_from_width(2),
                channels=1,
                rate=22050,
                output=True)

    start = time.time()
    while time.time() < start + forHowLong*0.5:    
            stream.write(data)

    stream.stop_stream()
    stream.close()
    p.terminate()


def listen_to_audio(forHowLong):
    CHUNK = 1024
    FORMAT = pyaudio.paInt16
    CHANNELS = 2
    RATE = 44100
    RECORD_SECONDS = forHowLong
    WAVE_OUTPUT_FILENAME = "output.wav"

    p = pyaudio.PyAudio()

    stream = p.open(format=FORMAT,
            channels=CHANNELS,
            rate=RATE,
            input=True,
            frames_per_buffer=CHUNK)

    print("* recording")

    frames = []

    for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
        data = stream.read(CHUNK)
        frames.append(data)

    print("* done recording")

    stream.stop_stream()
    stream.close()
    p.terminate()

    wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
    wf.setnchannels(CHANNELS)
    wf.setsampwidth(p.get_sample_size(FORMAT))
    wf.setframerate(RATE)
    wf.writeframes(b''.join(frames))
    wf.close()


def main():

#start play_audio and record_audio here at the same time



if __name__ == "__main__":
    main()
like image 204
Michael Lichtensteiner Avatar asked Nov 16 '12 18:11

Michael Lichtensteiner


1 Answers

import threading,time
def play1():
    while time.time() <= start_time:
        pass
    threading.Thread(target=listen_to_audio).start()
def play2():
    while time.time() <= start_time:
        pass
    threading.Thread(target=play_audio).start()
start_time=time.time()+20
threading.Thread(target=play1).start()
threading.Thread(target=play2).start()

This should work for you, it starts each function, and in each function it waits until it is the right time :)

like image 99
IT Ninja Avatar answered Sep 28 '22 21:09

IT Ninja