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()

Upvotes: 5

Views: 3701

Answers (7)

Laughingrice
Laughingrice

Reputation: 359

Long time after the fact but,

  1. The threads may start at the same time but playback / recording won't start at the same time due to limitations with windows. There is actually a buffer on both behind the scene causing an (unknown or controllable) delay.

  2. The best way to do this is to create an asynchronous read / write stream (one stream that does both read and write). That is, make a stream with a callback function and define both input=True and output=True (I'm assuming pyaudio here).

I'm not sure how pyaudio implements threading behind the scenes, especially if it's blocking audio, but another issue is the GIL (global interpreter lock). I didn't research enough into the current state of things and how in behaves under different python implementations, but IIRC only one thread can run python code at a given time, so python for the most part isn't very multi threading friendly at the moment AFAIK

Upvotes: 0

IT Ninja
IT Ninja

Reputation: 6430

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 :)

Upvotes: 5

Thank you IT Ninja,

your code did the job but I had to change it a bit to:

def main():
start_time=time.time()+1
def play1():
    while time.time() < start_time:
        pass
    threading.Thread(target=listen_to_audio(5)).start()
def play2():
    while time.time() < start_time:
        pass
    threading.Thread(target=play_audio(5)).start()

threading.Thread(target=play1).start()
threading.Thread(target=play2).start()

Now it works:) Thank you all!!

Upvotes: 1

Aamir Rind
Aamir Rind

Reputation: 39659

Why to start both functions using threads? You can simply call first function and initiate second one using thread within first function.

import threading


def play_audio():
    threading.Thread(target=listen_to_audio).start()
    #rest of code for play_audio

I think the delay here will be very less.

Upvotes: 0

Jon Clements
Jon Clements

Reputation: 142136

You could start with:

import threading

threading.Thread(target=listen_to_audio).start()
threading.Thread(target=play_audio).start()

Upvotes: 2

Cameron Sparr
Cameron Sparr

Reputation: 3981

I would use threads:

import threading

threads = []
threads.append(threading.Thread(target=listen_to_audio))
threads.append(threading.Thread(target=play_audio))

map(lambda x: x.start(), threads)

EDIT: not sure if map will start the threads absolutely at the same time, but it should be very very close

Upvotes: 2

snakesNbronies
snakesNbronies

Reputation: 3910

I'd create a def startBoth() to wrap both functions, importing both listen_to_audio and play_audio as necessary. Once you initiate startBoth(), it would call both functions.

Upvotes: -1

Related Questions