Can you make music with Mathcad?
I asked myself this question after attending the “Music and Mathematics” exhibition at the L’Agora, a museum in the City of Arts and Sciences here in Valencia, Spain. The City of Arts and Sciences is a futuristic complex of museums and venues, so futuristic that it was used for several locations on Coruscant in the Disney Star Wars series “Andor.”
Due to my involvement in the Mathcad Community Challenge for several years, I was compelled to attend this exhibition. I learned about the musical dice game attributed to Wolfgang Amadeus Mozart. The game generates a minuet based on rolling dice. The minuet has 16 bars. For each bar, there are 11 different musical arrangements, corresponding to the values of rolling dice, from snake eyes (2) to boxcars (12). You put together the bars based on the dice rolls to form a new composition. That’s over 45 quadrillion different possible musical combinations!
Could I replicate this game in Mathcad? I didn’t know, but it sure would be fun to try.
The Preparation
I knew from the Functions tab in Mathcad Prime that you could read from and write to the WAV audio file format. Different musical notes have different frequencies. The notes are played at a certain time for a certain duration. Theoretically, if I could find the original sheet music, I should be able to create each of the 176 WAV files.
I searched online for Mozart’s sheet music. My life became so much easier: I found Kevin Wayne’s page on Stanford’s website for a programming challenge that contains the zip file containing WAV files for piano, flute, clarinet, and even the mbira from Zimbabwe.
Editor’s Note: When you download the files at the button at the top of the blog, you’ll get a .zip with folders of each of the instruments. Inside each folder are the WAV collections, as well as the PTC Mathcad Prime 12 worksheet.
Before writing my worksheet, I had to refresh myself on 3 basic tasks in Mathcad: generating random numbers, reading and writing WAV files, and stacking matrices.
Building the Worksheets
On a Saturday morning, I sat down to create a worksheet that made music in a brute force way. It accomplished the following:
- Generate two random numbers 16 times using a range variable and the rnd() function.
- Use string functions to determine which WAV files to read in.
- Stack the WAV files into a new matrix.
- Create a filename with a random number.
- Write the matrix to the file.
Surprisingly, it worked the first time! I was able to double click on the resulting file and play some Mozart.
However, there were two things I found inelegant about my brute force approach. Upon opening the worksheet, a new minuet was created. To generate a subsequent minuet, I had to click the Recalculate command in the ribbon. I wanted the user to click a button to generate new music. Also, I wanted something better than a random number to generate a unique filename. After all, a random six-digit integer does not guarantee a unique filename.
Second Attempt
After a couple weeks, I decided to tackle the unique filename issue first. I didn’t want a random number; I really wanted a timestamp in the filename. In my other Mathcad work, I found out that there was a time() function. However, it didn’t return the date and time as we know it. It returns the number of seconds since January 1, 1970, also known as the “Unix epoch.”
In searching the PTC Knowledge Base, I found a discussion on PTC Community regarding the time() function. It included a Mathcad 15 worksheet with a collection of programs that provided useful current date information.
I ran the worksheet through Mathcad’s built-in converter. I found a program that returned the date and modified it for my preferred format. I wrote a new worksheet to calculate the time of day using the time() function. I used the Include Worksheet function to read these two worksheets into my new Mozart worksheet. Now it generated a unique filename with a timestamp. One problem solved... or so I thought.
Third Attempt
Now it was time to create a button advanced input control for the user to roll the dice. (By the way, one of my earliest ideas was to have the user roll the dice 16 separate times. I quickly abandoned that.)
It had been a while since I used buttons in Mathcad Prime. I dug up the worksheet I used in my channel video. I had completely forgotten I had written a button to simulate rolling dice in Dungeons and Dragons! Once again, most of the heavy lifting was done for me, this time by myself in the past. This button used the JScript Math.random() function. JScript functions will come up again in this story.
Want more advanced controls guidance?
This resource blog has downloadable worksheets for each control you can apply to your own projects.
Download Use Cases
I needed to figure out how to code a for loop in the button (to roll the dice 16 times) as well as output a matrix as the result. Once again, I searched the PTC Knowledge Base. I used both the Mathcad Help as well as some worksheets on the different advanced input control classes.
The button worked! However, I found that I could not get the filename to update with the timestamp when the user clicked the button. Back to the drawing board.
Final Worksheet (for now)
In one of my original ideas for the worksheet, I wanted the user to click a button twice, once to roll the dice, and then to generate a filename. Maybe I should return to that idea. Then I thought, if JScript has a random number generator, then surely it has a time and date function, right?
Indeed it does. I wrote a button that “got” the date, extracted each part of the date and time, and output a string for a filename. I even wrote my own function that turned the month, day, hour, minute, and seconds as two digits with a leading zero when necessary. (I am not a programmer, so I was quite proud of myself for that.)
With the two buttons, my worksheet was complete. I was happy.
Editor’s Note: Mathcad Prime’s advanced controls scripting languages are VBScript, JScript, and Python. JScript has a common origin with JavaScript, and Dave looked up JavaScript functions for his project, which happened to work with JScript. However, JavaScript and JScript are not exactly synonymous with their feature sets. Your own mileage may vary.
What Have I Learned?
If you want to learn how to use Mathcad Prime, it helps to work on projects that mean something to you. Formulate your own requirements and figure out how to implement them. Most of all, have fun.
There are a lot of resources available from PTC to help you with your projects in Mathcad Prime. These include the Knowledge Base, the Help, and PTC Community. Use them!
Also, I’ve learned it pays to keep your eyes open when working in Mathcad Prime. There are a bunch of commands and functions that I had come across in the past, but never used myself. Pulling together all those bits and pieces made the worksheet possible.
Do you want to play around with making some Mozart music with Mathcad? Download the final worksheet, with the instrument files, available at the top of this article. I also captured my journey in a YouTube playlist if you want to watch the creation of the worksheets.
I hope this inspires you to do some neat and interesting projects in Mathcad Prime!
Up Next
Subscribe to the Mathcad Minute
Get on the Mathcad Minute mailing list for even more curated stories to your email inbox. It’s free.
Subscribe Today