{"id":5327,"date":"2023-02-01T16:12:36","date_gmt":"2023-02-01T15:12:36","guid":{"rendered":"https:\/\/www.pschatzmann.ch\/home\/?p=5327"},"modified":"2023-02-02T09:05:23","modified_gmt":"2023-02-02T08:05:23","slug":"audiotools-and-24-bit-values","status":"publish","type":"post","link":"https:\/\/www.pschatzmann.ch\/home\/2023\/02\/01\/audiotools-and-24-bit-values\/","title":{"rendered":"AudioTools and 24 bit values"},"content":{"rendered":"<p>I always planned to support audio with <strong>24bit<\/strong> values in my <a href=\"https:\/\/github.com\/pschatzmann\/arduino-audio-tools\">Arduino Audio Tools<\/a> library, but I never actually tested the functionality and so far I did not have a clear understanding of how these values would need to be represented. To be prepared I already provided a draft implementation of the int24_t data type, but there were some misunderstandings, that have been corrected now.<\/p>\n<p>First we use the <strong>int24_t data type<\/strong> as you would expect. Here is an <strong>example Arduino sketch<\/strong>:<\/p>\n<pre><code>#include \"Arduino.h\"\n#include \"AudioTools.h\"\n\nI2SStream out;\nSineWaveGenerator&lt;int24_t&gt; sine_wave;               \nGeneratedSoundStream&lt;int24_t&gt; in_stream(sine_wave); \nStreamCopy copier(out, in_stream);                  \n\nvoid setup(){\n  Serial.begin(115200);\n  AudioLogger::instance().begin(Serial, AudioLogger::Warning);\n\n  auto cfg = out.defaultConfig();\n  cfg.bits_per_sample = 24;\n  out.begin(cfg);\n  sine_wave.begin(cfg, N_B4);\n  in_stream.begin(cfg);\n}\n\n\nvoid loop(){\n  copier.copy();\n}\n\n<\/code><\/pre>\n<p>No surprises so far: we generate 24bit samples (using int24_t) and specify the output to be 24 bits.<br \/>\nHere are the <strong>details on how int24_t is implemented<\/strong>:<\/p>\n<ul>\n<li>Unlike the name might suggest, the values are stored in a int32_t, so it is <strong>4 bytes wide<\/strong>. <\/li>\n<li>The <strong>value range<\/strong> is from <strong>-8388608 to 8388607<\/strong>, so values outside of this range will need to be clipped.<\/li>\n<li>I2S only considers the first 3 bytes and the last byte on the right will be ignored: So I am storing the value in a int32_t internally, but I shift the values by one byte to the left. <\/li>\n<li>When casting an array of int24_t to a uint8_t*, we will automatically get the right representation. <\/li>\n<li>However when <strong>converting<\/strong> an individual int24_t value <strong>to other data types<\/strong>, I needed to shift the value back by one byte to the right to get the correct original values.<\/li>\n<\/ul>\n<p>Here is the current <a href=\"https:\/\/github.com\/pschatzmann\/arduino-audio-tools\/blob\/main\/src\/AudioBasic\/Int24.h\">int24_t implementation<\/a> which is part of the AudioTools project.<\/p>\n<p>We can also visually test the result, by sending the output to CsvStream<int24_t><code>out(Serial);<\/code><br \/>\nThis gives:<\/p>\n<pre><code>0, 0\n588736, 588736\n1174569, 1174569\n1754609, 1754609\n2325996, 2325996\n2885912, 2885912\n3431595, 3431595\n3960355, 3960355\n4469584, 4469584\n4956769, 4956769\n5419510, 5419510\n5855522, 5855522\n6262658, 6262658\n6638906, 6638906\n6982415, 6982415\n7291488, 7291488\n7564601, 7564601\n7800408, 7800408\n7997746, 7997746\n8155641, 8155641\n8273315, 8273315\n8350187, 8350187\n8385879, 8385879\n8380214, 8380214\n8333219, 8333219\n8245128, 8245128\n8116374, 8116374\n7947593, 7947593\n7739616, 7739616\n7493469, 7493469\n7210366, 7210366\n6891705, 6891705\n6539055, 6539055\n6154156, 6154156\n5738908, 5738908\n5295356, 5295356\n4825688, 4825688\n4332221, 4332221\n3817391, 3817391\n3283733, 3283733\n2733880, 2733880\n2170546, 2170546\n1596506, 1596506\n1014593, 1014593\n427675, 427675\n-161348, -161348\n-749579, -749579\n-1334113, -1334113\n-1912066, -1912066\n-2480591, -2480591\n-3036882, -3036882\n-3578196, -3578196\n-4101861, -4101861\n-4605300, -4605300\n-5086026, -5086026\n-5541667, -5541667\n-5969981, -5969981\n-6368850, -6368850\n-6736312, -6736312\n-7070551, -7070551\n-7369920, -7369920\n-7632944, -7632944\n-7858323, -7858323\n-8044948, -8044948\n-8191897, -8191897\n-8298445, -8298445\n-8364068, -8364068\n-8388442, -8388442\n-8371446, -8371446\n-8313165, -8313165\n-8213886, -8213886\n-8074098, -8074098\n-7894490, -7894490\n-7675949, -7675949\n-7419553, -7419553\n-7126564, -7126564\n-6798431, -6798431\n-6436768, -6436768\n-6043363, -6043363\n-5620153, -5620153\n-5169224, -5169224\n-4692804, -4692804\n-4193241, -4193241\n-3672995, -3672995\n-3134638, -3134638\n-2580821, -2580821\n-2014273, -2014273\n-1437795, -1437795\n-854227, -854227\n-266441, -266441\n322649, 322649\n910155, 910155\n1493171, 1493171\n2068824, 2068824\n2634274, 2634274\n3186732, 3186732\n3723474, 3723474\n4241853, 4241853\n4739313, 4739313\n5213399, 5213399\n5661775, 5661775\n6082228, 6082228\n6472685, 6472685\n6831221, 6831221\n7156067, 7156067\n7445622, 7445622\n7698457, 7698457\n7913326, 7913326\n8089168, 8089168\n8225117, 8225117\n8320502, 8320502\n8374853, 8374853\n8387901, 8387901\n8359583, 8359583\n8290037, 8290037\n8179608, 8179608\n8028838, 8028838\n7838473, 7838473\n7609450, 7609450\n7342900, 7342900\n7040137, 7040137\n6702653, 6702653\n6332114, 6332114\n5930348, 5930348\n5499334, 5499334\n5041198, 5041198\n4558200, 4558200\n4052724, 4052724\n3527260, 3527260\n2984401, 2984401\n2426824, 2426824\n1857278, 1857278\n1278572, 1278572\n693560, 693560\n105130, 105130\n-483820, -483820\n-1070384, -1070384\n-1651668, -1651668\n-2224808, -2224808\n-2786976, -2786976\n-3335399, -3335399\n-3867371, -3867371\n-4380273, -4380273\n-4871571, -4871571\n-5338844, -5338844\n-5779787, -5779787\n-6192227, -6192227\n-6574128, -6574128\n-6923606, -6923606\n-7238939, -7238939\n-7518573, -7518573\n-7761127, -7761127\n-7965406, -7965406\n-8130400, -8130400\n-8255298, -8255298\n-8339483, -8339483\n-8382540, -8382540\n-8384257, -8384257\n-8344624, -8344624\n-8263839, -8263839\n-8142299, -8142299\n-7980602, -7980602\n-7779548, -7779548\n-7540128, -7540128\n-7263520, -7263520\n-6951093, -6951093\n-6604382, -6604382\n-6225103, -6225103\n-5815124, -5815124\n-5376463, -5376463\n-4911289, -4911289\n-4421895, -4421895\n-3910689, -3910689\n-3380201, -3380201\n-2833043, -2833043\n-2271909, -2271909\n-1699574, -1699574\n-1118858, -1118858\n-532619, -532619\n56240, 56240\n644824, 644824\n<\/code><\/pre>\n<p>This confirms that we get the result in the expected range.<\/p>\n<p>ps. If you don&#8217;t want to use the int24_t data type, you can just use the int32_t data type instead and provide audio with 32 bits resulution. Then I2S will just consider the first 3 bytes and ignores the last 8 bits&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I always planned to support audio with 24bit values in my Arduino Audio Tools library, but I never actually tested the functionality and so far I did not have a clear understanding of how these values would need to be represented. To be prepared I already provided a draft implementation [&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":[],"class_list":["post-5327","post","type-post","status-publish","format-standard","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>AudioTools and 24 bit values - 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\/02\/01\/audiotools-and-24-bit-values\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"AudioTools and 24 bit values - Phil Schatzmann\" \/>\n<meta property=\"og:description\" content=\"I always planned to support audio with 24bit values in my Arduino Audio Tools library, but I never actually tested the functionality and so far I did not have a clear understanding of how these values would need to be represented. To be prepared I already provided a draft implementation [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pschatzmann.ch\/home\/2023\/02\/01\/audiotools-and-24-bit-values\/\" \/>\n<meta property=\"og:site_name\" content=\"Phil Schatzmann\" \/>\n<meta property=\"article:published_time\" content=\"2023-02-01T15:12:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-02-02T08:05:23+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=\"4 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\\\/02\\\/01\\\/audiotools-and-24-bit-values\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/02\\\/01\\\/audiotools-and-24-bit-values\\\/\"},\"author\":{\"name\":\"pschatzmann\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"headline\":\"AudioTools and 24 bit values\",\"datePublished\":\"2023-02-01T15:12:36+00:00\",\"dateModified\":\"2023-02-02T08:05:23+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/02\\\/01\\\/audiotools-and-24-bit-values\\\/\"},\"wordCount\":323,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"articleSection\":[\"Arduino\",\"Machine Sound\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/02\\\/01\\\/audiotools-and-24-bit-values\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/02\\\/01\\\/audiotools-and-24-bit-values\\\/\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/02\\\/01\\\/audiotools-and-24-bit-values\\\/\",\"name\":\"AudioTools and 24 bit values - Phil Schatzmann\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#website\"},\"datePublished\":\"2023-02-01T15:12:36+00:00\",\"dateModified\":\"2023-02-02T08:05:23+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/02\\\/01\\\/audiotools-and-24-bit-values\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/02\\\/01\\\/audiotools-and-24-bit-values\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/02\\\/01\\\/audiotools-and-24-bit-values\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"AudioTools and 24 bit values\"}]},{\"@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":"AudioTools and 24 bit values - 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\/02\/01\/audiotools-and-24-bit-values\/","og_locale":"en_US","og_type":"article","og_title":"AudioTools and 24 bit values - Phil Schatzmann","og_description":"I always planned to support audio with 24bit values in my Arduino Audio Tools library, but I never actually tested the functionality and so far I did not have a clear understanding of how these values would need to be represented. To be prepared I already provided a draft implementation [&hellip;]","og_url":"https:\/\/www.pschatzmann.ch\/home\/2023\/02\/01\/audiotools-and-24-bit-values\/","og_site_name":"Phil Schatzmann","article_published_time":"2023-02-01T15:12:36+00:00","article_modified_time":"2023-02-02T08:05:23+00:00","author":"pschatzmann","twitter_card":"summary_large_image","twitter_misc":{"Written by":"pschatzmann","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/02\/01\/audiotools-and-24-bit-values\/#article","isPartOf":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/02\/01\/audiotools-and-24-bit-values\/"},"author":{"name":"pschatzmann","@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"headline":"AudioTools and 24 bit values","datePublished":"2023-02-01T15:12:36+00:00","dateModified":"2023-02-02T08:05:23+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/02\/01\/audiotools-and-24-bit-values\/"},"wordCount":323,"commentCount":0,"publisher":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"articleSection":["Arduino","Machine Sound"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pschatzmann.ch\/home\/2023\/02\/01\/audiotools-and-24-bit-values\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/02\/01\/audiotools-and-24-bit-values\/","url":"https:\/\/www.pschatzmann.ch\/home\/2023\/02\/01\/audiotools-and-24-bit-values\/","name":"AudioTools and 24 bit values - Phil Schatzmann","isPartOf":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#website"},"datePublished":"2023-02-01T15:12:36+00:00","dateModified":"2023-02-02T08:05:23+00:00","breadcrumb":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/02\/01\/audiotools-and-24-bit-values\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pschatzmann.ch\/home\/2023\/02\/01\/audiotools-and-24-bit-values\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/02\/01\/audiotools-and-24-bit-values\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pschatzmann.ch\/home\/"},{"@type":"ListItem","position":2,"name":"AudioTools and 24 bit values"}]},{"@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\/5327","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=5327"}],"version-history":[{"count":22,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/5327\/revisions"}],"predecessor-version":[{"id":5349,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/5327\/revisions\/5349"}],"wp:attachment":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/media?parent=5327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/categories?post=5327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/tags?post=5327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}