{"id":2972,"date":"2021-04-25T19:11:30","date_gmt":"2021-04-25T17:11:30","guid":{"rendered":"https:\/\/www.pschatzmann.ch\/home\/?p=2972"},"modified":"2024-03-21T07:58:49","modified_gmt":"2024-03-21T06:58:49","slug":"esp32-sound-processing-library","status":"publish","type":"post","link":"https:\/\/www.pschatzmann.ch\/home\/2021\/04\/25\/esp32-sound-processing-library\/","title":{"rendered":"Bluetooth A2DP &#8211; Streaming from an Analog Microphone"},"content":{"rendered":"<p>In my <a href=\"https:\/\/github.com\/pschatzmann\/ESP32-A2DP\">Arduino ESP32-A2DP library<\/a> I am providing some very simple examples that show how to transform the ESP32 into a A2DP source and transmit sound to a Bluetooth Sink (e.g. some Bluetooth Speakers).<\/p>\n<p>I had quite some questions on how to do this with files, microphones and I2S as input. So I started a <a href=\"https:\/\/github.com\/pschatzmann\/arduino-audio-tools\">small sister project which provides some additional sound tools<\/a>. I plan to provide plenty of additional examples for some more advances scenarios.<\/p>\n<h1>Analog Microphones<\/h1>\n<h2>The Sketch<\/h2>\n<p>Here is an first example sketch that you can use with an analog microphone as sound source:<\/p>\n<pre><code>#include \"Arduino.h\"\n#include \"BluetoothA2DPSource.h\"\n#include \"AudioTools.h\"\n\nusing namespace audio_tools;  \n\n\/**\n * @brief We use a mcp6022 analog microphone as input and send the data to A2DP\n *\/ \n\nADC adc;\nBluetoothA2DPSource a2dp_source;\n\/\/ The data has a center of around 26427, so we we need to shift it down to bring the center to 0\nFilterScaler&lt;int16_t&gt; scaler(1.0, -26427, 32700 );\n\n\/\/ callback used by A2DP to provide the sound data\nint32_t get_sound_data(Channels* data, int32_t len) {\n    arrayOf2int16_t *data_arrays = (arrayOf2int16_t *) data;\n   \/\/ the ADC provides data in 16 bits\n    size_t result_len = adc.read(data_arrays, len);   \n    scaler.process(data_arrays, result_len);\n    return result_len;\n}\n\n\/\/ Arduino Setup\nvoid setup(void) {\n  Serial.begin(115200);\n\n  \/\/ start i2s input with default configuration\n  Serial.println(\"starting I2S-ADC...\");\n  adc.begin(adc.defaultConfig());\n\n  \/\/ start the bluetooth\n  Serial.println(\"starting A2DP...\");\n  a2dp_source.start(\"MyMusic\", get_sound_data);  \n}\n\n\/\/ Arduino loop - repeated processing \nvoid loop() {\n}\n<\/code><\/pre>\n<p>I am using the built in ESP32 analog sampling functionality which is available using I2S. For this I provide the <a href=\"https:\/\/pschatzmann.github.io\/arduino-audio-tools\/html\/classsound__tools_1_1_a_d_c.html\">ADC class<\/a> to make it simple to use. The data from the microphone was centered around 26427, so we need to reduce all values by this. This is done with the help of the <a href=\"https:\/\/pschatzmann.github.io\/arduino-audio-tools\/html\/classsound__tools_1_1_filter_scaler.html\">FilterScaler<\/a>. You could also amplify the sound (make the sound louder) by using a factor > 1.0<\/p>\n<h2>The Hardware<\/h2>\n<p>To test it I am using the tiny MCP6022 microphone module.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/pschatzmann.github.io\/Resources\/img\/mcp6022.jpeg\" alt=\"MCP6022\" \/><br \/>\n<img decoding=\"async\" src=\"https:\/\/pschatzmann.github.io\/Resources\/img\/mcp6022-1.jpeg\" alt=\"MCP6022\" \/><\/p>\n<p>The MCP6022 is a analaog microphone which operates at 3.3 V<\/p>\n<h2>Pin Assignments<\/h2>\n<table>\n<thead>\n<tr>\n<th>MCP6022<\/th>\n<th>ESP32<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>VCC<\/td>\n<td>3.3<\/td>\n<\/tr>\n<tr>\n<td>GND<\/td>\n<td>GND<\/td>\n<\/tr>\n<tr>\n<td>OUT<\/td>\n<td>GPIO34<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Source Code<\/h2>\n<p>Both the <a href=\"https:\/\/github.com\/pschatzmann\/arduino-audio-tools\">project<\/a> and the <a href=\"https:\/\/github.com\/pschatzmann\/arduino-audio-tools\/blob\/main\/examples\/examples-basic-api\/base-adc-a2dp\/base-adc-a2dp.ino\">example<\/a> can be found on Github.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my Arduino ESP32-A2DP library I am providing some very simple examples that show how to transform the ESP32 into a A2DP source and transmit sound to a Bluetooth Sink (e.g. some Bluetooth Speakers). I had quite some questions on how to do this with files, microphones and I2S as [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2988,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","_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":[46],"class_list":["post-2972","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arduino","category-machine-sound","tag-communications"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Bluetooth A2DP - Streaming from an Analog Microphone - 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\/04\/25\/esp32-sound-processing-library\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Bluetooth A2DP - Streaming from an Analog Microphone - Phil Schatzmann\" \/>\n<meta property=\"og:description\" content=\"In my Arduino ESP32-A2DP library I am providing some very simple examples that show how to transform the ESP32 into a A2DP source and transmit sound to a Bluetooth Sink (e.g. some Bluetooth Speakers). I had quite some questions on how to do this with files, microphones and I2S as [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pschatzmann.ch\/home\/2021\/04\/25\/esp32-sound-processing-library\/\" \/>\n<meta property=\"og:site_name\" content=\"Phil Schatzmann\" \/>\n<meta property=\"article:published_time\" content=\"2021-04-25T17:11:30+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-21T06:58:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2021\/04\/mcp6022.jpeg\" \/>\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\/jpeg\" \/>\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\\\/04\\\/25\\\/esp32-sound-processing-library\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/04\\\/25\\\/esp32-sound-processing-library\\\/\"},\"author\":{\"name\":\"pschatzmann\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"headline\":\"Bluetooth A2DP &#8211; Streaming from an Analog Microphone\",\"datePublished\":\"2021-04-25T17:11:30+00:00\",\"dateModified\":\"2024-03-21T06:58:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/04\\\/25\\\/esp32-sound-processing-library\\\/\"},\"wordCount\":226,\"commentCount\":10,\"publisher\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/04\\\/25\\\/esp32-sound-processing-library\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2021\\\/04\\\/mcp6022.jpeg\",\"keywords\":[\"Communications\"],\"articleSection\":[\"Arduino\",\"Machine Sound\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/04\\\/25\\\/esp32-sound-processing-library\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/04\\\/25\\\/esp32-sound-processing-library\\\/\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/04\\\/25\\\/esp32-sound-processing-library\\\/\",\"name\":\"Bluetooth A2DP - Streaming from an Analog Microphone - Phil Schatzmann\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/04\\\/25\\\/esp32-sound-processing-library\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/04\\\/25\\\/esp32-sound-processing-library\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2021\\\/04\\\/mcp6022.jpeg\",\"datePublished\":\"2021-04-25T17:11:30+00:00\",\"dateModified\":\"2024-03-21T06:58:49+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/04\\\/25\\\/esp32-sound-processing-library\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/04\\\/25\\\/esp32-sound-processing-library\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/04\\\/25\\\/esp32-sound-processing-library\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2021\\\/04\\\/mcp6022.jpeg\",\"contentUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2021\\\/04\\\/mcp6022.jpeg\",\"width\":225,\"height\":225},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/04\\\/25\\\/esp32-sound-processing-library\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Bluetooth A2DP &#8211; Streaming from an Analog Microphone\"}]},{\"@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":"Bluetooth A2DP - Streaming from an Analog Microphone - 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\/04\/25\/esp32-sound-processing-library\/","og_locale":"en_US","og_type":"article","og_title":"Bluetooth A2DP - Streaming from an Analog Microphone - Phil Schatzmann","og_description":"In my Arduino ESP32-A2DP library I am providing some very simple examples that show how to transform the ESP32 into a A2DP source and transmit sound to a Bluetooth Sink (e.g. some Bluetooth Speakers). I had quite some questions on how to do this with files, microphones and I2S as [&hellip;]","og_url":"https:\/\/www.pschatzmann.ch\/home\/2021\/04\/25\/esp32-sound-processing-library\/","og_site_name":"Phil Schatzmann","article_published_time":"2021-04-25T17:11:30+00:00","article_modified_time":"2024-03-21T06:58:49+00:00","og_image":[{"width":225,"height":225,"url":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2021\/04\/mcp6022.jpeg","type":"image\/jpeg"}],"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\/04\/25\/esp32-sound-processing-library\/#article","isPartOf":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/04\/25\/esp32-sound-processing-library\/"},"author":{"name":"pschatzmann","@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"headline":"Bluetooth A2DP &#8211; Streaming from an Analog Microphone","datePublished":"2021-04-25T17:11:30+00:00","dateModified":"2024-03-21T06:58:49+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/04\/25\/esp32-sound-processing-library\/"},"wordCount":226,"commentCount":10,"publisher":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"image":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/04\/25\/esp32-sound-processing-library\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2021\/04\/mcp6022.jpeg","keywords":["Communications"],"articleSection":["Arduino","Machine Sound"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pschatzmann.ch\/home\/2021\/04\/25\/esp32-sound-processing-library\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/04\/25\/esp32-sound-processing-library\/","url":"https:\/\/www.pschatzmann.ch\/home\/2021\/04\/25\/esp32-sound-processing-library\/","name":"Bluetooth A2DP - Streaming from an Analog Microphone - Phil Schatzmann","isPartOf":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/04\/25\/esp32-sound-processing-library\/#primaryimage"},"image":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/04\/25\/esp32-sound-processing-library\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2021\/04\/mcp6022.jpeg","datePublished":"2021-04-25T17:11:30+00:00","dateModified":"2024-03-21T06:58:49+00:00","breadcrumb":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/04\/25\/esp32-sound-processing-library\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pschatzmann.ch\/home\/2021\/04\/25\/esp32-sound-processing-library\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/04\/25\/esp32-sound-processing-library\/#primaryimage","url":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2021\/04\/mcp6022.jpeg","contentUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2021\/04\/mcp6022.jpeg","width":225,"height":225},{"@type":"BreadcrumbList","@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/04\/25\/esp32-sound-processing-library\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pschatzmann.ch\/home\/"},{"@type":"ListItem","position":2,"name":"Bluetooth A2DP &#8211; Streaming from an Analog Microphone"}]},{"@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\/2972","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=2972"}],"version-history":[{"count":37,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/2972\/revisions"}],"predecessor-version":[{"id":5456,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/2972\/revisions\/5456"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/media\/2988"}],"wp:attachment":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/media?parent=2972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/categories?post=2972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/tags?post=2972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}