{"id":2878,"date":"2016-12-27T11:15:45","date_gmt":"2016-12-27T11:15:45","guid":{"rendered":"http:\/\/nayrb.org\/~blog\/?p=2878"},"modified":"2016-12-27T06:19:49","modified_gmt":"2016-12-27T06:19:49","slug":"if-you-wish-to-make-a-song-from-scratch-you-must-first-invent-the-universe","status":"publish","type":"post","link":"http:\/\/nayrb.org\/~blog\/2016\/12\/27\/if-you-wish-to-make-a-song-from-scratch-you-must-first-invent-the-universe\/","title":{"rendered":"If you wish to make a song from scratch, you must first Invent the Universe&#8230;"},"content":{"rendered":"<p>To write some music, you must first invent some instruments. To do this, one might start with a simple sine wave, then do modulations and superpositions to make various &#8216;instruments&#8217;.<\/p>\n<p>To this end, I did a little bit of research (thanks, <a href=\"https:\/\/soledadpenades.com\/2009\/10\/29\/fastest-way-to-generate-wav-files-in-python-using-the-wave-module\/\" target=\"_blank\">soledadpeandes<\/a>!), and put\/cribbed together some python code to make arbitrary .wav files:<br \/>\n<code># Written 2016-12-26, with special thanks to:<br \/>\n# https:\/\/soledadpenades.com\/2009\/10\/29\/fastest-way-to-generate-wav-files-in-python-using-the-wave-module\/<\/p>\n<p>import wave<br \/>\nimport struct<br \/>\nimport math<\/p>\n<p>SAMPLING_RATE = 44100<br \/>\nWAV_FILE_LEN = SAMPLING_RATE * 1 # 44.1KHz sampling rate, 5 seconds<br \/>\nMAX_AMP = 32767<br \/>\nCORR_FACTOR = 10<br \/>\nSIN_WAV_FREQ = 100 * CORR_FACTOR # Sine wave frequency, in Hz*10 for some reason, 1000 gives 100Hz<\/p>\n<p>output_file = wave.open('test.wav', 'w')<br \/>\noutput_file.setparams((2, 2, 44100, 0, 'NONE', 'not compressed'))<\/p>\n<p>for i in range (0,WAV_FILE_LEN):<\/p>\n<p style=\"padding-left: 30px;\">\ndata = MAX_AMP*math.sin(i*float(SIN_WAV_FREQ)\/float(SAMPLING_RATE)\/(math.pi\/float(2)))<br \/>\nprint data<br \/>\npacked_data = struct.pack('h', data)<br \/>\noutput_file.writeframes(packed_data)<br \/>\noutput_file.writeframes(packed_data)<\/p>\n<p>output_file.close<\/code><\/p>\n<p>For those who are curious, this generates a 100Hz sine wave: <!--[if lt IE 9]><script>document.createElement('audio');<\/script><![endif]-->\n<audio class=\"wp-audio-shortcode\" id=\"audio-2878-1\" preload=\"none\" style=\"width: 100%;\" controls=\"controls\"><source type=\"audio\/wav\" src=\"http:\/\/nayrb.org\/~blog\/wp-content\/uploads\/2016\/12\/test_100Hz_works.wav?_=1\" \/><a href=\"http:\/\/nayrb.org\/~blog\/wp-content\/uploads\/2016\/12\/test_100Hz_works.wav\">http:\/\/nayrb.org\/~blog\/wp-content\/uploads\/2016\/12\/test_100Hz_works.wav<\/a><\/audio>.<\/p>\n<p>Next up, some experimentation with different pitches, perhaps different timbres.  Stay tuned*!<\/p>\n<p>*Also 100Hz.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To write some music, you must first invent some instruments. To do this, one might start with a simple sine wave, then do modulations and superpositions to make various &#8216;instruments&#8217;. To this end, I did a little bit of research (thanks, soledadpeandes!), and put\/cribbed together some python code to make arbitrary .wav files: # Written &hellip; <a href=\"http:\/\/nayrb.org\/~blog\/2016\/12\/27\/if-you-wish-to-make-a-song-from-scratch-you-must-first-invent-the-universe\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">If you wish to make a song from scratch, you must first Invent the Universe&#8230;<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[38,24,30],"tags":[],"_links":{"self":[{"href":"http:\/\/nayrb.org\/~blog\/wp-json\/wp\/v2\/posts\/2878"}],"collection":[{"href":"http:\/\/nayrb.org\/~blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nayrb.org\/~blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nayrb.org\/~blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/nayrb.org\/~blog\/wp-json\/wp\/v2\/comments?post=2878"}],"version-history":[{"count":8,"href":"http:\/\/nayrb.org\/~blog\/wp-json\/wp\/v2\/posts\/2878\/revisions"}],"predecessor-version":[{"id":2888,"href":"http:\/\/nayrb.org\/~blog\/wp-json\/wp\/v2\/posts\/2878\/revisions\/2888"}],"wp:attachment":[{"href":"http:\/\/nayrb.org\/~blog\/wp-json\/wp\/v2\/media?parent=2878"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nayrb.org\/~blog\/wp-json\/wp\/v2\/categories?post=2878"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nayrb.org\/~blog\/wp-json\/wp\/v2\/tags?post=2878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}