Änderungen

Aus Hackerspace Ffm
Wechseln zu: Navigation, Suche

SimpleSDAudio

4.503 Byte hinzugefügt, 17:35, 7. Apr. 2015
/* It does not compile */
<div style="clear: both; border-width:0px; border-color:#333300; border-style:solid; padding:5px; background-color:#FFEFEF; margin:1em; margin-left:5em; margin-right:5em;">
Please note: If you got here by clicking a link from internet parasites like "gotlinks" that force you to install software that takes over you computer or force you to do other things that you don't want to just to get the correct link to this free site, please be aware that we are NOT responsible for that! Feel free to correct such links if you have access to those sites. </div>
 
[[Datei:SSDA Minimal.jpg|320px|right|thumb|Minimal setup for SimpleSDAudio (here without level-shifters - only for Arduino running at 3.3V)]]
[[Datei:SSDA Arduino Ethernet.jpg|320px|right|thumb|Minimal setup for SimpleSDAudio with Arduino Ethernet - loudspeaker connected via 100uF capacitor to audio output]]
* Integrated SD library (minimal FAT only, optimized for low RAM usage and high performance)
* Works with most SD card shields that are hooked to SPI port
* Easy to use API: 1. init library, 2. select audio file, 3. call play, 4. call worker while audio is playing
* Supports SD and SDHC cards formated with FAT16 or FAT32
* Combination of fullrate and quadro-output actually leads to buffer underruns
* Minimum controller required: ATmega368. ATmega168 is too low on RAM.
 
=== Alternatives ===
Now there is an alternative library called '''TMRpcm''' that does something similar: https://github.com/TMRh20/TMRpcm/wiki . This is a little bit easier to use (no conversion of WAV file needed). Here a table with the differences:
{| class="wikitable"
!Feature!!SimpleSDAudio!!TMRpcm
|-
|Highest sampling rate||X||-
|-
|Native WAV playback (no file conversion needed)||-||X
|-
|Highly speed-optimized code (using Assembler for audio core)||X||-
|-
|Using size-optimized minimal SD-FAT library||X||-
|-
|Universal SD library in the background supporting fragmented files and many other uses||-||X
|-
|Stereo operation possible||X||X
|-
|Quad speaker operation possible||X||X
|-
|Volume setting support||-||X
|}
So depending on your needs, try the library that best fit your needs. TMRpcm might be easier for beginners. Both libraries should work with the same connections. You can take the information about the connections from here and use the TMRpcm library with that.
 
You want even more, multi-channel audio playback, digital filters, mixers, DSP functions? Then take a look at the [http://www.pjrc.com/teensy/td_libs_Audio.html Teensy 3.1 Audio Library] using a serious (but still affordable) micro controller but still using the Arduino IDE.
= Download =
Latest version:
* [[Datei:SimpleSDAudio_V1.0203.zip]]
Old versions:
* [[Datei:SimpleSDAudio_V1.02.zip]]
* [[Datei:SimpleSDAudio_V1.01.zip]]
* [[Datei:SimpleSDAudio_V1.00.zip]]
* Copy the file /libraries/SimpleSDAudio/examples/EXAMPLE.AFM to root folder of a freshly formated SD card (don't use quick format!).
* Connect SD card to your Arduino board (using shield or whatever, SD card's chip select should go to pin 4, all other to SPI pins).
* Connect a speaker or headphone via 100 ohm resistor in series to audio output pin (pin 9 on Arduinos with ATmega168/328ATmega328, pin 44 on Arduinos with ATmega1280/2560). Connect other end of speaker to GND.
* Launch Arduino IDE and try example "BareMinimumWithDebug" first.
* You should hear the audio (plays just a few seconds). Activate serial monitor to find some information if it does not work. Maybe you have to adjust SD cards CS pin in sketch.
=== Windows users ===
For Windows users should download from there the ZIP distribution and unzip it to Sox binaries are already part of the library located in the libraries/SimpleSDAudio/tools folder. Afterwards they can Just drag and drop .wav files to the appropriate batch files to start the conversion. The converted files will end up in folder "converted".
=== Linux users ===
== Hardware setup ==
=== SD card connection ===The SD card should be connected to the SPI port of the controller. The chip select pin from the card can be connected to any free digital pin, but if pin 4 is used on Arduinos you don't have to adjust the source code. Many shields with SD card support like Ethernet-Shield will work. You need pegel level conversion circuits from 5V to 3.3V for most Arduinos (except those that run natively on 3.3V)- three resistor dividers are enough. Look at the circuit diagram below how to do it.
According your mode configuration, one or two pins are used for audio output. This pins can not be changed and are different between different Arduino boards. For ATmega168/328 based plattforms those are pins 9 and 10. For ATmega1280/2560 those are pins 44 and 45. For other plattforms look into the file SimpleSDAudio.h.
<gallery widths=300px heights=150px perrow=2>
Datei:SD_Card_Connection.png|SD card connection with level shifting to standard Arduinos
</gallery>
 
=== Audio output connection ===
Be careful that the audio output pins are digital ports that carry a positive voltage between 0V and 5V. It is not a good idea to have a DC voltage at line-inputs or smaller speakers as there will be a steady current flow. Therefore at least a resistor and/or a capacitor should be connected in series. For a start use at least 100 ohm or a capacitor between 100nF and 100uF. For line inputs use a voltage divider or poti to reduce the voltage.
<gallery widths=300px heights=150px perrow=32>
Datei:SSDA SimpleConnections.png|Examples for simple 8-bit audio outputs
Datei:SSDA Simple16bit.png|Rising the audio resolution to something near 16-bit
Datei:SSDA Better16bit.png|Still unexpensive solution to get nice 16-bit audio output
</gallery>
 The 16-bit output is done by operating two 8-bit outputs together: One will provide an 8-bit signal containing the higher 8-bits and the other one contains the lower 8-bits. If you listen to each of those channels separately, you will hear the usual audio on the upper 8-bit outputs but with noticable 8-bit noise especially on quiet audio parts. On the lower 8-bit you will hear just noise. But when you add this noise (reduced in volume by a factor of 256th done by the resistors) to the higher 8-bits then something magic will happen: The 8-bit noise will vanish (but unfortunately often a lot of noise originating from power supply might still be left). === Audio amplifier for loudspeakers ===If you want more power, you can build a cheap class-D PWM amplifier based on the 74AC14 (hex Schmitt-trigger inverters). The coils are optional but should be used for RF reject when long cables are used.<gallery widths=300px heights=150px perrow=2>Datei:PWM 8Bit MonoAmplifier Steckplatine.png|8-bit mono amplifier on breadboardDatei:PWM 8Bit MonoAmplifier Schaltplan.png|8-bit mono amplifier circuitDatei:PWM 16Bit Amplifier Steckplatine.png|16-bit stereo amp on breadboard, use trimmer to adjust for lowest noiseDatei:PWM 16Bit Amplifier Schaltplan.png|16-bit stereo amp circuit</gallery> === Build your own SD breadboard adapter ===
Follow those pictures to build a very cheap SD-card adapter.
[[<gallery widths=600px heights=300px perrow=1>Datei:BuildSDBreadboardHolder_Step1.jpg|frame|left|You need one single-row and one double-row pinheader cut to 7 pins each.]]</gallery> <gallery widths=300px heights=300px perrow=23>
Datei:BuildSDBreadboardHolder_Step2.jpg|Using pliers to bend one side of pins < shape. Don't bend other side.
Datei:BuildSDBreadboardHolder_Step3.jpg|Bend all 7 pins of one row, leave other row non bend. You should end up like this:
Here is an overview of the constants used in the library.
<pre>
#define SSDA_VERSIONSTRING "1.0203"
// Sound Mode Flags
#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
==== init() ====
<pre>boolean init(uint8_t soundMode);</pre>
Call this to initialze initialize the library and set sound mode. Thisfunction will also aquire acquire the needed buffer (if not
already set manually using setWorkBuffer), initialize SD card
and sets up all used pins.
Output channel configuration flags
* SSDA_MODE_MONO - Use only 1st PWM pin
* SSDA_MODE_STEREO - Use both PWM pins for stereo outputor 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 successfullsuccessful, false if an erroroccuredoccurred. 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.
providing the filename in 8.3 format.
The function return true if successfullsuccessful, 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.
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 <pre>SSDA_MODE_AUTOWORKER</pre>
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() ====
== 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).
* Have you selected the correct CS pin for your SD-card? Uncomment the line <pre>// SdPlay.setSDCSPin(10);</pre> 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 <pre>SPSR |= (1 << SPI2X);</pre> 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).
1.993
Bearbeitungen