working audio
This commit is contained in:
106
FW/leo_muziekdoos_sam51/src/audio.cpp
Normal file
106
FW/leo_muziekdoos_sam51/src/audio.cpp
Normal file
@@ -0,0 +1,106 @@
|
||||
// Arduino Zero / Feather M0 I2S audio tone generation example.
|
||||
// Author: Tony DiCola
|
||||
//
|
||||
// Connect an I2S DAC or amp (like the UDA1334A) to the Arduino Zero
|
||||
// and play back simple sine, sawtooth, triangle, and square waves.
|
||||
// Makes your Zero sound like a NES!
|
||||
//
|
||||
// NOTE: The I2S signal generated by the Zero does NOT have a MCLK /
|
||||
// master clock signal. You must use an I2S receiver that can operate
|
||||
// without a MCLK signal (like the UDA1334A).
|
||||
//
|
||||
// For an Arduino Zero / Feather M0 connect it to you I2S hardware as follows:
|
||||
// - Digital 0 -> I2S LRCLK / FS (left/right / frame select clock)
|
||||
// - Digital 1 -> I2S BCLK / SCLK (bit / serial clock)
|
||||
// - Digital 9 -> I2S DIN / SD (data output)
|
||||
// - Ground
|
||||
//
|
||||
// Released under a MIT license: https://opensource.org/licenses/MIT
|
||||
#include "audio.h"
|
||||
|
||||
#include <I2S.h>
|
||||
#include <DMA.h>
|
||||
#include "utility/dma.h"
|
||||
#include <math.h>
|
||||
|
||||
#include "storage.h"
|
||||
|
||||
/* max volume for 32 bit data */
|
||||
#define VOLUME ( (1UL << 31) - 1)
|
||||
|
||||
/* create a buffer for both the left and right channel data */
|
||||
#define BUFSIZE 256
|
||||
int data[BUFSIZE];
|
||||
|
||||
DMA_class myDMA;
|
||||
DMAstatus stat; // DMA status codes returned by some functions
|
||||
|
||||
I2S_class i2s;
|
||||
|
||||
bool transfer_is_done = false;
|
||||
bool playbackIsDone = true;
|
||||
|
||||
void dma_callback(DMA_class *dma) {
|
||||
/* we don't need to do anything here */
|
||||
transfer_is_done = true;
|
||||
}
|
||||
|
||||
void initAudio(void)
|
||||
{
|
||||
Serial.begin(115200);
|
||||
//while(!Serial); // Wait for Serial monitor before continuing
|
||||
|
||||
Serial.println("I2S output via DMA");
|
||||
|
||||
int *ptr = data;
|
||||
|
||||
/*the I2S module will be expecting data interleaved LRLR*/
|
||||
for(int i=0; i<BUFSIZE/2; i++){
|
||||
/* create a sine wave on the left channel */
|
||||
*ptr++ = sin( (2*PI / (BUFSIZE/2) ) * i) * VOLUME;
|
||||
|
||||
/* create a cosine wave on the right channel */
|
||||
*ptr++ = cos( (2*PI / (BUFSIZE/2) ) * i) * VOLUME;
|
||||
}
|
||||
|
||||
Serial.println("Configuring DMA trigger");
|
||||
myDMA.setTrigger(I2S_DMAC_ID_TX_0);
|
||||
myDMA.setAction(DMA_TRIGGER_ACTON_BEAT);
|
||||
|
||||
Serial.print("Allocating DMA channel...");
|
||||
stat = myDMA.allocate();
|
||||
myDMA.printStatus(stat);
|
||||
|
||||
Serial.println("Setting up transfer");
|
||||
myDMA.addDescriptor(
|
||||
data, // move data from here
|
||||
#if defined(__SAMD51__)
|
||||
(void *)(&I2S->TXDATA.reg), // to here (M4)
|
||||
#else
|
||||
(void *)(&I2S->DATA[0].reg), // to here (M0+)
|
||||
#endif
|
||||
BUFSIZE, // this many...
|
||||
DMA_BEAT_SIZE_WORD, // bytes/hword/words
|
||||
true, // increment source addr?
|
||||
false);
|
||||
myDMA.loop(true);
|
||||
Serial.println("Adding callback");
|
||||
myDMA.setCallback(dma_callback);
|
||||
|
||||
/* begin I2S on the default pins. 24 bit depth at
|
||||
* 44100 samples per second
|
||||
*/
|
||||
i2s.begin(I2S_32_BIT, 44100);
|
||||
i2s.enableTx();
|
||||
|
||||
stat = myDMA.startJob();
|
||||
}
|
||||
|
||||
uint32_t filendex = 0;
|
||||
uint32_t filesize = 0;
|
||||
File musicFileHandle;
|
||||
|
||||
void handleAudio(void)
|
||||
{
|
||||
|
||||
}
|
||||
7
FW/leo_muziekdoos_sam51/src/audio.h
Normal file
7
FW/leo_muziekdoos_sam51/src/audio.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "arduino.h"
|
||||
|
||||
|
||||
void initAudio(void);
|
||||
void handleAudio(void);
|
||||
14
FW/leo_muziekdoos_sam51/src/main.cpp
Normal file
14
FW/leo_muziekdoos_sam51/src/main.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#include "audio.h"
|
||||
#include "storage.h"
|
||||
|
||||
void setup()
|
||||
{
|
||||
initAudio();
|
||||
initStorage();
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
handleAudio();
|
||||
handleStorage();
|
||||
}
|
||||
57
FW/leo_muziekdoos_sam51/src/storage.cpp
Normal file
57
FW/leo_muziekdoos_sam51/src/storage.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
#include "storage.h"
|
||||
|
||||
File root;
|
||||
|
||||
|
||||
void printDirectory(File dir, int numTabs) {
|
||||
// Begin at the start of the directory
|
||||
dir.rewindDirectory();
|
||||
|
||||
while(true) {
|
||||
File entry = dir.openNextFile();
|
||||
if (! entry) {
|
||||
// no more files
|
||||
//Serial.println("**nomorefiles**");
|
||||
break;
|
||||
}
|
||||
for (uint8_t i=0; i<numTabs; i++) {
|
||||
Serial.print('\t'); // we'll have a nice indentation
|
||||
}
|
||||
// Print the 8.3 name
|
||||
Serial.print(entry.name());
|
||||
// Recurse for directories, otherwise print the file size
|
||||
if (entry.isDirectory()) {
|
||||
Serial.println("/");
|
||||
printDirectory(entry, numTabs+1);
|
||||
} else {
|
||||
// files have sizes, directories do not
|
||||
Serial.print("\t\t");
|
||||
Serial.println(entry.size(), DEC);
|
||||
}
|
||||
entry.close();
|
||||
}
|
||||
}
|
||||
|
||||
void initStorage(void)
|
||||
{
|
||||
if(!SD.begin(A5))
|
||||
{
|
||||
Serial.println("Failed to init SDcard");
|
||||
}
|
||||
|
||||
root = SD.open("/");
|
||||
}
|
||||
|
||||
void handleStorage(void)
|
||||
{
|
||||
//printDirectory(root, 0);
|
||||
|
||||
}
|
||||
|
||||
File getmusicFile(void)
|
||||
{
|
||||
if(SD.exists("/003.WAV"))
|
||||
{
|
||||
return SD.open("/003.WAV");
|
||||
}
|
||||
}
|
||||
9
FW/leo_muziekdoos_sam51/src/storage.h
Normal file
9
FW/leo_muziekdoos_sam51/src/storage.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "SPI.h"
|
||||
#include "SD.h"
|
||||
|
||||
void initStorage(void);
|
||||
void handleStorage(void);
|
||||
|
||||
File getmusicFile(void);
|
||||
Reference in New Issue
Block a user