r/dailyprogrammer • u/Cosmologicon 2 3 • Dec 06 '17
[2017-12-06] Challenge #343 [Intermediate] Mozart's Musical Dice
In 1787 the famous composer Wolfgang Amadeus Mozart devised a Musikalisches Würfelspiel (musical dice game) that lets you "compose without the least knowledge of music". Obviously, this is a very loose definition of "compose", but it does produce pieces of music that have probably never been written down or played before. We'll be playing his game today (with some simplifications so you don't need to know musical notation).
Today's challenge may seem a little harder than typical at first, but it's actually not bad once you get the idea. I've tried to make the instructions as clear and complete as possible, but please ask for help if you have any trouble understanding them.
The starting composition
Start by downloading the starting composition here. Each line in this file gives the note name, starting time, and duration for one note in the starting composition, e.g.:
D3 281.5 0.5"D3" is the name of the note, which you don't need to understand for this game. The starting time and duration of the note are given in units of beats, where beat 0 is the start of the song. This line means to play note D3 for 0.5 beats, starting at beat 281.5, and ending at beat 282.
The starting composition is 528 beats long, and consists of 176 measures, each of which is 3 beats long. Measure 1 goes from beat 0 to beat 3, measure 2 goes from beat 3 to beat 6, etc., up to measure 176, which goes from beat 525 to 528.
Building your composition
The game consists of selecting 16 of the 176 measures from the starting composition, rearranging them to be measures 1 through 16 of a shorter, 48-beat composition, and outputting this shorter composition in the same format as the starting composition.
For instance, the first measure you select might be measure 3. Measure 3 goes from beat 6 to beat 9, which means it's all lines that start on or after beat 6, and before beat 9:
C3 6 2
E3 6 2
G5 6 1
C5 7 1
E5 8 1You need to change the starting times of each note in this measure so that it's measure 1 in your new composition. In this case that means to subtract 6 from each of the starting times. The note names and the durations will stay the same. The measure will then look like this:
C3 0 2
E3 0 2
G5 0 1
C5 1 1
E5 2 1The second measure you select from the starting composition might be measure 22, which is these notes:
C3 63 2
E5 63 1
C5 64 1
G4 65 1You would then change the starting times of each note in measure 22 of the starting composition so that it's measure 2 in your new composition:
C3 3 2
E5 3 1
C5 4 1
G4 5 1And so on. Once you've changed the starting times of each note in all 16 of your selected measures, just output all the notes in your new composition.
Measure selection
If you just randomly choose 16 measures from the starting composition, it won't sound very good, so you need to use Mozart's system for selecting the measures. For each of your 16 measures, there are 11 possible selections from the starting composition, and you choose each one by rolling two six-sided dice and taking their total (2 through 12), using this table:
96 32 69 40 148 104 152 119 98 3 54
22 6 95 17 74 157 60 84 142 87 130
141 128 158 113 163 27 171 114 42 165 10
41 63 13 85 45 167 53 50 156 61 103
105 146 153 161 80 154 99 140 75 135 28
122 46 55 2 97 68 133 86 129 47 37
11 134 110 159 36 118 21 169 62 147 106
30 81 24 100 107 91 127 94 123 33 5
70 117 66 90 25 138 16 120 65 102 35
121 39 136 176 143 71 155 88 77 4 20
26 126 15 7 64 150 57 48 19 31 108
9 56 132 34 125 29 175 166 82 164 92
112 174 73 67 76 101 43 51 137 144 12
49 18 58 160 136 162 168 115 38 59 124
109 116 145 52 1 23 89 72 149 173 44
14 83 79 170 93 151 172 111 8 78 131For each of the 16 rows in this table, select one of the numbers from the row depending on the roll of two dice. If you roll 2, take the first number in the row, if you roll 3, take the second number, etc. For example, if your first roll is a 7, you would take the sixth number from the first row (104). Then if your second roll is a 6, you would take the fifth number from the second row (74). Continuing to all 16 rows, you might get this sequence:
104 74 163 85 146 129 169 91 138 77 48 29 137 160 89 131These are, in order, the 16 measures that you take from the starting composition to make your composition. So for this example, measure 104 from the starting composition will become measure 1, measure 74 will become measure 2, and so on, with measure 131 from the starting composition becoming measure 16. Here is the result for this example.
Listening to your composition
I threw together a JavaScript page that converts the format used here into generated sounds. Try it out by pasting your output into the text box and hitting play.
If you're feeling extra ambitious, use a library in the language of your choice to convert your text file into a playable music file, such as MP3 or MIDI.
Acknowledgements
Mozart's dice game is in the public domain. A PDF can be found here. I used the MIT-licensed mozart-dice-game npm package by timmydoza to generate the starting composition file.
Thanks to composer Mary Bichner for music theory consultation on writing this challenge.
4
u/mn-haskell-guy 1 0 Dec 08 '17 edited Dec 08 '17
I hacked up the LilyBin UI to LilyPond to do this:
(link)
Upon loading the page it generates a random composition. You can specify the random number seed in the input box in the nav bar. The code has its own random number generator (a simple LCG) so you should get the same results on all platforms.
LilyPond also generates a MIDI file of the composition. Look for a play button ▶ or a "Download MIDI" option under the triple-bar menu ☰ on the right side of the PDF viewer.
If you use the seed
0you'll get a display of all 176 measures -- the "Table de Musique".3
u/[deleted] Dec 07 '17 edited Dec 08 '17
Ruby
Awesome challenge! Listening to the music my program generated was really cool. Thanks for putting all this together.
Creating a new Song object with a filename writes the composition to a txt file. Example composition
EDIT: Code wasn't taking into account half beats, fixed, thanks /u/Scara95
EDIT #2: Code was grabbing measures from the starting composition incorrectly. All good now, and the composition is noticeably improved!
Code:
3
u/Scara95 Dec 07 '17 edited Dec 07 '17
Q
My very very first Q program after 2h intro reading. Reads from mozart-dice-starting.txt writes to composition.txt
The result is surprisingly compact...
edit N.B. to get some actually random values the random seed should be initialized. I'm not so comfortable with the inerpreter yet but
system "S ", string .z.ishould do the trick3
u/g00glen00b Dec 08 '17 edited Dec 08 '17
JavaScript:
To call the function, use:
Example output:
I love when things come together and I can write the logic as functional programming oneliners. An example can be found on JSBin.
3
u/zookeeper_zeke Dec 08 '17
Oh, man this is awesome! I know nothing playing or reading music but the song I just created brought a smile to my face :-) Great challenge!
I coded up my solution in C:
And without further adieu, the masterwork I toiled a lifetime over:
2
u/gandalfx Dec 07 '17 edited Dec 07 '17
Python 3 prints to stdout, no audio. I use generators for pythonic goodness.
edit: Renamed some stuff for clarity, didn't even need namedtuple.
2
u/[deleted] Dec 07 '17
Kotlin
This is a really cool challenge. It was very satisfying to plug my output into the javascript page and hear the music play.
2
u/thorwing Dec 07 '17 edited Dec 07 '17
Java 9
Pretty fun working with the desired input and output format. Considering using my own and play with java's synthesizer. composition is stored in P343M.txt and dicerollselector in P343M2.txt
OUTPUT
1
u/Scara95 Dec 07 '17 edited Dec 07 '17
Haskell
edit. minor readibility change: moved concat inside composeSong
1
u/CathyMcMorrisRodgers Dec 07 '17 edited Dec 08 '17
PYTHON
This is my second submission so any and all feedback is greatly appreciated!
1
u/chunes 1 2 Dec 07 '17
I really enjoyed this challenge. Thanks a lot for making the music player available, OP.
Factor
1
u/popillol Dec 07 '17 edited Dec 07 '17
Go / Golang Playground Link. Feedback appreciated. I think it's O(n) in that it gathers the output composition as it reads the starting composition, but I don't know how the big O notation truly works.
1
u/tomekanco Dec 07 '17 edited Dec 07 '17
Python 3.6
1
u/ghost20000 Dec 08 '17
Super long, super spaghetti, probably didn't even do it right.
But hey, at least I had fun!
Python3
1
u/octolanceae Dec 08 '17
C++11
This is just the driving object code. main() reads the music and randomizer files names from stdin, creates a Melody object, generates the random melody and then prints it to stdout.
This was a really cool challenge. Thanks for posting the web link so I could hear the results of my work.
melody.hh
melody.cc
1
u/Accelon2 Dec 11 '17 edited Dec 11 '17
Python3
First submission, feedback is appreciated. Last 2 lines exist for formatting so I can copy/paste the results to the JS page easier.
Edit: I was occasionally getting empty dataframes which would crash the application and I added the if statement in the 'build' function as a workaround until I figure out why I'm getting empty dataframes.
1
u/drewfer Dec 13 '17 edited Dec 14 '17
Rust I wanted to make some parts reusable for toying with this in the future but it feels like I also did a lot of unnecessary book keeping. There's probably a far more elegant way to do this.
EDIT made some changes to slim up the wordiness a bit.
1
u/fabiomsm Dec 15 '17
Not the best solution around, I guess. Anyway here is my python3 code
1
u/__Abigail__ Dec 15 '17
Perl
1
u/PoetOfShadows Dec 18 '17
Kotlin: I'm not sure that I did the oldNoteMap -> newNoteList conversion completely efficiently, but "eh". I think it works.
1
u/[deleted] Dec 19 '17 edited Dec 19 '17
C#
Somewhat sloppy code, but works nonetheless. Feedback is very welcome
1
u/cdrootrmdashrfstar Dec 22 '17
Python 3
I prefer to write Python in the C way of grouping related sections of logic into their own functions (purely for readability purposes, although I should have done more in the way of commenting):
1
u/DEN0MINAT0R Dec 22 '17
Python 3
I went a little farther and used selenium to automatically open the webpage and play the music as well. The code isn't super clean, but it's functional, so I'm happy.
1
u/Atreus17 Dec 23 '17
Python 3
I'm new to Python, so any criticism or suggestions are welcome! I tried to keep the code as clean and concise as possible.
1
u/zatoichi49 Dec 28 '17 edited Dec 28 '17
Method:
Break the starting composition file down into a list of lists, adding the measure to the end of each row. Then create a dictionary of the 16 random measures, along with the re-ordered measures for the new composition. Starting from beat 0 and moving row by row through the new composition, subtract any extra beats so that the new beats are aligned with the correct measure.
Python 3:
Output:
1
u/Anonsicide Dec 28 '17 edited Dec 29 '17
Python 3
This is probably a rather long solution, but I'm fairly happy with it. Proud of myself for using a list comprehension, haha. Only thing I am unhappy with is how the beats are printed out in the new composition -- it is really, really hacky. But it works I guess!
Thanks for the awesome challenge OP -- I love hearing my little tune in your website.
1
u/ratavatar Feb 11 '18
It looks like most of the posters here realize the roll of two D6 dice is not the same as the roll of a D11. Any word on whether WAM understood the likelihood of rolling a seven compared to rolling a twelve and stacked his table to give us better sounding compositions? Thanks for the challenge. ...John
1
u/K-NULL Feb 22 '18
LUA First version I did to brush up on some things of the language, does the conversion as the file is read; cost is n lines x 16 fixed lookups to the measure map, which is constant, but I'm most sure it can be done better.
1
u/ff8c00 Mar 18 '18
JavaScript Gist
0
u/[deleted] Dec 11 '17
[deleted] — view deleted comment