{"id":5538,"date":"2023-04-01T19:02:52","date_gmt":"2023-04-01T17:02:52","guid":{"rendered":"https:\/\/www.pschatzmann.ch\/home\/?p=5538"},"modified":"2023-06-30T11:53:57","modified_gmt":"2023-06-30T09:53:57","slug":"arduino-audiotools-formatconverterstream","status":"publish","type":"post","link":"https:\/\/www.pschatzmann.ch\/home\/2023\/04\/01\/arduino-audiotools-formatconverterstream\/","title":{"rendered":"Arduino AudioTools &#8211; FormatConverterStream"},"content":{"rendered":"<p>I am currently providing quite a few audio processing classes which can be used to <a href=\"https:\/\/pschatzmann.github.io\/arduino-audio-tools\/classaudio__tools_1_1_reformat_base_stream.html\">change the audio format<\/a>.<\/p>\n<p>So far, the <a href=\"https:\/\/pschatzmann.github.io\/arduino-audio-tools\/classaudio__tools_1_1_format_converter_stream.html\">FormatConverterStream<\/a> could be used to change the <strong>bits_per_sample<\/strong> and the number of <strong>channels<\/strong> only.<\/p>\n<p>I thought it might be cool, if we could use this class for <strong>changing the sample rate<\/strong> as well, so I finally managed to commit this change!<\/p>\n<p>Here is an example which changes<\/p>\n<ul>\n<li>The sample rate from 32000 to 16000<\/li>\n<li>The channels from 2 to 1<\/li>\n<li>The bits_per_sample from 32 to 16<\/li>\n<\/ul>\n<p>The following audio chain is implemented <strong>GeneratedSoundStream -> FormatConverterStream &#8211; copy -> I2SStream<\/strong>. The data is read with the audio information defined in the from variable and writes it with a different format (specified with the to variable) to I2S. In this example the conversion is happening on the reading\/input side!<\/p>\n<h2>Example<\/h2>\n<pre><code>#include \"AudioTools.h\"\n#include \"AudioLibs\/AudioKit.h\"\n\nAudioInfo from(32000,2,32);\nAudioInfo to(16000,1,16);\nSineWaveGenerator&lt;int32_t&gt; sineWave;                \nGeneratedSoundStream&lt;int32_t&gt; sound(sineWave); \/\/ Stream generated from sine wave\nI2SStream out;   \/\/ or any other e.g. AudioKitStream, CsvStream&lt;int16_t&gt; out(Serial); \nFormatConverterStream converter(sound);  \/\/ or use converter(out)\nStreamCopy copier(out, converter);       \/\/        copier(converter, sound);     \n\n\/\/ Arduino Setup\nvoid setup(void) {  \n  \/\/ Open Serial \n  Serial.begin(115200);\n  AudioLogger::instance().begin(Serial, AudioLogger::Warning);\n\n  \/\/ Setup Input\n  sineWave.begin(from, N_B4);\n  sound.begin(from);\n\n  \/\/ Define Converter\n  converter.begin(from, to);\n\n  \/\/ Start Output\n  Serial.println(\"starting I2S...\");\n  auto config = out.defaultConfig(TX_MODE);\n  config.copyFrom(to);\n  out.begin(config);\n\n  Serial.println(\"started...\");\n}\n\n\/\/ Arduino loop - copy sound to out \nvoid loop() {\n  copier.copy();\n}\n\n<\/code><\/pre>\n<p>Of cause this is also working on the <strong>output side<\/strong>: In fact it is even better to do the conversion this way, because we will use <strong>less memory<\/strong>. This only requires a small change for the example above:<\/p>\n<pre><code>FormatConverterStream converter(out);  \nStreamCopy copier(converter, sound);       \n\n<\/code><\/pre>\n<h2>Documentation<\/h2>\n<p>I have updated the <a href=\"https:\/\/github.com\/pschatzmann\/arduino-audio-tools\/wiki\/Converting-the-Data-Format\">documentation on Github<\/a><br \/>\nYou can also find <a href=\"https:\/\/github.com\/pschatzmann\/arduino-audio-tools\/blob\/main\/examples\/examples-stream\/streams-generator-formatconverter-i2s\/streams-generator-formatconverter-i2s.ino\">the demo example<\/a> there.<\/p>\n<h2>Final Comments<\/h2>\n<p>This class is very flexible and convenient to use. However if you need to save some milliseconds in your processing, it is a bit more efficient to use the dedicated class if you need to change only a single parameter: e.g. the sample_rate:<\/p>\n<ul>\n<li><a href=\"https:\/\/pschatzmann.github.io\/arduino-audio-tools\/classaudio__tools_1_1_channel_format_converter_stream.html\">ChannelFormatConverterStream<\/a><\/li>\n<li><a href=\"https:\/\/pschatzmann.github.io\/arduino-audio-tools\/classaudio__tools_1_1_number_format_converter_stream.html\">NumberFormatConverterStream<\/a><\/li>\n<li><a href=\"https:\/\/pschatzmann.github.io\/arduino-audio-tools\/classaudio__tools_1_1_resample_stream.html\">ResampleStream<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>I am currently providing quite a few audio processing classes which can be used to change the audio format. So far, the FormatConverterStream could be used to change the bits_per_sample and the number of channels only. I thought it might be cool, if we could use this class for changing [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"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":[44],"class_list":["post-5538","post","type-post","status-publish","format-standard","hentry","category-arduino","category-machine-sound","tag-uno"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Arduino AudioTools - FormatConverterStream - 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\/2023\/04\/01\/arduino-audiotools-formatconverterstream\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Arduino AudioTools - FormatConverterStream - Phil Schatzmann\" \/>\n<meta property=\"og:description\" content=\"I am currently providing quite a few audio processing classes which can be used to change the audio format. So far, the FormatConverterStream could be used to change the bits_per_sample and the number of channels only. I thought it might be cool, if we could use this class for changing [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pschatzmann.ch\/home\/2023\/04\/01\/arduino-audiotools-formatconverterstream\/\" \/>\n<meta property=\"og:site_name\" content=\"Phil Schatzmann\" \/>\n<meta property=\"article:published_time\" content=\"2023-04-01T17:02:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-06-30T09:53:57+00:00\" \/>\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\\\/2023\\\/04\\\/01\\\/arduino-audiotools-formatconverterstream\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/04\\\/01\\\/arduino-audiotools-formatconverterstream\\\/\"},\"author\":{\"name\":\"pschatzmann\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"headline\":\"Arduino AudioTools &#8211; FormatConverterStream\",\"datePublished\":\"2023-04-01T17:02:52+00:00\",\"dateModified\":\"2023-06-30T09:53:57+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/04\\\/01\\\/arduino-audiotools-formatconverterstream\\\/\"},\"wordCount\":249,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"keywords\":[\"UNO\"],\"articleSection\":[\"Arduino\",\"Machine Sound\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/04\\\/01\\\/arduino-audiotools-formatconverterstream\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/04\\\/01\\\/arduino-audiotools-formatconverterstream\\\/\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/04\\\/01\\\/arduino-audiotools-formatconverterstream\\\/\",\"name\":\"Arduino AudioTools - FormatConverterStream - Phil Schatzmann\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#website\"},\"datePublished\":\"2023-04-01T17:02:52+00:00\",\"dateModified\":\"2023-06-30T09:53:57+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/04\\\/01\\\/arduino-audiotools-formatconverterstream\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/04\\\/01\\\/arduino-audiotools-formatconverterstream\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/04\\\/01\\\/arduino-audiotools-formatconverterstream\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Arduino AudioTools &#8211; FormatConverterStream\"}]},{\"@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":"Arduino AudioTools - FormatConverterStream - 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\/2023\/04\/01\/arduino-audiotools-formatconverterstream\/","og_locale":"en_US","og_type":"article","og_title":"Arduino AudioTools - FormatConverterStream - Phil Schatzmann","og_description":"I am currently providing quite a few audio processing classes which can be used to change the audio format. So far, the FormatConverterStream could be used to change the bits_per_sample and the number of channels only. I thought it might be cool, if we could use this class for changing [&hellip;]","og_url":"https:\/\/www.pschatzmann.ch\/home\/2023\/04\/01\/arduino-audiotools-formatconverterstream\/","og_site_name":"Phil Schatzmann","article_published_time":"2023-04-01T17:02:52+00:00","article_modified_time":"2023-06-30T09:53:57+00:00","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\/2023\/04\/01\/arduino-audiotools-formatconverterstream\/#article","isPartOf":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/04\/01\/arduino-audiotools-formatconverterstream\/"},"author":{"name":"pschatzmann","@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"headline":"Arduino AudioTools &#8211; FormatConverterStream","datePublished":"2023-04-01T17:02:52+00:00","dateModified":"2023-06-30T09:53:57+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/04\/01\/arduino-audiotools-formatconverterstream\/"},"wordCount":249,"commentCount":0,"publisher":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"keywords":["UNO"],"articleSection":["Arduino","Machine Sound"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pschatzmann.ch\/home\/2023\/04\/01\/arduino-audiotools-formatconverterstream\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/04\/01\/arduino-audiotools-formatconverterstream\/","url":"https:\/\/www.pschatzmann.ch\/home\/2023\/04\/01\/arduino-audiotools-formatconverterstream\/","name":"Arduino AudioTools - FormatConverterStream - Phil Schatzmann","isPartOf":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#website"},"datePublished":"2023-04-01T17:02:52+00:00","dateModified":"2023-06-30T09:53:57+00:00","breadcrumb":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/04\/01\/arduino-audiotools-formatconverterstream\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pschatzmann.ch\/home\/2023\/04\/01\/arduino-audiotools-formatconverterstream\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/04\/01\/arduino-audiotools-formatconverterstream\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pschatzmann.ch\/home\/"},{"@type":"ListItem","position":2,"name":"Arduino AudioTools &#8211; FormatConverterStream"}]},{"@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\/5538","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=5538"}],"version-history":[{"count":22,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/5538\/revisions"}],"predecessor-version":[{"id":5560,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/5538\/revisions\/5560"}],"wp:attachment":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/media?parent=5538"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/categories?post=5538"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/tags?post=5538"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}