Python Forum

Full Version: How to execute several fonctions at the same time
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hello,
I have a python project where I should use this fonction to make musics.
from simpleaudio import *
import numpy as np

def sound(freq, duration):
    sample_rate = 44100
    t = np.linspace(0, duration, int(duration * sample_rate), False)
    tone = np.sin(freq * t * 2 * np.pi)
    tone *= 8388607 / np.max(np.abs(tone))
    tone = tone.astype(np.int32)
    i = 0
    byte_array = []
    for b in tone.tobytes():
        if i % 4 != 3:
            byte_array.append(b)
        i += 1
    audio = bytearray(byte_array)
    play_obj = play_buffer(audio, 1, 3, sample_rate)
    play_obj.wait_done()
I want to integrate this function in a Tkinter window to play music while displaying notes on the screen. I know how to display the note but my function sound block my Tkinter window.
So how should I call my function sound to execute the two at the same time ?
I think you want to use multiprocessing (not threading) here.
see: https://docs.python.org/3/library/multiprocessing.html
Thanks for your answers, I read those two links, but I'm a beginner in python so I still don't understand how those two methods works and how I can use it for my program. Can someone explain it to me more simply?