Audio.prepare()

[Audio]

描述

Prepares audio samples from the named file to the audio buffer and sets the volume.

語法

Audio.prepare(buffer, samples, volume);

參數

  • buffer (short): the named buffer holding the audio file.
  • samples (int): number of samples to write.
  • volume (int): a 10-bit number representing the volume of the audio being played. 0 turns the sound off, 1023 is the maximum volume.

回傳

Nothing.

範例

#include <SD.h>
#include <Audio.h>
 
void setup()
{
  // debug output at 9600 baud
  Serial.begin(9600);
 
  // setup SD-card
  Serial.print("Initializing SD card...");
  if (!SD.begin()) {
    Serial.println(" failed!");
    return;
  }
  Serial.println(" done.");
 
  // 44100hz stereo => 88200 sample rate
  // 100 mSec of prebuffering.
  Audio.begin(88200, 100);
}
 
void loop()
{
  int count=0;
 
  // open wave file from sdcard
  File myFile = SD.open("test.wav");
  if (!myFile) {
    // if the file didn't open, print an error and stop
    Serial.println("error opening test.wav");
    while (true);
  }
 
  const int S=1024; // Number of samples to read in block
  short buffer[S];
 
  Serial.print("Playing");
  // until the file is not finished
  while (myFile.available()) {
    // read from the file into buffer
    myFile.read(buffer, sizeof(buffer));
 
    // Prepare samples
    int volume = 1023;
    Audio.prepare(buffer, S, volume);
    // Feed samples to audio
    Audio.write(buffer, S);
 
    // Every 100 block print a '.'
    count++;
    if (count == 100) {
      Serial.print(".");
      count = 0;
    }
  }
  myFile.close();
 
  Serial.println("End of file. Thank you for listening!");
  while (true) ;
}

函式庫參考主頁面

86Duino 參考的文本是根據 Creative Commons Attribution-ShareAlike 3.0 License,部分文本是從 the Arduino reference 修改的。 參考中的代碼示例已發佈到公共領域。

發表評論

上部へスクロール