{"id":14612,"date":"2017-12-19T22:20:46","date_gmt":"2017-12-20T03:20:46","guid":{"rendered":"http:\/\/scruss.com\/blog\/?p=14612"},"modified":"2017-12-20T13:42:23","modified_gmt":"2017-12-20T18:42:23","slug":"synthesizing-simple-chords-with-sox","status":"publish","type":"post","link":"https:\/\/scruss.com\/blog\/2017\/12\/19\/synthesizing-simple-chords-with-sox\/","title":{"rendered":"Synthesizing simple chords with sox"},"content":{"rendered":"<p><a href=\"http:\/\/sox.sourceforge.net\/\">SoX<\/a> can do almost anything with audio files \u00e2\u20ac\u201d including synthesize audio from scratch. Unfortunately, SoX&#8217;s syntax is more than a bit hard to follow, and the manual page isn&#8217;t the most clear. But there is one example in the manual that gives a glimpse of what SoX can do:<\/p>\n<pre>play -n synth pl G2 pl B2 pl D3 pl G3 pl D4 pl G4 \\ \r\n     delay 0 .05 .1 .15 .2 .25 remix - fade 0 4 .1 norm -1\r\n<\/pre>\n<p>While it plays a nice chord, it&#8217;s not obvious how to make audio files from this process. I have a project coming up that needs a few simple guitar chords, and with much trial and error I got SoX to spit out audio files. Here&#8217;s what I keyed into the shell:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ncat guitar.txt | while read chord foo first third fifth\r\ndo\r\n  echo &quot;$chord&quot; :\r\n  sox -n \\ \r\n    -r 16000 -b 16 &quot;chord-${chord}.wav&quot; \\\r\n    synth pl &quot;$first&quot; pl &quot;$third&quot; pl &quot;$fifth&quot; \\\r\n    delay 0 .05 .1 \\ \r\n    remix - \\ \r\n    fade 0 1 .095 \\ \r\n    norm -1\r\ndone\r\n<\/pre>\n<p>with these lines in the file \u00e2\u20ac\u0153guitar.txt\u00e2\u20ac\u009d<\/p>\n<pre>G   :  G2  B2  D3\r\nC   :  C3  E3  G4\r\nD   :  D3  F#4 A3\r\nF   :  F3  A3  C4\r\nA   :  A3  C#4 E4\r\nE   :  E2  G#3 B3\r\nEm  :  E2  G3  B3\r\n<\/pre>\n<p>How the SoX command line breaks down:<\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li><strong>-n<\/strong> \u00e2\u20ac\u201duse no input file: SoX is going to generate the audio itself<\/li>\n<li><strong>-r 16000 -b 16 &#8220;chord-${chord}.wav&#8221;<\/strong> \u00e2\u20ac\u201d with a sample rate of 16 kHz and 16-bits per sample, write to the output file \u00e2\u20ac\u0153chord-\u00e2\u20ac\u00a6.wav\u00e2\u20ac\u009d<\/li>\n<li><strong>synth pl &#8220;$first&#8221; pl &#8220;$third&#8221; pl &#8220;$fifth&#8221;<\/strong> \u00e2\u20ac\u201dsynthesize three plucked tones read from the file<\/li>\n<li><strong>delay 0 .05 .1<\/strong> \u00e2\u20ac\u201ddelay the second tone 0.05 s after the first and likewise the third after the second. This simulates the striking of guitar strings very slightly apart.<\/li>\n<li><strong>remix &#8211;<\/strong> \u00e2\u20ac\u201dmix the tones in an internal pipe to the output<\/li>\n<li><strong>fade 0 1 .095<\/strong> \u00e2\u20ac\u201dfade the audio smoothly down to nothing in 1 s<\/li>\n<li><strong>norm -1<\/strong> \u00e2\u20ac\u201dnormalize the volume to -1 dB.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>The chords don&#8217;t sound great: they&#8217;re played on only three strings, so they sound very sparse. As my application will be playing these through a tiny MEMS speaker, I don&#8217;t think anyone will notice.<\/p>\n<audio class=\"wp-audio-shortcode\" id=\"audio-14612-1\" preload=\"none\" style=\"width: 100%;\" controls=\"controls\"><source type=\"audio\/wav\" src=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/12\/chord-G.wav?_=1\" \/><a href=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/12\/chord-G.wav\">http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/12\/chord-G.wav<\/a><\/audio>\n<audio class=\"wp-audio-shortcode\" id=\"audio-14612-2\" preload=\"none\" style=\"width: 100%;\" controls=\"controls\"><source type=\"audio\/wav\" src=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/12\/chord-F.wav?_=2\" \/><a href=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/12\/chord-F.wav\">http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/12\/chord-F.wav<\/a><\/audio>\n<audio class=\"wp-audio-shortcode\" id=\"audio-14612-3\" preload=\"none\" style=\"width: 100%;\" controls=\"controls\"><source type=\"audio\/wav\" src=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/12\/chord-Em.wav?_=3\" \/><a href=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/12\/chord-Em.wav\">http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/12\/chord-Em.wav<\/a><\/audio>\n<audio class=\"wp-audio-shortcode\" id=\"audio-14612-4\" preload=\"none\" style=\"width: 100%;\" controls=\"controls\"><source type=\"audio\/wav\" src=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/12\/chord-E.wav?_=4\" \/><a href=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/12\/chord-E.wav\">http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/12\/chord-E.wav<\/a><\/audio>\n<audio class=\"wp-audio-shortcode\" id=\"audio-14612-5\" preload=\"none\" style=\"width: 100%;\" controls=\"controls\"><source type=\"audio\/wav\" src=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/12\/chord-D.wav?_=5\" \/><a href=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/12\/chord-D.wav\">http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/12\/chord-D.wav<\/a><\/audio>\n<audio class=\"wp-audio-shortcode\" id=\"audio-14612-6\" preload=\"none\" style=\"width: 100%;\" controls=\"controls\"><source type=\"audio\/wav\" src=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/12\/chord-C.wav?_=6\" \/><a href=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/12\/chord-C.wav\">http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/12\/chord-C.wav<\/a><\/audio>\n<audio class=\"wp-audio-shortcode\" id=\"audio-14612-7\" preload=\"none\" style=\"width: 100%;\" controls=\"controls\"><source type=\"audio\/wav\" src=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/12\/chord-A.wav?_=7\" \/><a href=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/12\/chord-A.wav\">http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/12\/chord-A.wav<\/a><\/audio>\n<p><strong>Update<\/strong>: well, now I know how to do it, why not do all 36 autoharp strings and make the \u00e2\u20ac\u0153magic ensues\u00e2\u20ac\u009d sound of just about every TV show of my childhood?<\/p>\n<p>Glissando up:<\/p>\n<pre>sox -n -r 48000 -b 16 autoharp-up.wav synth pl \"F2\" pl \"G2\" pl \"C3\" pl \"D3\" pl \"E3\" pl \"F3\" pl \"F#3\" pl \"G3\" pl \"A3\" pl \"A#3\" pl \"B3\" pl \"C4\" pl \"C#4\" pl \"D4\" pl \"D#4\" pl \"E4\" pl \"F4\" pl \"F#4\" pl \"G4\" pl \"G#4\" pl \"A4\" pl \"A#4\" pl \"B4\" pl \"C5\" pl \"C#5\" pl \"D5\" pl \"D#5\" pl \"E5\" pl \"F5\" pl \"F#5\" pl \"G5\" pl \"G#5\" pl \"A5\" pl \"A#5\" pl \"B5\" pl \"C6\" delay 0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 0.55 0.6 0.65 0.7 0.75 0.8 0.85 0.9 0.95 1 1.05 1.1 1.15 1.2 1.25 1.3 1.35 1.4 1.45 1.5 1.55 1.6 1.65 1.7 1.75 remix - fade 0 6 .1 norm -1<\/pre>\n<audio class=\"wp-audio-shortcode\" id=\"audio-14612-8\" preload=\"none\" style=\"width: 100%;\" controls=\"controls\"><source type=\"audio\/wav\" src=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/12\/autoharp-up1648.wav?_=8\" \/><a href=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/12\/autoharp-up1648.wav\">http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/12\/autoharp-up1648.wav<\/a><\/audio>\n<p>Glissando down:<\/p>\n<pre>sox -n -r 48000 -b 16 autoharp-down.wav synth pl \"C6\" pl \"B5\" pl \"A#5\" pl \"A5\" pl \"G#5\" pl \"G5\" pl \"F#5\" pl \"F5\" pl \"E5\" pl \"D#5\" pl \"D5\" pl \"C#5\" pl \"C5\" pl \"B4\" pl \"A#4\" pl \"A4\" pl \"G#4\" pl \"G4\" pl \"F#4\" pl \"F4\" pl \"E4\" pl \"D#4\" pl \"D4\" pl \"C#4\" pl \"C4\" pl \"B3\" pl \"A#3\" pl \"A3\" pl \"G3\" pl \"F#3\" pl \"F3\" pl \"E3\" pl \"D3\" pl \"C3\" pl \"G2\" pl \"F2\" delay 0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 0.55 0.6 0.65 0.7 0.75 0.8 0.85 0.9 0.95 1 1.05 1.1 1.15 1.2 1.25 1.3 1.35 1.4 1.45 1.5 1.55 1.6 1.65 1.7 1.75 remix - fade 0 6 .1 norm -1<\/pre>\n<audio class=\"wp-audio-shortcode\" id=\"audio-14612-9\" preload=\"none\" style=\"width: 100%;\" controls=\"controls\"><source type=\"audio\/wav\" src=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/12\/autoharp-down1648.wav?_=9\" \/><a href=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/12\/autoharp-down1648.wav\">http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/12\/autoharp-down1648.wav<\/a><\/audio>\n<p>Could maybe use some reverb in there for the ultimate nostalgic effect.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SoX can do almost anything with audio files \u00e2\u20ac\u201d including synthesize audio from scratch. Unfortunately, SoX&#8217;s syntax is more than a bit hard to follow, and the manual page isn&#8217;t the most clear. But there is one example in the manual that gives a glimpse of what SoX can do: play -n synth pl G2 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[7],"tags":[1310,1316,2903,270,1487],"class_list":["post-14612","post","type-post","status-publish","format-standard","hentry","category-computers-suck","tag-autoharp","tag-chords","tag-guitar","tag-linux","tag-sox"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pQNZZ-3NG","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/14612","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/comments?post=14612"}],"version-history":[{"count":9,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/14612\/revisions"}],"predecessor-version":[{"id":14626,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/14612\/revisions\/14626"}],"wp:attachment":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/media?parent=14612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/categories?post=14612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/tags?post=14612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}