1

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()
mgilson
  • 300,191
  • 65
  • 633
  • 696
The Professor
  • 51
  • 1
  • 10
  • 1
    You asked this exact question a few days ago. – Blender Oct 15 '12 at 19:09
  • @Blender I know (duh). The answers I got were not very helpful (for me anyways) so I took it down and re-posted with a little more detail hoping to get some better ones – The Professor Oct 15 '12 at 19:12

1 Answers1

3

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.

Community
  • 1
  • 1
sinelaw
  • 16,205
  • 3
  • 49
  • 80
  • I've checked out the related question already. As for the rest, I am way to much of a baby programmer to know how to do that. I haven't even looked at PyGame yet, so I'll have to have a little more simplicity and/or detail. – The Professor Oct 15 '12 at 19:16
  • I've added a link to `myro` which claims it can be done using their library. – sinelaw Oct 15 '12 at 19:19
  • Yes, but this program is reading a song file. Thus the "saveSong(HftM1, "HymnfortheMissing1.txt", append=1)" part of the code. That would only work if I was trying to just combine two different tones. I am trying to basically combine two different sheets of music or sets of tones. – The Professor Oct 15 '12 at 19:26
  • You can use the `beep` function on the songs after they are converted to tones. That is, use it in your loop at the end instead of calling `winsound.Beep`. You'll have to write some logic that figures out how to split the lengths of each tone in case where one song is changing a tone while the other is still playing the older tone. If you don't *have* to use the song files in that format, I recommend you just switch to pygame. Check out some of [these pygame tutorials](http://www.pygame.org/wiki/tutorials) to get started. – sinelaw Oct 15 '12 at 20:08
  • You're saying change "for n in range(len(song1)): play = song1[n] note = play[0] duration = play[1] winsound.Beep(int(note), int(duration*2000))" to: "for n in range(len(song2)): play = song1[n], song2[n] note = play[0] duration = play[1] beep(int(note), int(duration*2000))"? – The Professor Oct 15 '12 at 20:14
  • No, you need to use all the information from both songs (both frequency and duration). I suggest you think about the problem at hand in general terms before trying to program it. – sinelaw Oct 15 '12 at 20:15
  • I don't think you can put that much information into a beep function. Besides, the beep function uses frequency, the song function uses notes and octaves. – The Professor Oct 15 '12 at 20:17
  • I've added an example of how you could do it. – sinelaw Oct 16 '12 at 16:27