{"id":12088,"date":"2022-12-13T02:49:33","date_gmt":"2022-12-13T02:49:33","guid":{"rendered":"https:\/\/www.qec.tw\/?p=12088"},"modified":"2022-12-13T03:04:55","modified_gmt":"2022-12-13T03:04:55","slug":"audio-begin","status":"publish","type":"post","link":"https:\/\/www.qec.tw\/zh\/86duino-library\/audio\/audio-begin\/","title":{"rendered":"Audio.begin()"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">[Audio]<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Description<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Initializes the Audio library by specifying the target sample rate and size of the audio buffer.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Audio.begin(rate, size);<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Parameters<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>rate<\/code>&nbsp;(int): the sample rate of the sound file. If stereo, double the rate (ex. 44100hz stereo = 88200).<\/li>\n\n\n\n<li><code>size<\/code>&nbsp;(int): the size of the audio buffer in milliseconds.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Returns<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Nothing.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>#include &lt;SD.h&gt;\n#include &lt;Audio.h&gt;\n \nvoid setup()\n{\n  \/\/ debug output at 9600 baud\n  Serial.begin(9600);\n \n  \/\/ setup SD-card\n  Serial.print(&quot;Initializing SD card...&quot;);\n  if (!SD.begin()) {\n    Serial.println(&quot; failed!&quot;);\n    return;\n  }\n  Serial.println(&quot; done.&quot;);\n \n  \/\/ 44100hz stereo =&gt; 88200 sample rate\n  \/\/ 100 mSec of prebuffering.\n  Audio.begin(88200, 100);\n}\n \nvoid loop()\n{\n  int count=0;\n \n  \/\/ open wave file from sdcard\n  File myFile = SD.open(&quot;test.wav&quot;);\n  if (!myFile) {\n    \/\/ if the file didn't open, print an error and stop\n    Serial.println(&quot;error opening test.wav&quot;);\n    while (true);\n  }\n \n  const int S=1024; \/\/ Number of samples to read in block\n  short buffer[S];\n \n  Serial.print(&quot;Playing&quot;);\n  \/\/ until the file is not finished\n  while (myFile.available()) {\n    \/\/ read from the file into buffer\n    myFile.read(buffer, sizeof(buffer));\n \n    \/\/ Prepare samples\n    int volume = 1023;\n    Audio.prepare(buffer, S, volume);\n    \/\/ Feed samples to audio\n    Audio.write(buffer, S);\n \n    \/\/ Every 100 block print a '.'\n    count++;\n    if (count == 100) {\n      Serial.print(&quot;.&quot;);\n      count = 0;\n    }\n  }\n  myFile.close();\n \n  Serial.println(&quot;End of file. Thank you for listening!&quot;);\n  while (true) ;\n}<\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/www.qec.tw\/86duino\/libraries\/\">Libraries Reference Home<\/a><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><em>The text of the 86Duino reference is a modification of&nbsp;<a href=\"http:\/\/arduino.cc\/en\/Reference\/HomePage\" target=\"_blank\" rel=\"noopener\">the Arduino reference<\/a>&nbsp;and is licensed under a&nbsp;<a href=\"http:\/\/creativecommons.org\/licenses\/by-sa\/3.0\/\" target=\"_blank\" rel=\"noopener\">Creative Commons Attribution-ShareAlike 3.0 License<\/a>. Code samples in the reference are released into the public domain.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>[Audio] Description Initializes the Audio library by specifying the target sample rate and size of the audio buffer. Syntax Audio.begin(rate, size); Parameters Returns Nothing. Example [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":12089,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[258],"tags":[162],"class_list":["post-12088","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-audio","tag-86duino"],"_links":{"self":[{"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/posts\/12088","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/comments?post=12088"}],"version-history":[{"count":0,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/posts\/12088\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/media\/12089"}],"wp:attachment":[{"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/media?parent=12088"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/categories?post=12088"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/tags?post=12088"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}