Vladimir Shevyakov
Vladimir Shevyakov

Reputation: 2831

Playing several notes simultaneously using winsound on Python 3

I'm trying to make a function that plays several notes (a chord) simultaneously using the winsound module on python 3. I made a function called playNote() that would take in a note in the format of 'C4' (middle C) and the duration, in milliseconds. For example:

playNote('C4', 1000)

so I tried to make a function that would play a chord like this:

def playChord (notes, length):
    notes *= length
    for counter in range(0,length//100):
        playNote(notes[counter], 1)

that would be called like:

playChord(['C4', 'E4', 'G4'], 1000)

but that did not make a sound. I changed it to:

def playChord (notes, length):
    notes *= length
    for counter in range(0,length//100):
        playNote(notes[counter], 100)

but that, of course, just played the notes after one another as the playNote() duration of 100 defeated the original purpose of playing the notes one after another in succession quick enough to sound like they are being played at the same time.

I looked into a module called pyFluidSynth, but it seems it is not compatible with the 64-bit version of python.

What am I doing terribly wrong? And is there and easier way to do this?

Upvotes: 0

Views: 1213

Answers (1)

Matias Kotlik
Matias Kotlik

Reputation: 11

I can't test this right now - but I think using threads might work.

import threading
def playChord(notes, length):
    for note in notes:
        t = threading.Thread(target=playNote, args=(note,length))
        t.start()
    time.sleep(length)

You'll have to play around with it to get the timing right, I think.

Upvotes: 1

Related Questions