{"id":3290,"date":"2021-06-30T17:58:50","date_gmt":"2021-06-30T15:58:50","guid":{"rendered":"https:\/\/www.pschatzmann.ch\/home\/?p=3290"},"modified":"2023-06-30T22:10:08","modified_gmt":"2023-06-30T20:10:08","slug":"pwm-audio-output","status":"publish","type":"post","link":"https:\/\/www.pschatzmann.ch\/home\/2021\/06\/30\/pwm-audio-output\/","title":{"rendered":"PWM Audio Output support for the &#8220;arduino-audio-tools&#8221; Arduino Library"},"content":{"rendered":"<p>In <a href=\"https:\/\/www.pschatzmann.ch\/home\/2021\/05\/19\/back-to-basics-audio-output-on-the-raspberry-pico\/\">one of my last Posts<\/a> I have described an approach how we can implement a simple audio output <strong>using PWM<\/strong> using the Raspberry Pico.<\/p>\n<p>The advantage of this approach is, that we can output audio to almost any GPIO pin, and that it can be used on any other processor which supports PWM.<\/p>\n<p>I have extended my <a href=\"https:\/\/github.com\/pschatzmann\/arduino-audio-tools\">arduino-audio-tools project<\/a> to provide this functionality with a <strong>Stream<\/strong> based and a <strong>callback<\/strong> based <a href=\"https:\/\/pschatzmann.github.io\/arduino-audio-tools\/html\/classaudio__tools_1_1_p_w_m_audio_stream_base.html\">API<\/a>.<\/p>\n<p>The following processors are currently supported:<\/p>\n<ul>\n<li>Raspberry Pico<\/li>\n<li>ESP32<\/li>\n<li>MBED based Arduino Boards (e.g. Nano 33 BLE)<\/li>\n<li>Arduino UNO R4<\/li>\n<\/ul>\n<p>It should not be too difficult to extend the functionality to <strong>support other processors<\/strong> as well: All that is needed is to implement a <strong>timer<\/strong> which is executed in the speed of the audio sampling rate and some PWM logic which sets the <strong>PWM frequency<\/strong> above the audible range. Here is the <a href=\"https:\/\/github.com\/pschatzmann\/arduino-audio-tools\/blob\/main\/src\/AudioPWM\/PWMAudioStreamESP32.h\">ESP32 specific implementation logic<\/a> as an example. I also tried to add an experimental implementation for the Arduino Nano, but unfortunately there is just not enough memory and speed available.<\/p>\n<p>Finally I close with an example <strong>Arduino Sketch<\/strong> which plays an wav file provided as a simple array using the stream API:<\/p>\n<pre><code>#include \"AudioTools.h\"\n#include \"CodecWAV.h\"\n#include \"knghtsng.h\"\n\nusing namespace audio_tools;  \n\n\/\/ MemoryStream -&gt; AudioOutputStream -&gt; WAVDecoder -&gt; PWMAudioStream\nMemoryStream wav(knghtsng_wav, knghtsng_wav_len);\nPWMAudioStream pwm;          \/\/ PWM output \nWAVDecoder decoder(pwm);        \/\/ decode wav to pcm and send it to printer\nAudioOutputStream out(decoder); \/\/ output to decoder\nStreamCopy copier(out, wav);    \/\/ copy in to out\n\nvoid setup(){\n  Serial.begin(115200);\n\n  \/\/ setup pwm output\n  PWMConfig config = pwm.defaultConfig();\n  config.channels = 1;\n  config.sample_rate = 11025;\n  pwm.begin(config);\n}\n\nvoid loop(){\n  if (wav) {\n    copier.copy();\n  } else {\n    \/\/ after we are done we just print some info form the wav file\n    auto info = decoder.audioInfo();\n    LOGI(\"The audio rate from the wav file is %d\", info.sample_rate);\n    LOGI(\"The channels from the wav file is %d\", info.channels);\n    stop();\n  }\n}\n<\/code><\/pre>\n<p>The <a href=\"https:\/\/github.com\/pschatzmann\/arduino-audio-tools\/tree\/main\/examples\/streams-memory_wav-pwm\">full source can be found on Github!<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In one of my last Posts I have described an approach how we can implement a simple audio output using PWM using the Raspberry Pico. The advantage of this approach is, that we can output audio to almost any GPIO pin, and that it can be used on any other [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3308,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[20,22],"tags":[30,44],"class_list":["post-3290","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arduino","category-machine-sound","tag-back-to-basics","tag-uno"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PWM Audio Output support for the &quot;arduino-audio-tools&quot; Arduino Library - Phil Schatzmann<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pschatzmann.ch\/home\/2021\/06\/30\/pwm-audio-output\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PWM Audio Output support for the &quot;arduino-audio-tools&quot; Arduino Library - Phil Schatzmann\" \/>\n<meta property=\"og:description\" content=\"In one of my last Posts I have described an approach how we can implement a simple audio output using PWM using the Raspberry Pico. The advantage of this approach is, that we can output audio to almost any GPIO pin, and that it can be used on any other [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pschatzmann.ch\/home\/2021\/06\/30\/pwm-audio-output\/\" \/>\n<meta property=\"og:site_name\" content=\"Phil Schatzmann\" \/>\n<meta property=\"article:published_time\" content=\"2021-06-30T15:58:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-06-30T20:10:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2021\/06\/sound-waves.png\" \/>\n\t<meta property=\"og:image:width\" content=\"225\" \/>\n\t<meta property=\"og:image:height\" content=\"225\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"pschatzmann\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"pschatzmann\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/06\\\/30\\\/pwm-audio-output\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/06\\\/30\\\/pwm-audio-output\\\/\"},\"author\":{\"name\":\"pschatzmann\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"headline\":\"PWM Audio Output support for the &#8220;arduino-audio-tools&#8221; Arduino Library\",\"datePublished\":\"2021-06-30T15:58:50+00:00\",\"dateModified\":\"2023-06-30T20:10:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/06\\\/30\\\/pwm-audio-output\\\/\"},\"wordCount\":214,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/06\\\/30\\\/pwm-audio-output\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2021\\\/06\\\/sound-waves.png\",\"keywords\":[\"Back to Basics\",\"UNO\"],\"articleSection\":[\"Arduino\",\"Machine Sound\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/06\\\/30\\\/pwm-audio-output\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/06\\\/30\\\/pwm-audio-output\\\/\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/06\\\/30\\\/pwm-audio-output\\\/\",\"name\":\"PWM Audio Output support for the \\\"arduino-audio-tools\\\" Arduino Library - Phil Schatzmann\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/06\\\/30\\\/pwm-audio-output\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/06\\\/30\\\/pwm-audio-output\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2021\\\/06\\\/sound-waves.png\",\"datePublished\":\"2021-06-30T15:58:50+00:00\",\"dateModified\":\"2023-06-30T20:10:08+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/06\\\/30\\\/pwm-audio-output\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/06\\\/30\\\/pwm-audio-output\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/06\\\/30\\\/pwm-audio-output\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2021\\\/06\\\/sound-waves.png\",\"contentUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2021\\\/06\\\/sound-waves.png\",\"width\":225,\"height\":225},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/06\\\/30\\\/pwm-audio-output\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PWM Audio Output support for the &#8220;arduino-audio-tools&#8221; Arduino Library\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#website\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/\",\"name\":\"Phil Schatzmann Consulting\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\",\"name\":\"pschatzmann\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/pschatzmann.png\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/pschatzmann.png\",\"contentUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/pschatzmann.png\",\"width\":305,\"height\":305,\"caption\":\"pschatzmann\"},\"logo\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/pschatzmann.png\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PWM Audio Output support for the \"arduino-audio-tools\" Arduino Library - Phil Schatzmann","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pschatzmann.ch\/home\/2021\/06\/30\/pwm-audio-output\/","og_locale":"en_US","og_type":"article","og_title":"PWM Audio Output support for the \"arduino-audio-tools\" Arduino Library - Phil Schatzmann","og_description":"In one of my last Posts I have described an approach how we can implement a simple audio output using PWM using the Raspberry Pico. The advantage of this approach is, that we can output audio to almost any GPIO pin, and that it can be used on any other [&hellip;]","og_url":"https:\/\/www.pschatzmann.ch\/home\/2021\/06\/30\/pwm-audio-output\/","og_site_name":"Phil Schatzmann","article_published_time":"2021-06-30T15:58:50+00:00","article_modified_time":"2023-06-30T20:10:08+00:00","og_image":[{"width":225,"height":225,"url":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2021\/06\/sound-waves.png","type":"image\/png"}],"author":"pschatzmann","twitter_card":"summary_large_image","twitter_misc":{"Written by":"pschatzmann","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/06\/30\/pwm-audio-output\/#article","isPartOf":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/06\/30\/pwm-audio-output\/"},"author":{"name":"pschatzmann","@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"headline":"PWM Audio Output support for the &#8220;arduino-audio-tools&#8221; Arduino Library","datePublished":"2021-06-30T15:58:50+00:00","dateModified":"2023-06-30T20:10:08+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/06\/30\/pwm-audio-output\/"},"wordCount":214,"commentCount":0,"publisher":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"image":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/06\/30\/pwm-audio-output\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2021\/06\/sound-waves.png","keywords":["Back to Basics","UNO"],"articleSection":["Arduino","Machine Sound"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pschatzmann.ch\/home\/2021\/06\/30\/pwm-audio-output\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/06\/30\/pwm-audio-output\/","url":"https:\/\/www.pschatzmann.ch\/home\/2021\/06\/30\/pwm-audio-output\/","name":"PWM Audio Output support for the \"arduino-audio-tools\" Arduino Library - Phil Schatzmann","isPartOf":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/06\/30\/pwm-audio-output\/#primaryimage"},"image":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/06\/30\/pwm-audio-output\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2021\/06\/sound-waves.png","datePublished":"2021-06-30T15:58:50+00:00","dateModified":"2023-06-30T20:10:08+00:00","breadcrumb":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/06\/30\/pwm-audio-output\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pschatzmann.ch\/home\/2021\/06\/30\/pwm-audio-output\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/06\/30\/pwm-audio-output\/#primaryimage","url":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2021\/06\/sound-waves.png","contentUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2021\/06\/sound-waves.png","width":225,"height":225},{"@type":"BreadcrumbList","@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/06\/30\/pwm-audio-output\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pschatzmann.ch\/home\/"},{"@type":"ListItem","position":2,"name":"PWM Audio Output support for the &#8220;arduino-audio-tools&#8221; Arduino Library"}]},{"@type":"WebSite","@id":"https:\/\/www.pschatzmann.ch\/home\/#website","url":"https:\/\/www.pschatzmann.ch\/home\/","name":"Phil Schatzmann Consulting","description":"","publisher":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pschatzmann.ch\/home\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1","name":"pschatzmann","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/08\/pschatzmann.png","url":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/08\/pschatzmann.png","contentUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/08\/pschatzmann.png","width":305,"height":305,"caption":"pschatzmann"},"logo":{"@id":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/08\/pschatzmann.png"}}]}},"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/3290","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/comments?post=3290"}],"version-history":[{"count":27,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/3290\/revisions"}],"predecessor-version":[{"id":5781,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/3290\/revisions\/5781"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/media\/3308"}],"wp:attachment":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/media?parent=3290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/categories?post=3290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/tags?post=3290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}