{"id":6155,"date":"2024-03-20T07:36:03","date_gmt":"2024-03-20T06:36:03","guid":{"rendered":"https:\/\/www.pschatzmann.ch\/home\/?p=6155"},"modified":"2024-03-20T07:45:19","modified_gmt":"2024-03-20T06:45:19","slug":"i2s-output-of-4-channels-on-a-esp32","status":"publish","type":"post","link":"https:\/\/www.pschatzmann.ch\/home\/2024\/03\/20\/i2s-output-of-4-channels-on-a-esp32\/","title":{"rendered":"I2S Output of 4 Channels with an ESP32"},"content":{"rendered":"<p>Recently there was a <a href=\"https:\/\/github.com\/pschatzmann\/arduino-audio-tools\/discussions\/1450\">discussion on how to output more then 2 channels<\/a> via I2S. I2S is limited to 2 channels, but the ESP32 has 2 I2S ports, so we can output a maximum of 4 channels!<\/p>\n<h2>Arduino Sketch<\/h2>\n<p>I have added a small example that shows how this can be done using my <a href=\"https:\/\/github.com\/pschatzmann\/arduino-audio-tools\">AudioTools<\/a> library:<\/p>\n<pre><code>#include &lt;SPI.h&gt;\n#include &lt;SD.h&gt;\n#include \"AudioTools.h\"\n#include \"AudioCodecs\/CodecMP3Helix.h\"\n\nconst int chipSelect=10;\nAudioInfo info(44100, 2, 16);\nI2SStream i2s_1; \/\/ final output\nI2SStream i2s_2; \/\/ final out\nChannelsSelectOutput out;\nWAVDecoder wav;\nEncodedAudioOutput decoder(&amp;out, &amp;wav); \/\/ Decoding stream\nStreamCopy copier; \nFile audioFile;\n\nvoid setup(){\n  Serial.begin(115200);\n  AudioLogger::instance().begin(Serial, AudioLogger::Info);  \n\n  \/\/ setup file\n  SD.begin(chipSelect);\n  audioFile = SD.open(\"\/Music\/ch4.wav\");\n\n  \/\/ setup i2s\n  auto config1 = i2s_1.defaultConfig(TX_MODE);\n  config1.copyFrom(info);\n  config1.port_no = 0;\n  i2s_1.begin(config1);\n  auto config2 = i2s_2.defaultConfig(TX_MODE);\n  config2.copyFrom(info);\n  config2.port_no = 1;\n  i2s_2.begin(config2);\n\n  \/\/ split channels to different i2s ports\n  out.addOutput(i2s_1, 0, 1);\n  out.addOutput(i2s_2, 2, 3);\n  \/\/out.addNotifyAudioChange(i2s_1);\n  \/\/out.addNotifyAudioChange(i2s_2);\n  out.begin(info);\n\n  \/\/ setup decoder\n  decoder.begin();\n\n  \/\/ begin copy\n  copier.begin(decoder, audioFile);\n}\n\nvoid loop(){\n  if (!copier.copy()) {\n    stop();\n  }\n}\n<\/code><\/pre>\n<p>We define 2 separate I2SStream audio sink objects. The data source is a decoded WAV file that has audio with 4 channels and we just need to use a ChannelsSelectOutput as additional intermediary sink. This allows us to define which channels should go to which final output. In the example channel 0 and 1 go to the I2S on port 0 and the channels 2 and 3 go to port 1.<\/p>\n<p>Please note that these output destinations are not automatically notified about format changes, so if you want this to happen, you need to add the corresponding addNotifyAudioChange() statements yourself.<\/p>\n<p>This API might be changing, so please check the <a href=\"https:\/\/github.com\/pschatzmann\/arduino-audio-tools\/blob\/main\/examples\/examples-stream\/streams-sd_wav4-i2s\/streams-sd_wav4-i2s.ino\">potentially updated example on Github<\/a>!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently there was a discussion on how to output more then 2 channels via I2S. I2S is limited to 2 channels, but the ESP32 has 2 I2S ports, so we can output a maximum of 4 channels! Arduino Sketch I have added a small example that shows how this can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":5682,"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":[],"class_list":["post-6155","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arduino","category-machine-sound"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>I2S Output of 4 Channels with an ESP32 - 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\/2024\/03\/20\/i2s-output-of-4-channels-on-a-esp32\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"I2S Output of 4 Channels with an ESP32 - Phil Schatzmann\" \/>\n<meta property=\"og:description\" content=\"Recently there was a discussion on how to output more then 2 channels via I2S. I2S is limited to 2 channels, but the ESP32 has 2 I2S ports, so we can output a maximum of 4 channels! Arduino Sketch I have added a small example that shows how this can [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pschatzmann.ch\/home\/2024\/03\/20\/i2s-output-of-4-channels-on-a-esp32\/\" \/>\n<meta property=\"og:site_name\" content=\"Phil Schatzmann\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-20T06:36:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-20T06:45:19+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2023\/05\/wav.png\" \/>\n\t<meta property=\"og:image:width\" content=\"512\" \/>\n\t<meta property=\"og:image:height\" content=\"512\" \/>\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\\\/2024\\\/03\\\/20\\\/i2s-output-of-4-channels-on-a-esp32\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2024\\\/03\\\/20\\\/i2s-output-of-4-channels-on-a-esp32\\\/\"},\"author\":{\"name\":\"pschatzmann\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"headline\":\"I2S Output of 4 Channels with an ESP32\",\"datePublished\":\"2024-03-20T06:36:03+00:00\",\"dateModified\":\"2024-03-20T06:45:19+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2024\\\/03\\\/20\\\/i2s-output-of-4-channels-on-a-esp32\\\/\"},\"wordCount\":171,\"commentCount\":5,\"publisher\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2024\\\/03\\\/20\\\/i2s-output-of-4-channels-on-a-esp32\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2023\\\/05\\\/wav.png\",\"articleSection\":[\"Arduino\",\"Machine Sound\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2024\\\/03\\\/20\\\/i2s-output-of-4-channels-on-a-esp32\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2024\\\/03\\\/20\\\/i2s-output-of-4-channels-on-a-esp32\\\/\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2024\\\/03\\\/20\\\/i2s-output-of-4-channels-on-a-esp32\\\/\",\"name\":\"I2S Output of 4 Channels with an ESP32 - Phil Schatzmann\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2024\\\/03\\\/20\\\/i2s-output-of-4-channels-on-a-esp32\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2024\\\/03\\\/20\\\/i2s-output-of-4-channels-on-a-esp32\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2023\\\/05\\\/wav.png\",\"datePublished\":\"2024-03-20T06:36:03+00:00\",\"dateModified\":\"2024-03-20T06:45:19+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2024\\\/03\\\/20\\\/i2s-output-of-4-channels-on-a-esp32\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2024\\\/03\\\/20\\\/i2s-output-of-4-channels-on-a-esp32\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2024\\\/03\\\/20\\\/i2s-output-of-4-channels-on-a-esp32\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2023\\\/05\\\/wav.png\",\"contentUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2023\\\/05\\\/wav.png\",\"width\":512,\"height\":512},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2024\\\/03\\\/20\\\/i2s-output-of-4-channels-on-a-esp32\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"I2S Output of 4 Channels with an ESP32\"}]},{\"@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":"I2S Output of 4 Channels with an ESP32 - 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\/2024\/03\/20\/i2s-output-of-4-channels-on-a-esp32\/","og_locale":"en_US","og_type":"article","og_title":"I2S Output of 4 Channels with an ESP32 - Phil Schatzmann","og_description":"Recently there was a discussion on how to output more then 2 channels via I2S. I2S is limited to 2 channels, but the ESP32 has 2 I2S ports, so we can output a maximum of 4 channels! Arduino Sketch I have added a small example that shows how this can [&hellip;]","og_url":"https:\/\/www.pschatzmann.ch\/home\/2024\/03\/20\/i2s-output-of-4-channels-on-a-esp32\/","og_site_name":"Phil Schatzmann","article_published_time":"2024-03-20T06:36:03+00:00","article_modified_time":"2024-03-20T06:45:19+00:00","og_image":[{"width":512,"height":512,"url":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2023\/05\/wav.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\/2024\/03\/20\/i2s-output-of-4-channels-on-a-esp32\/#article","isPartOf":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2024\/03\/20\/i2s-output-of-4-channels-on-a-esp32\/"},"author":{"name":"pschatzmann","@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"headline":"I2S Output of 4 Channels with an ESP32","datePublished":"2024-03-20T06:36:03+00:00","dateModified":"2024-03-20T06:45:19+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2024\/03\/20\/i2s-output-of-4-channels-on-a-esp32\/"},"wordCount":171,"commentCount":5,"publisher":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"image":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2024\/03\/20\/i2s-output-of-4-channels-on-a-esp32\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2023\/05\/wav.png","articleSection":["Arduino","Machine Sound"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pschatzmann.ch\/home\/2024\/03\/20\/i2s-output-of-4-channels-on-a-esp32\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pschatzmann.ch\/home\/2024\/03\/20\/i2s-output-of-4-channels-on-a-esp32\/","url":"https:\/\/www.pschatzmann.ch\/home\/2024\/03\/20\/i2s-output-of-4-channels-on-a-esp32\/","name":"I2S Output of 4 Channels with an ESP32 - Phil Schatzmann","isPartOf":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2024\/03\/20\/i2s-output-of-4-channels-on-a-esp32\/#primaryimage"},"image":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2024\/03\/20\/i2s-output-of-4-channels-on-a-esp32\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2023\/05\/wav.png","datePublished":"2024-03-20T06:36:03+00:00","dateModified":"2024-03-20T06:45:19+00:00","breadcrumb":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2024\/03\/20\/i2s-output-of-4-channels-on-a-esp32\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pschatzmann.ch\/home\/2024\/03\/20\/i2s-output-of-4-channels-on-a-esp32\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pschatzmann.ch\/home\/2024\/03\/20\/i2s-output-of-4-channels-on-a-esp32\/#primaryimage","url":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2023\/05\/wav.png","contentUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2023\/05\/wav.png","width":512,"height":512},{"@type":"BreadcrumbList","@id":"https:\/\/www.pschatzmann.ch\/home\/2024\/03\/20\/i2s-output-of-4-channels-on-a-esp32\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pschatzmann.ch\/home\/"},{"@type":"ListItem","position":2,"name":"I2S Output of 4 Channels with an ESP32"}]},{"@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\/6155","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=6155"}],"version-history":[{"count":8,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/6155\/revisions"}],"predecessor-version":[{"id":6163,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/6155\/revisions\/6163"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/media\/5682"}],"wp:attachment":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/media?parent=6155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/categories?post=6155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/tags?post=6155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}