Pachabel's Canon in D is a very interesting piece because it repeats the same 8 notes/chords over and over again with different melodies over it. I like to use it as background music for homework, but all of the "1-hour versions" that I find on youtube are just short versions repeated over and over, and I decided there had to be a better solution.

After spending hours figuring out how to play a sound file with Python, I have begun writing my Infinite Canon Generator. When completed, it will select one base line and two treble lines at random to play at the same time, repeating this over and over.

So far, I have gotten multiple lines to play at once just fine and now have figured out how to make the event loop end, but I can't make the process repeat yet:

import pyglet

Line1 ='Line1.aiff')
Line2 ='Line2.aiff')
Line3 ='Line3.aiff')

LineA ='LineA.aiff')
LineB ='LineB.aiff')

while True:

    event_loop =

    def on_window_close(window):

When run, the sound files of Lines 2, 3, A, and B will all play at once, (as a bunch of error messages spew out into the shell) and then the program will end. How should I go about making it repeat?

This is the error text:

Traceback (most recent call last):
  File "/Users/Johnny/Documents/Programming/Python/Sound/", line 12, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pyglet/media/", line 472, in play
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pyglet/media/", line 978, in queue
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pyglet/media/", line 695, in queue
    source = source._get_queue_source()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pyglet/media/", line 579, in _get_queue_source
    raise MediaException('This source is already queued on a player.') This source is already queued on a player.
Looks like pyglet's function is non-blocking so it starts playing a sound and returns immediately. You either need to know how long each clip is and wait for it to complete, or (in what is probably a better solution) use the Player interface and queue resources to be played.

You'll want to ensure the queue doesn't grow too long (at least to keep memory usage down), but I'll leave that as an exercise to the reader.
Uhh, a canon in itself isn't too hard to generate procedurally, why not do that?

Basically, you want to create a base melody, then copy it down a measure forward. You then, as you are playing it, want to pick a note that is 3, 4, or 5 half steps (srry that I am educated in music theory) above or below it, and copy it down a measure forward.

Basically, if my notes were A, C, E, F (oh god), I could do C, F, A, C and it would look like this:

Where the dashes are rests.

Also, please excuse my crappy explaining...

EDIT: here's an example of a canon created following a similar procedure:
Ok, so there were three problems, all of which I have now fixed:

First, the run command never finished unless it had an exit command, but if that was there, it would start replaying right away. To fix this, I put in the exit command followed by a 12-second delay, and it worked right.

Second, the reason that the items were still queued was that I needed to reload them every time I wanted to play them.

Finally, the clips were longer than I thought (12 seconds of notes followed by 8 seconds of silence instead of just 12 seconds of notes). I trimmed them, and that fixed the lag.

Thanks, everybody!

I did think about generating the notes in the program, but it was simpler and had better sound quality to just play files.
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum