Playing a chord in python

I want to make a platform for playing chords such as guitars. For example, to play an E chord, he plays [0, 2, 2, 1, 0, 0] (from the Low-E line to the High-E line).

I am trying to play chords in python by playing all different lines at once (using streams).

The problem is that every time I start playing the following lines, it seems that the last line stops playing, and the new one replaces it. So all that I hear after playing a chord is the highest line (last).

Am I using threads incorrectly? Or is this a problem with current features? Or maybe it's a winsound.Beep () function that can handle such things?

This is my code:

from winsound import Beep import threading import time def play(freq, dur): Beep(round(freq), round(dur)) def get_freq(string, fret): a3_freq = 220 half_tone = 2 ** (1 / 12) higher_from_a3_by = 5 * (string - 2) + fret if string > 4: higher_from_a3_by += 1 return a3_freq * half_tone ** higher_from_a3_by def strum(string, fret, time): freq = get_freq(string, fret) t = threading.Thread(target=play, args=(freq, time)) t.start() return t def chord(frets, dur): threads = [] for i in range(6): if frets[i] != -1: threads.append(strum(i + 1, frets[i], dur)) for t in threads: t.join() chord((-1, 0, 2, 2, 2, 0), 1000) # plays the A chord for one second, for example. 

From what I sketched, the play () and get_freq () functions have no problems.

So what is the problem, and how can I fix it?

Edit:

I tried it in C #, but it didn't work either. Is this the correct way to start threads in C #?

 foreach (Thread t in threads) t.Start(); foreach (Thread t in threads) t.Join(); 
+6
source share

Source: https://habr.com/ru/post/1011842/


All Articles