进阶制作

如何利用Arduino播放音乐

字号+ 作者:duino123.com 来源:未知 2016-06-02 21:06 我要评论( )

原作是用SD卡直接连接,本人是用W5100和CC3300自带的SD卡槽实现的,原理相同。 前半部分直接驱动喇叭,声音比较小;后半部分接入功放,满足大音量的需要。 材料: 1. Arduino UNO R3 一块 2. 喇叭 一只 3. 10uF以上电容 一只 4. SD卡及相应的读卡器 一块 5.

原作是用SD卡直接连接,本人是用W5100和CC3300自带的SD卡槽实现的,原理相同。

前半部分直接驱动喇叭,声音比较小;后半部分接入功放,满足大音量的需要。


材料:

1. Arduino UNO R3  一块

2. 喇叭 一只

3. 10uF以上电容 一只

4. SD卡及相应的读卡器  一块

5. 导线若干

6. 自己制作SD卡座或者利用W5100/CC3300



声音文件格式转换:

首先利用foorbar2000,将其他声音格式文件转换为wav格式。

第二步,将wav格式的文件转换为Arduino可识别的AFM格式。注意把待转换的wav文件复制到 tools/Arduino with 16 MHz/ 目录下,才能正确转换。 将你刚刚生成的.wav格式文件拖到这个批处理上。


5.jpg

3.jpg

4.jpg


第三部将 SD卡在电脑上格式化为FAT格式,并将上一步转换出的文件(.AFM后缀)复制到SD卡内。注意,文件名不能使用中文。
 
连接:

自制SD卡托的接线

IMGP9505-1.jpg

喇叭正极接到D9,负极接到GND,可以在喇叭负极和GND之间串联一颗10uF以上的电容,提高音质。



下载和使用库文件:


6.jpg 

下载完成后,打开串口调试器,如果没有出错的情况下,你会看到如下画面
7.jpg 

然后,将你要播放的文件名输入后发送,正确的情况下回提示如下
9.jpg 

然后,发送p就可以播放了,你就可以从喇叭中听到优美的歌曲了
10.jpg 

---------------------------------------------------------------------------------
以下是升级功放的内容:

增加材料:

7. D类功放

8. 对应的喇叭(4-8W)

9. 12V电源




API reference

Constants

Here is an overview of the constants used in the library.

#define SSDA_VERSIONSTRING      "1.03"

// Sound Mode Flags
#define SSDA_MODE_FULLRATE      0x00    // 62.500 kHz @ 16 MHz, 31.250 kHz @ 8 MHz
#define SSDA_MODE_HALFRATE      0x10    // 31.250 kHz @ 16 MHz, 15.625 kHz @ 8 MHz

#define SSDA_MODE_MONO          0x00    // Use only 1st PWM pin
#define SSDA_MODE_STEREO        0x01    // Use both PWM pins for stereo output
#define SSDA_MODE_QUADRO	0x04	// Uses four PWM pins for either 4 speakers or Stereo 16 Bit
#define SSDA_MODE_MONO_BRIDGE   0x02    // Use both PWM pins for more power

#define SSDA_MODE_AUTOWORKER	0x80	// Use this and worker is called automatically

// Error codes from SimpleSDAudio, see other sd_l*.h for more error codes
#define SSDA_ERROR_NULL         0x80    // Null pointer
#define SSDA_ERROR_BUFTOSMALL   0x81    // Buffer to small
#define SSDA_ERROR_NOT_INIT     0x82    // System not initialized properly

Class name and default instance

class SdPlayClass { ... }
extern SdPlayClass SdPlay;

The name of the class is SdPlayClass. One instance is already created for use and is named SdPlay.

Public class functions

init()

boolean init(uint8_t soundMode);

Call this to initialize the library and set sound mode. This function will also acquire the needed buffer (if not already set manually using setWorkBuffer), initialize SD card and sets up all used pins.

A combination of the following flags must be provided as argument (combine via or-ing):

Sample rate setting flags

  • SSDA_MODE_FULLRATE - 62.500 kHz @ 16 MHz, 31.250 kHz @ 8 MHz
  • SSDA_MODE_HALFRATE - 31.250 kHz @ 16 MHz, 15.625 kHz @ 8 MHz

Output channel configuration flags

  • SSDA_MODE_MONO - Use only 1st PWM pin
  • SSDA_MODE_STEREO - Use both PWM pins for stereo output or mono 16 bit
  • SSDA_MODE_QUADRO - Uses four PWM pins for either 4 speakers or stereo 16 Bit
  • SSDA_MODE_MONO_BRIDGE - Use both PWM pins for more power (mono output only)

Auto worker call flag

  • SSDA_MODE_AUTOWORKER - Add this flag and you don't need to call worker for playback

The function return true if successful, false if an error occurred. You can use getLastError() to retrieve the error code. Typical reasons for errors are wrong SD card connection or too low RAM (1k heap required) for internal buffer available.

setFile()

boolean setFile(char *fileName);

After init was successfull, call this to select audio file by providing the filename in 8.3 format.

The function return true if successful, false if an error occured. You can use getLastError() to retrieve the error code. Typical reasons for errors are that the file was not found.

worker()

void worker(void); 

Call this continually at least as long as the audio playback is running. This function fills the internal playback buffer by reading the next sectors from SD card. You can´t call this function too often, but a buffer underrun can occur when the time between two calls are too long.

As of Version 1.03: Add the flag
SSDA_MODE_AUTOWORKER

to init and worker will be called automatically in background interrupt. You don't need to call it yourself anymore. This makes audio playback more robust when Serial functions are used. However, it does not work in conjunction with Ethernet-Shield, as access to SPI bus must be properly shared between SD-card and Ethernet chip.

play()

void play(void);

If audio is not playing, playing will be started. If it is already playing, it will start playing from zero again.

stop()

void stop(void);

Stops playback if playing, sets playposition to zero.

pause()

void pause(void);

Pauses playing if not playing, resumes playing if was paused.

setSDCSPin()

void setSDCSPin(uint8_t csPin);

Call this before init to set SD-Cards CS-Pin to other than default.

setWorkBuffer()

void setWorkBuffer(uint8_t *pBuf, uint16_t bufSize);

Call this if you want to use your own buffer (at least 1024 bytes, must be multiple of 512). Call this before init and then init will use this buffer instead using malloc to create its own.

deInit()

void deInit(void);

Call this to free resources like buffer, release SD card pins, audio interrupts and audio pins.

dir()

void dir(void (*callback)(char *));

Output the directory of the SD card. A pointer to a callback function must be provided. The callback function is called once for every file found in the root directory of the card. The strings contain zero-termination but no linefeeds.

Usage example:

...
// setup global callback function
void dir_callback(char *buf) {
 Serial.println(buf);
}
...

   // somewhere after initialization
   SdPlay.dir(&dir_callback);

isStopped(), isPlaying(), isPaused()

boolean isStopped(void);
boolean isPlaying(void);
boolean isPaused(void);

Returns the current state of the playback. The function isStopped() can be used after issuing play() to determine when the playback has finished.

isUnderrunOccured()

boolean isUnderrunOccured(void);

Returns if the internal underrun-flag is set and clears it. If audio sounds strange or has dropouts, test if underruns are the cause.

getLastError()

uint8_t getLastError(void);

Retrieve the last error code and clears it. To analyse the error reason, take a look also at the files sd_l1.h and sd_l2.h of the library to find the meaning of the code.

FAQ

It does not compile

  • Are you using the latest version of Arduino IDE?
    • SimpleSDAudio_V1.00 need at least V1.0.1 of Arduino IDE. It has been tested and works ok also under V1.6.1.
    • SimpleSDAudio_V1.01 need at least V1.0 of Arduino IDE (remove complete block with !Serial from examples when using with Arduino IDE V1.0).

SdPlay.init fails

  • Have you selected the correct CS pin for your SD-card? Uncomment the line
    // SdPlay.setSDCSPin(10);
    in the examples and enter correct pin number here.
  • Your SD shield may be crap and SD communication is only possible with limited speed. Try commenting the line
    SPSR |= (1 << SPI2X);
    in function SD_L0_SpiSetHighSpeed(). If this doesn't help, comment out also the first line and try again. If then init works you have a bad SD card shield.

How to use 16-Bit audio?

If you want to use 16-Bit audio, you need 2 PWM outputs joined together with a resistor (see above). Select SSDA_MODE_STEREO if you want one 16-Bit output channel, select SSDA_MODE_QUAD if you want two 16-Bit output channels (works only on Mega-Arduinos or with Timer2 patch).

如需更详细资料,如有好资源分享,请加入QQ交流群:214461008

欢迎阅读,欢迎转载,不用注明出处!本站只提供入门和进阶资料,做您学习的小伙伴!

老司机也欢迎光临指导!有好内容请加群:214461008 不要看声明了,看内容吧!

相关文章
  • 移位寄存器在Arduino上的应用

    移位寄存器在Arduino上的应用

    2016-05-13 16:05

  • Arduino硬件中断详解

    Arduino硬件中断详解

    2016-05-13 14:29

  • 利用Arduino制作键盘乐器

    利用Arduino制作键盘乐器

    2016-04-26 16:11

  • 利用Arduino和Android制作智能家居系统

    利用Arduino和Android制作智能家居系统

    2016-04-26 14:34

网友点评