Python Forum
How to execute several fonctions at the same time
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to execute several fonctions at the same time
#1
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 ?
Reply
#2
I think you want to use multiprocessing (not threading) here.
see: https://docs.python.org/3/library/multiprocessing.html
Reply
#3
https://python-forum.io/Thread-Tkinter-H...ng-the-gui
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  struggling w/ fonctions in Python Tiril123 7 3,731 May-09-2020, 10:51 AM
Last Post: pyzyx3qwerty

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020