Comments (4)
What is the exact error message you are getting?
Also can you post some example code that demonstrates the issue?
from autoanalogaudio.
I have not written my own code, I just change the SD.h part to SdFat and file to file32.
`/******** User Config ************************************/
#define sd_CS_PIN 4
#define AUDIO_DEBUG
#define RECORD_DEBUG
const char newWavFile[] = "test.wav";
//
#include <SPI.h>
#include <SdFat.h>
#include <sdios.h>
#include <AutoAnalogAudio.h>
//
AutoAnalog aaAudio;
SdFat32 sd;
File32 myFile;
File32 recFile;
//
#include "myWAV.h"
#include "myRecording.h"
//
void setup() {
Serial.begin(115200);
if (!sd.begin(sd_CS_PIN)) {
Serial.println("sd init failed!");
return;
}
Serial.println("sd ok\nAnalog Audio Begin");
aaAudio.begin(1, 1); // Start AAAudio with ADC & DAC
aaAudio.autoAdjust = 0; // Disable automatic timer adjustment
}
/*********************************************************/
uint32_t displayTimer = 0;
void loop() {
if (millis() - displayTimer > 1000) {
displayTimer = millis();
if (counter) {
Serial.print("Samples per Second: ");
Serial.println(counter * MAX_BUFFER_SIZE);
}
counter = 0;
}
if (Serial.available()) {
char input = Serial.read();
switch (input) {
case '1': playAudio("M8b24kM.wav"); break; //Play a *.wav file by name - 8bit, 24khz, Mono
case '2': playAudio("M8b24kS.wav"); break; //Play 8bit, 24khz, Stereo
case '3': playAudio("M16b24kS.wav"); break; //Play 16bit, 24khz, Stereo
case '4': playAudio("M8b44kST.wav"); break; //Play 8bit, 44khz, Stereo
case '5': channelSelection = 0; break; //Play the audio on DAC0
case '6': channelSelection = 1; break; //Play the audio on DAC1
case '7': channelSelection = 2; break; //Play the audio on DAC0 & DAC1
case '8': Serial.println("OK"); break;
case '9': startRecording(newWavFile, 11000); break; //Start recording @11khz,8-bit,Mono
case '0': stopRecording(newWavFile, 11000); break; //Stop the recording and finalize the file
case 'p': playAudio(newWavFile); break; //Play back the recorded audio
case 'D': sd.remove(newWavFile); break; //Delete the file and start fresh
}
}
}
this is the main code then this is for the myWav
uint8_t channelSelection = 0;
void loadBuffer();
/*********************************************************/
void DACC_Handler(void) {
aaAudio.feedDAC(channelSelection); //Feed the DAC with the data loaded into the dacBuffer
aaAudio.dacHandler(); //Link the DAC ISR/IRQ to the library. Called by the MCU when DAC is ready for data
loadBuffer();
}
/********************************************************/
/ Function to open the audio file, seek to starting position and enable the DAC */
void playAudio(const char *audioFile) {
uint32_t sampleRate = 16000;
uint16_t numChannels = 1;
uint16_t bitsPerSample = 8;
uint32_t dataSize = 0;
uint32_t startPosition = 44;
if (recFile) {
aaAudio.adcInterrupts(false);
recFile.close();
}
if (myFile) {
aaAudio.disableDAC();
myFile.close();
//delay(25);
}
//Open the designated file
myFile = sd.open(audioFile);
if (myFile) {
myFile.seek(22);
myFile.read((byte*)&numChannels, 2);
myFile.read((byte*)&sampleRate, 4);
myFile.seek(34);
myFile.read((byte*)&bitsPerSample, 2);
myFile.seek(40);
myFile.read((byte*)&dataSize, 4);
dataSize += 44; //Set this variable to the total size of header + data
#if defined (AUDIO_DEBUG)
Serial.print("\nNow Playing ");
Serial.println(audioFile);
Serial.print("Channels ");
Serial.print(numChannels);
Serial.print(", SampleRate ");
Serial.print(sampleRate);
Serial.print(", BitsPerSample ");
Serial.println(bitsPerSample);
#endif
if (myFile.size() > dataSize) {
startPosition = myFile.size() - dataSize;
#if defined (AUDIO_DEBUG)
Serial.println("Skipping metadata");
#endif
}
if (bitsPerSample > 10 ) {
bitsPerSample = 12;
} else if (bitsPerSample > 8) {
bitsPerSample = 10;
} else {
bitsPerSample = 8;
}
sampleRate *= numChannels;
aaAudio.dacBitsPerSample = bitsPerSample;
aaAudio.setSampleRate(sampleRate);
#if defined (AUDIO_DEBUG)
Serial.print("Timer Rate ");
Serial.print(sampleRate);
Serial.print(", DAC Bits Per Sample ");
Serial.println(bitsPerSample);
#endif
//Skip past the WAV header
myFile.seek(startPosition);
//Load one buffer
loadBuffer();
//Feed the DAC to start playback
aaAudio.feedDAC();
} else {
#if defined (AUDIO_DEBUG)
Serial.print("Failed to open ");
Serial.println(audioFile);
#endif
}
}
/********************************************************/
/ Function called from DAC interrupt after dacHandler(). Loads data into the dacBuffer */
void loadBuffer() {
if (myFile) {
if (myFile.available()) {
if (aaAudio.dacBitsPerSample == 8) {
//Load 32 samples into the 8-bit dacBuffer
myFile.read((byte*)aaAudio.dacBuffer, MAX_BUFFER_SIZE);
} else {
//Load 32 samples (64 bytes) into the 16-bit dacBuffer
myFile.read((byte*)aaAudio.dacBuffer16, MAX_BUFFER_SIZE * 2);
//Convert the 16-bit samples to 12-bit
for (int i = 0; i < MAX_BUFFER_SIZE; i++) {
aaAudio.dacBuffer16[i] = (aaAudio.dacBuffer16[i] + 0x8000) >> 4;
}
}
} else {
#if defined (AUDIO_DEBUG)
Serial.println("File close");
#endif
myFile.close();
aaAudio.disableDAC();
}
}
}
and myRecording
/*********************************************************/
/* WAV HEADER STRUCTURE */
struct wavStruct {
const char chunkID[4] = {'R', 'I', 'F', 'F'};
uint32_t chunkSize = 36; //Size of (entire file in bytes - 8 bytes) or (data size + 36)
const char format[4] = {'W', 'A', 'V', 'E'};
const char subchunkID[4] = {'f', 'm', 't', ' '};
const uint32_t subchunkSize = 16;
const uint16_t audioFormat = 1; //PCM == 1
uint16_t numChannels = 1; //1=Mono, 2=Stereo
uint32_t sampleRate = 11000;
uint32_t byteRate = 11000; //== SampleRate * NumChannels * BitsPerSample/8
uint16_t blockAlign = 1; //== NumChannels * BitsPerSample/8
uint16_t bitsPerSample = 8; //8,16,32...
const char subChunk2ID[4] = {'d', 'a', 't', 'a'};
uint32_t subChunk2Size = 0; //== NumSamples * NumChannels * BitsPerSample/8
//Data //The audio data
};
//
uint32_t counter = 0;
//
void ADC_Handler(void) { //ADC Interrupt triggered by ADC sampling completion
aaAudio.getADC();
if (recFile) {
recFile.write(aaAudio.adcBuffer, MAX_BUFFER_SIZE); //Write the data to sd as it is available
counter++;
}
}
/*********************************************************/
void startRecording(const char *fileName, uint32_t sampleRate) {
#if defined (RECORD_DEBUG)
Serial.print("Start Recording: ");
Serial.println(fileName);
#endif
if (recFile) {
aaAudio.adcInterrupts(false);
recFile.close();
}
if (myFile) { //Close any open playback files & disable the DAC
aaAudio.disableDAC();
myFile.close();
}
recFile = sd.open(fileName, FILE_WRITE); //Open the file for writing
if (!recFile) {
#if defined (RECORD_DEBUG)
Serial.println("Failed to open file");
#endif
return;
}
recFile.seek(0); //Write a blank WAV header
uint8_t bb = 0;
for (int i = 0; i < 44; i++) {
recFile.write(bb);
}
aaAudio.adcBitsPerSample = 8; //Configure AAAudio
aaAudio.setSampleRate(sampleRate);
aaAudio.getADC();
aaAudio.getADC();
aaAudio.adcInterrupts(true);
}
/*********************************************************/
void createWavHeader(const char *fileName, uint32_t sampleRate ) {
if (!sd.exists(fileName)) {
#if defined (RECORD_DEBUG)
Serial.println("File does not exist, please write WAV/PCM data starting at byte 44");
#endif
return;
}
recFile = sd.open(fileName, FILE_WRITE);
if (recFile.size() <= 44) {
#if defined (RECORD_DEBUG)
Serial.println("File contains no data, exiting");
#endif
recFile.close();
return;
}
wavStruct wavHeader;
wavHeader.chunkSize = recFile.size() - 8;
//wavHeader.numChannels = numChannels;
wavHeader.sampleRate = sampleRate;
wavHeader.byteRate = sampleRate * wavHeader.numChannels * wavHeader.bitsPerSample / 8;
wavHeader.blockAlign = wavHeader.numChannels * wavHeader.bitsPerSample / 8;
//wavHeader.bitsPerSample = bitsPerSample;
wavHeader.subChunk2Size = recFile.size() - 44;
#if defined (RECORD_DEBUG)
Serial.print("WAV Header Write ");
#endif
recFile.seek(0);
if ( recFile.write((byte*)&wavHeader, 44) > 0) {
#if defined (RECORD_DEBUG)
Serial.println("OK");
} else {
Serial.println("Failed");
#endif
}
recFile.close();
}
/*********************************************************/
void stopRecording(const char *fileName, uint32_t sampleRate) {
aaAudio.adcInterrupts(false); //Disable the ADC interrupt
recFile.close(); //Close the file
createWavHeader(fileName, sampleRate); //Add appropriate header info, to make it a valid *.wav file
#if defined (RECORD_DEBUG)
Serial.println("Recording Stopped");
#endif
}
`
I am not sure is it because I did not have #define SPI_CLOCK SD_SCK_MHZ(21)
or I just simpliy misunderstood your code.
from autoanalogaudio.
Yeah, according to my notes with SDFat on the Due you need to configure the SPI frequency down to 10Mhz. I think you can call if !SD.begin(SD_CS_PIN,SPI_DIV6_SPEED); or something like that, but I can't remember exactly how I did it.
from autoanalogaudio.
okay, I will try... thank you :D
from autoanalogaudio.
Related Issues (20)
- Useing AAAudio for rec sound's from mic with esp32 HOT 9
- Can we get support esp8266 board? HOT 3
- Record sound from esp-cam HOT 1
- How Pin use to record audio on esp32 in AAAudio library? HOT 1
- Use on new Nano33BLE format boards HOT 1
- Low / Distorted Input and Output HOT 1
- Does it supports esp8266? HOT 1
- I can't make it stream over radio nrf24l01. HOT 3
- Which code Example can i use to record sound with the Arduino Due HOT 2
- Does the library Support Audio recording on an STM32 Board/Architecture HOT 1
- Example Confusion of hardware definition HOT 2
- AutoAnalogAudio.h conflicts with Audio.h library HOT 4
- Problem with ADC read HOT 4
- Compile error for ESP32-S3 HOT 1
- max 9814 record HOT 2
- Recording speed HOT 9
- Microphone problem HOT 1
- Undetected type when using Arduino IDE HOT 3
- problems with esp32 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from autoanalogaudio.