Hii all,
I want to mix two sound files as well as play each sound files independently. I have used switch statment mechanism (for eg. If I press 1 on serial monitor, play 1.wav, press 2, play 2.wav, press 3, mix 1.wav and 2.wav). For that, I gave commands on serial monitor.
Now, I am not able to hear mixed sound nor independent sound. I am using timer interrupt and my samples are (16 bit, 11025khz,mono). I also observed that, interrupt does not occur at regular interval. I analysed the code but coud not trace the problem. Please help and thanks in advance.
Please find the attachment of my code.
Note that variable number1 is for switch case and number2 is used for timer interrupt.
#include <SD.h>
#include <SPI.h>
#include <stdlib.h>
#include <stdio.h>
#include "sampler1.h"
Sampler1 sampler1;
// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;
File folder;
int charno;
int state = 0;
int number1,number2;
String str;
float v, number, m11, m22, m1, m2;
void setup() {
// debug output at 9600 baud
Serial.begin(9600);
// setup SD-card
Serial.print(F("Initializing SD card..."));
if (!SD.begin(4)) {
Serial.println(F(" failed!"));
while (true);
}
Serial.println(F(" done."));
//turn on the timer clock in the power management controller
pmc_set_writeprotect(false);
//we want wavesel 01 with RC
TC_Configure(TC1, 1, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK2);
TC_SetRC(TC1, 1, 953); // sets <> 11.1 Khz interrupt rate
TC_Start(TC1, 1);
// enable timer interrupts on the timer
TC1->TC_CHANNEL[1].TC_IER = TC_IER_CPCS;
TC1->TC_CHANNEL[1].TC_IDR = ~TC_IER_CPCS;
//Enable the interrupt in the nested vector interrupt controller
//TC4_IRQn where 4 is the timer number * timer channels (3) + the channel
//number (=(1*3)+1) for timer1 channel1
NVIC_EnableIRQ(TC4_IRQn);
root.openRoot(volume);
folder = SD.open("/SoundP~1/"); //1
Serial.println(F("Sound directory"));
if (printDirectory(folder, 0) == 0) {
Serial.println(F("No wav files, nothing to do!"));
while (1);
}
// Enable DAC1 Port
analogWrite(DAC1, 0);
}
void loop() {
getFile();
// Speed
Serial.println(F("Speed: "));
while (Serial.available() == 0) {
}
number = Serial.parseFloat();
Serial.println(number);
// modification of speed of wav file
sampler1.setSpeed1(number);
sampler1.setSpeed2(number);
// Volume
Serial.println(F("V: "));
while (Serial.available() == 0) {
}
v = Serial.parseFloat();
Serial.println(v);
// Modification of volume
sampler1.setVol(v);
}
void getFile() {
boolean gotname = false;
char junk = ' ';
Serial.println();
while (Serial.available() > 0) {
junk = Serial.read();
}
Serial.println(F("File: "));
while (Serial.available() == 0) {
}
number1 = Serial.parseInt();
Serial.println(number1);
number2 = number1;
switch (number1) {
case 1: { // Play 1 .wav
if (number1 != state) {
sampler1.sstop();
sampler1.sstop2();
sampler1.init();
sampler1.load();
sampler1.splay();
sampler1.buffill();
state = number1;
} else {
sampler1.splay();
sampler1.buffill();
state = number1;
}
////mixing factor
Serial.println(F("m1: "));
while (Serial.available() == 0) {
}
m1 = Serial.parseFloat();
Serial.println(m1);
break;
}
case 2 : { // play 2.wav
if (number1 != state) {
sampler1.sstop();
sampler1.sstop2();
sampler1.init();
sampler1.load();
sampler1.splay();
sampler1.buffill2();
state = number1;
} else {
sampler1.splay();
sampler1.buffill2();
state = number1;
}
Serial.println(F("m2: "));
while (Serial.available() == 0) {
}
m2 = Serial.parseFloat();
Serial.println(m2);
break;
}
case 3: { // play 1.wav and 2.wav
if (number1 != state) {
sampler1.sstop();
sampler1.sstop2();
sampler1.init();
sampler1.load();
sampler1.splay();
sampler1.buffill();
sampler1.init();
sampler1.load();
sampler1.splay();
sampler1.buffill2();
state = number1;
} else {
sampler1.splay();
sampler1.buffill();
sampler1.splay();
sampler1.buffill2();
state = number1;
}
Serial.println(F("m1: "));
while (Serial.available() == 0) {
}
m1 = Serial.parseFloat();
Serial.println(m1);
Serial.println(F("m2: "));
while (Serial.available() == 0) {
}
m2 = Serial.parseFloat();
Serial.println(m2);
break;
}
default:
Serial.println(F("No such wav file"));
break;
}
}
int printDirectory(File dir, int numTabs) {
String temp;
int fcount = 0;
while (true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
Serial.println(F("*****************************************"));
return (fcount);
break;
}
for (uint8_t i = 0; i < numTabs; i++) Serial.print(F(" "));
int mylen = strlen(entry.name()) - 4; // compare last 4 characters
if ((strcmp(entry.name() + mylen, ".WAV") == 0) || (strcmp(entry.name() + mylen, ".wav") == 0)) {
fcount++;
temp = entry.name();
temp.toLowerCase();
Serial.print(temp);
for (uint8_t i = mylen; i < 30; i++) Serial.print(F(" "));
Serial.println(entry.size(), DEC);
}
if (entry.isDirectory()) {
Serial.println(F("/"));
printDirectory(entry, numTabs + 1);
}
}
}
void TC4_Handler() //Interrupt at 11KHz rate
{
TC_GetStatus(TC1, 1);
// 2048 is the 0 value of audio out.
int16_t ulOutput;
int16_t ulOutput1 = 2048;
int16_t ulOutput2 = 2048;
int16_t ulOutput3 = 2048;
//mix factor in %
m11 = m1/100;
m22 = m2/100;
// only 1.wav
if(number2 == 1){
ulOutput1 += sampler1.output();
ulOutput = (ulOutput1 * m11 );
if(ulOutput>4095) ulOutput=4095;
}
// only 2.wav
if(number2 == 2){
ulOutput2 += sampler1.output2();
ulOutput = (ulOutput2 * m22 );
if(ulOutput>4095) ulOutput=4095;
}
// mix 1.wav and 2 .wav
if(number2 == 3){
ulOutput1 += sampler1.output();
ulOutput2 += sampler1.output2();
ulOutput = (ulOutput1 * m11) + (ulOutput2 * m22) ;
if(ulOutput>4095) ulOutput=4095;
}
dacc_write_conversion_data(DACC_INTERFACE, ulOutput);
}