William Matsuoka
  • Introduction
  • Topics
  • Stata
  • Teaching
    • ECON 641L
    • ECON 640L >
      • Econ-Data
  • Blog
  • About

W=M/Stata

Welcome

Composer - Writing Music in Stata

7/17/2016

24 Comments

 
Why Care Block:
To be honest, I'm still trying to figure this one out myself
Yo yo, what’s up my Stata-ites! DJ Control+D is in the house because today we’re talking about making music with Stata.  There’s a big trend in today’s age involving visualizations, but truth be told, visualizations are so last season.  You’ve always been able to see your data, yet have you ever wanted to hear your data?  “That’s absurd, I don’t need that.”

Yeah, you’re probably right... but today, this post is for the slacker: from the undergraduate struggling through their Stata homework, to the professional who’s bored with the day-to-day grind, our new command composer is there to brighten up your day and aid with your ever evolving procrastination techniques.

What is composer?  It’s a way to create music.  How is that music generated?  You write a string of notes that get converted to the midi file format.  Why did we do this?  I’m still asking myself that same question while introspectively reevaluating my life.  Truth be told, I love music.  Not in the traditional sense, but in the very traditional sense – classical music and music theory has always been a passion of mine and this has been a passion project for a long time now.
Picture

​Composer makes music in two separate ways.  You can write songs from the command line and from a dataset.  The command line approach is faster and is especially meant for ad hoc procrastination.  The dataset approach is a more methodical way of creating music and actually allows for multiple tracks and multiple instruments.
​
Take our command line approach:
composer "D, 384D, A, A, B, B, *2A, G, G, F#, F#, E, E, *.25r, *.25Db, 768D" using tt.mid, play replace

​We could have also written this first part as D, D, A, A, B, B, 768A, but it’s good to see the variations of this command.  First we have notes: A, B, C, D, E, F, and G.  Each note can be modified using a # (sharp) or a b (flat) which directly follows the note.

A number before the note shows the duration of the note.  The quarter note takes the value 384 (3*2^7).  This way it can be halved up to seven times with 384 representing a quarter note, 192 representing an eight note, 96 a sixteenth note, and so on.  Rather than specify a value, you may also multiply the note by a number to modify the value of the quarter note. 
​
Any number following your note denotes which octave or pitch your note will take.  You may also change the instrument using a number or a named instrument available in the composer help file.  For instance, say we wrote:
local n1 = "F#,A,E6,D6,E6,D6,A,D6"
local n2 = "E,A,E6,D6,E6,D6,A,D6"
local n3 = "F#,B,E6,D6,E6,D6,B,D6"
local n4 = "G,A,E6,D6,E6,D6,A,D6"
composer "`n1',`n1',`n2',`n2',`n3',`n3',`n4',`n4'" using ls.mid, play replace instrument("Pizzicato") bpm(240)

See if you can identify the song!

Now take our data set approach.  It’s just like the command line approach, except the command now takes sets of three variables: time duration, note, and octave.  Notes can be played simultaneously by adding another track (three more variables).  This way we can build chords and add volume to our masterpieces just like Beethoven.  But unlike Beethoven, we’re not creating classical masterpieces, we’re recreating pop music.  If you weren’t able to pinpoint the song before, here’s the dataset aided version of our previous track.
ls.dta
File Size: 12 kb
File Type: dta
Download File

Picture
ls.dta Contents
use http://www.wmatsuoka.com/uploads/2/1/4/6/21469478/ls.dta, clear
composer pizzdur pizznote pizzoct p1dur p1note p1oct p2dur p2note p2oct voicedur voicenote voiceoct using ls.mid, play replace bpm(120) instrument(Pizzicato Pizzicato Pizzicato Voice)

And there we have it, a fairly easy way to write music in Stata.  Is this interesting?  I hope so, especially if you’ve made it this far.  I’m sure you’re wondering how useful this is though.  Truth be told, this is probably one of the only things on this blog that I haven’t found a general use for but perhaps you can find some sort of practical application and share your thoughts.  There will be one more post about music soon which deals with the actual analysis of music which will use composer.  Until then, good luck and stay creative future Stata maestros! 
composer.ado
File Size: 10 kb
File Type: ado
Download File

composer.sthlp
File Size: 4 kb
File Type: sthlp
Download File

24 Comments
Chris Mims
8/1/2016 09:00:38 am

Is this what you mentioned to me a while back? It might actually be an efficient way to start up chord progressions when I don't have my midi keyboard.

Reply
William Matsuoka
8/1/2016 12:37:39 pm

This is a new development that will handle chord progression with ease (using the data step approach). I wouldn't say it's all that efficient, but I definitely enjoy using it

Reply
Mariana
9/29/2016 09:35:38 pm

Thank you for sharing your passion project! This is lovely. I've studied economics but I love music. While doing my research, I am trying to be an artist like designing and composing the study. Composer command is like an oasis in the desert!

Reply
Will
10/3/2016 08:37:51 am

Hi Mariana,

Thanks for your kind words! I always enjoy the idea of blending art and non-art (for lack of a better word) - I'm glad to hear you're able to incorporate it into your research!

Reply
Miguel
10/2/2016 07:20:46 pm

Hey there. This is such a good ado!
I was trying to run it in Stata 13, though I can't.
Any clues on how to change the ado for it can be possible o run it?

Thanks!

Reply
Will link
10/3/2016 08:34:37 am

Hi Miguel,

While I only support composer for Stata 14.0 and up, there are a few changes you can make that seem to work fine:

Line 102: change "version 14.0" to "version 13.0"

For the next set, replace the lines containing - uisletter() - and - uisdigit() - with:
Line 123:
else if regexm(substr(`"`notes'"', 1, 1), "[0-9]") {
Line 128:
else if regexm(substr(`"`notes'"', 1, 1), "[A-Za-z]") {
Line 137:
if regexm(substr("`notes'", -1, 1), "[0-9]") {

These changes worked on my system.

The ls.dta file is in the Stata 14 format as well. If you'd like to use it, feel free to send me an email and I'll convert it to an older version.

Reply
Magnus
10/4/2016 06:50:03 am

Cool application for Stata! Tried to use it on my Mac and constantly got the error "/bin/bash: tt.mid: command not found". Perhaps you have some clue how to solve it, thanks. BR

Reply
Will
10/4/2016 08:16:31 am

Hi Magnus,

I'll have to borrow a Mac in order to create a permanent fix for this solution. Right now the "play" option doesn't work on Macs, but you can navigate to the file that's written to your current working directory and play it directly from there. You should see the tt.mid file so long as you have write access to your working directory.

Hope that helps!

Reply
Austin Nichols link
11/14/2016 10:32:13 am

Please submit to http://www.stata.com/meeting/baltimore17/ when call is active in 2 or 3 weeks!

Reply
Insik Min
11/15/2016 03:09:30 pm

Dear Sir,
Thanks for your nice work
When I typed the folloing lines, I got the error message.
Could you give us some solutions.
Appreciate
. composer "D, 384D, A, A, B, B, *2A, G, G, F#, F#, E, E, *.25r, *.25Db, 768D" using tt.mid, play replace
file tt.mid could not be opened
fopen(): 603 file could not be opened
midi_write_single(): - function returned error
<istmt>: - function returned error
r(603);

Reply
Will
11/15/2016 04:07:32 pm

Hello there,

First, I'd recommend checking your current working directory to make sure you have permissions to write to it. - composer - writes the output as a midi file to your current working directory and the fopen() error seems to show that the file cannot be written.

You can type - pwd - to look at your current directory if you're using something other than the Stata GUI.

Try changing your directory to your desktop or something like:

cd `c(tmpdir)'

If that doesn't work, perhaps you can send me a screenshot of the error.

Thanks,
Will

Reply
Kim
11/16/2016 12:06:51 pm

Awesome. I am definitely writing a Happy Birthday script to be sent out on office birthdays!

Reply
Lisa
11/20/2016 04:28:28 pm

After typing the line in Stata
- composer "D, 384D, A, A, B, B, *2A, G, G, F#, F#, E, E, *.25r*, .25Db, 768D" using tt.mid, play replace-

how would you listen to it? I tried -br tt.mid- like we do with normal Stata dataset but failed. Thanks

Reply
Will
11/20/2016 08:50:31 pm

The .mid file is a midi file that's created from the program. The composer command is more like - export excel - than the - save - command. The file it creates must be played with an external media player.

For Windows, you should be able to open the file easily using Windows Media Player and for Mac you can use the QuickTime player.

The "play" option under the composer command only works on Windows, but it shells out the file to be run by your default media player.

Hope that helps!

Reply
Simplicity
11/21/2016 07:49:08 am

I am working on Stata 14.2 on OSX. I copied and pasted .ado and files into the Stata's /ado/c folder. Initially when I was running the code I was receiving the following: "/bin/bash: tt.mid: command not found"
Now I am able to run the codes but the product is .mid file and does not play any sound. What could be the problem?
Thanks

Reply
A. D. Hoang
11/30/2016 11:28:32 am

Hi Sir,

I found this awesome and have just tried it out, but I only have Stata 12.0. Could you please provide some instructions? Thank you so much!

Reply
Jesse
12/1/2016 01:47:30 am

This is amazing :D

Reply
ankit link
12/1/2016 03:15:52 am

Someone Sometimes with visits your blog regularly and recommended it in my experience to read as well.

Reply
online paraphrase tool link
2/5/2017 12:38:21 am

This blog updated news is very important to get writing services. Everyone using writers for the best one and result is very important to all of them and thanks for everything given in this blog.

Reply
vadim
9/26/2017 05:28:26 am

ad1game . profit . verifycode: 82171ab0d63675b110e2e611ee8f3b21

Reply
Gospel link
11/26/2017 11:32:27 pm


nice post, keep up with this interesting work. It really is good to know that this topic is being covered also on this web site so cheers for taking time to discuss this!

Reply
Yannik
2/2/2018 06:49:56 am

Hey,

I found this website and it seems to be amazing. Though this doesn't work with my Stata. I am using Stata 15 x64 V5 under Windows and I urgently need it to run! I have to procrastinate. Can you help me out?

Thank you! :)

Reply
Yannik
2/2/2018 07:58:08 am

It works! I figured it out! This is like the most amazing thing I have seen so far! Thank you soooo much! This is procrastinating on a whole new level!!! Thank you Thank you Thank you!!!

Reply
Content post link
2/8/2023 01:39:35 am

I am glad you take pride in what you write. This makes you stand way out from many other writers that push poorly written content.

Reply



Leave a Reply.

    Author

    Will Matsuoka is the creator of W=M/Stata - he likes creativity and simplicity, taking pictures of food, competition, and anything that can be analyzed.

    For more information about this site, check out the teaser above!

    Archives

    July 2016
    June 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015
    September 2015

    Categories

    All
    3ds Max
    Adobe
    API
    Base16
    Base2
    Base64
    Binary
    Bitmap
    Color
    Crawldir
    Email
    Encryption
    Excel
    Exif
    File
    Fileread
    Filewrite
    Fitbit
    Formulas
    Gcmap
    GIMP
    GIS
    Google
    History
    JavaScript
    Location
    Maps
    Mata
    Music
    NFL
    Numtobase26
    Parsing
    Pictures
    Plugins
    Privacy
    Putexcel
    Summary
    Taylor Swift
    Twitter
    Vbscript
    Work
    Xlsx
    XML

    RSS Feed

Proudly powered by Weebly
  • Introduction
  • Topics
  • Stata
  • Teaching
    • ECON 641L
    • ECON 640L >
      • Econ-Data
  • Blog
  • About