{"id":98,"date":"2014-08-03T23:52:43","date_gmt":"2014-08-03T23:52:43","guid":{"rendered":"http:\/\/www.jamesrising.net\/blog\/?p=98"},"modified":"2014-08-04T00:01:00","modified_gmt":"2014-08-04T00:01:00","slug":"python-soundtouch-wrapper","status":"publish","type":"post","link":"http:\/\/www.jamesrising.net\/blog\/?p=98","title":{"rendered":"Python SoundTouch Wrapper"},"content":{"rendered":"<p>SoundTouch is a very useful set of audio manipulation tools, with three powerful features:<\/p>\n<ul>\n<li>Adjusting the pitch of a segment, without changing its tempo<\/li>\n<li>Adjusting the tempo of a segment, without changing its pitch<\/li>\n<li>Detecting the tempo of a segment, using beat detection<\/li>\n<\/ul>\n<p>I used SoundTouch when I was developing <a href=\"cantovario.com\">CantoVario<\/a>\u00a0under the direction\u00a0of Diana Dabby and using her algorithms for generating new music from existing music, using Lorenz attractors. \u00a0SoundTouch is a C++ library, but CantoVario was in python, so I built a wrapper for it.<\/p>\n<p>Now you can use it too! \u00a0<a href=\"https:\/\/github.com\/jrising\/pysoundtouch\">PySoundTouch<\/a>, a python wrapper for the SoundTouch library\u00a0is available on github! \u00a0It&#8217;s easy to use, especially with the super-cool AudioReader abstraction that I made with it.<\/p>\n<p>AudioReader provides a single interface to any audio file (currently MP3, WAV, AIF, and AU files are supported). \u00a0Here&#8217;s an example of using AudioReader with the SoundTouch library:<\/p>\n<pre class=\"brush: python; collapse: false; title: ; wrap-lines: false; notranslate\" title=\"\">\r\n# Open the file and convert it to have SoundTouch's required 2-byte samples\r\nreader = AudioReader.open(srcpath)\r\nreader2 = ConvertReader(reader, set_raw_width=2)\r\n\r\n# Create the SoundTouch object and set the given shift\r\nst = soundtouch.SoundTouch(reader2.sampling_rate(), reader2.channels())\r\nst.set_pitch_shift(shift)\r\n\r\n# Create the .WAV file to write the result to\r\nwriter = wave.open(dstpath, 'w')\r\nwriter.setnchannels(reader2.channels())\r\nwriter.setframerate(reader2.sampling_rate())\r\nwriter.setsampwidth(reader2.raw_width())\r\n\r\n# Read values and feed them into SoundTouch\r\nwhile True:\r\n    data = reader2.raw_read()\r\n    if not data:\r\n        break\r\n\r\n    print len(data)\r\n    st.put_samples(data)\r\n\r\n    while st.ready_count() &gt; 0:\r\n        writer.writeframes(st.get_samples(11025))\r\n\r\n# Flush any remaining samples\r\nwaiting = st.waiting_count()\r\nready = st.ready_count()\r\nflushed = &quot;&quot;\r\n\r\n# Add silence until another chunk is pushed out\r\nsilence = array('h', &#x5B;0] * 64)\r\nwhile st.ready_count() == ready:\r\n    st.put_samples(silence)\r\n\r\n# Get all of the additional samples\r\nwhile st.ready_count() &gt; 0:\r\n    flushed += st.get_samples(4000)\r\n\r\nst.clear()\r\n\r\nif len(flushed) &gt; 2 * reader2.getnchannels() * waiting:\r\n    flushed = flushed&#x5B;0:(2 * reader2.getnchannels() * waiting)]\r\n\r\nwriter.writeframes(flushed)\r\n\r\n# Clean up\r\nwriter.close()\r\nreader2.close()\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>SoundTouch is a very useful set of audio manipulation tools, with three powerful features: Adjusting the pitch of a segment, without changing its tempo Adjusting the tempo of a segment, without changing its pitch Detecting the tempo of a segment, using beat detection I used SoundTouch when I was developing CantoVario\u00a0under the direction\u00a0of Diana Dabby &hellip; <a href=\"http:\/\/www.jamesrising.net\/blog\/?p=98\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Python SoundTouch Wrapper<\/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":{"footnotes":""},"categories":[16],"tags":[10,14,15],"class_list":["post-98","post","type-post","status-publish","format-standard","hentry","category-software","tag-ifttt","tag-james-rising","tag-pro"],"_links":{"self":[{"href":"http:\/\/www.jamesrising.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/98","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.jamesrising.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.jamesrising.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.jamesrising.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.jamesrising.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=98"}],"version-history":[{"count":2,"href":"http:\/\/www.jamesrising.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/98\/revisions"}],"predecessor-version":[{"id":100,"href":"http:\/\/www.jamesrising.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/98\/revisions\/100"}],"wp:attachment":[{"href":"http:\/\/www.jamesrising.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=98"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.jamesrising.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=98"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.jamesrising.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=98"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}