How do you play two different song files on python at the same time?

1.7k views Asked by At

I'm trying to play song1 and song2 (the commented out one) at the same time, but I don't know how to do that using myro or winsound or whatever it is. Someone suggested using threading, but I'm not sure how to do that either, since I am just a very baby beginner programmer. Would someone please help me figure this thing out and/or explain in detail how to do this? Here's what I have so far:

import winsound
from myro import *
def main():

    HftM1 = makeSong("Db4 3/8; C4 3/8; Bb3 1/4; Bb3 3/8; Ab3 3/8; Gb3 3/8; Ab3 1/16; C4 3/8; Bb3 3/8; Ab3 1/8; Eb3 1/16; F3 1/16; Ab3 3/8; G3 3/8; F3 3/8; C4 1/16; Db4 3/8; C4 3/8; Bb3 1/8; F3 1/16; Gb3 1/16; Bb3 3/8; Ab3 3/8; Gb3 3/8; C4 3/8; Bb3 3/8; Ab3 1/8; Eb3 1/16; F3 1/16; Ab3 3/8; G3 3/8; F3 3/8")

    saveSong(HftM1, "HymnfortheMissing1.txt", append=1)

    HftM2 = makeSong("Bb2 1/8; F3 1/8; Bb3 1/4; Bb2 1/8; F3 3/8; Gb2 1/8; Db3 1/8; Gb3 1/4; Gb2 1/8; Db3 3/8; Ab2 1/8; Eb3 1/8; Ab3 1/4; Ab2 1/8; Eb3 3/8; F2 1/8; C3 1/8; F3 1/4; F2 1/8; C3 3/8; Bb2 1/8; F3 1/8; Bb3 1/4; Bb2 1/8; F3 3/8; Gb2 1/8; Db3 1/8; Gb3 1/4; Gb2 1/8; Db3 3/8; Ab2 1/8; Eb3 1/8; Ab3 1/4; Ab2 1/8; Eb3 3/8; F2 1/8; C3 1/8; F3 1/4; F2 1/8; C3 3/8")

    saveSong(HftM2, "HymnfortheMissing2.txt", append=1)

    song1 = readSong("HymnfortheMissing1.txt")

    #song2 = readSong("HymnfortheMissing2.txt") #This part of the song is supposed to be played at the same time as the first part, but I don't know how to do that so...

    play = []

    for n in range(len(song1)):
        play = song1[n]
        note = play[0]
        duration = play[1]
        winsound.Beep(int(note), int(duration*2000))
main()
1

There are 1 answers

8
sinelaw On BEST ANSWER

I recommend you use PyGame for this kind of work, and check out this related question. You'd have to use the mixer module.

In general you would have to mix the PCM data using some mathematical manipulation on the audio streams (for an example discussion see this question).


Update: Myro (which I have never used) says in the docs that there is a way to do this using the beep function:

beep(self, duration, frequency, frequency2 = None): make a tone. If two tones are given, the robot will combine them.

As a simple example, if you have note A, duration 1 second in the first song, and note B, duration 2 seconds in the second song you will have to call beep twice: First to play both notes A and B simultaneously for 1 second, and then to play only note B for another 1 second.

Needless to say, this code will be much more complicated than just playing sounds from audio files using something like PyGame and mixing them.