VS1053b making noise when recording

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
hongduozhou
 
Posts: 3
Joined: Fri Sep 12, 2014 5:48 pm

VS1053b making noise when recording

Post by hongduozhou »

Hi,
I am using VS1053b Breakout and Arduino Pro Mini in a project which is required record and playback in a same sketch.
I successfully made it using Electret Microphone Amplifier instead of the self-made MIC which recommended by the tutorial. I linked the AUD pin from the Microphone amplifier to the MIC+ on the VS1053b. And made the sketch "musicPlayer.startRecordOgg(false);" to make it using the LINE1. http://forums.adafruit.com/viewtopic.ph ... ic#p256995

The record sound quality is pretty good. However when it is recording the speaker makes loud and sharp feedback.

I am using a Mono Audio Amp Breakout to amplify the speaker instead of a headphone jack.
I found that I still can use Mic instead of LINE1 which is "musicPlayer.startRecordOgg(true);" . And the feedback disappeared. But the record sound quality if much worse than the LINE1.

How can I erase the speakers feedback when I use LINE1?
Or how can I level up the record sound quality if I have to use MIC rather tha LINE1?

User avatar
adafruit_support_mike
 
Posts: 67454
Joined: Thu Feb 11, 2010 2:51 pm

Re: VS1053b making noise when recording

Post by adafruit_support_mike »

Are you playing the audio input through the speakers at the same time you're recording it?

User avatar
hongduozhou
 
Posts: 3
Joined: Fri Sep 12, 2014 5:48 pm

Re: VS1053b making noise when recording

Post by hongduozhou »

adafruit_support_mike wrote:Are you playing the audio input through the speakers at the same time you're recording it?
Thank you for reply!

I added "musicPlayer.stopPlaying();" before upload the record plug-in in recording section.
The feed back is like this during recording: when I speaking towards the microphone, the speaker gives a little feedback of my voice. But when I stopped speaking, the speaker is like playing a very sharp and loud whistle sound. I tried to link the speaker as far as possible to the microphone, and 6 inches away, the speaker will not give feedback.



Code: Select all

#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
#include <PinChangeInt.h>

// define the pins used
#define RESET 9      // VS1053 reset pin (output)
#define CS 10        // VS1053 chip select pin (output)
#define DCS 8        // VS1053 Data/command select pin (output)
#define CARDCS 4     // Card chip select pin
#define DREQ 3       // VS1053 Data request, ideally an Interrupt pin

#define REC_BUTTON 7
#define ledPin   6  
#define ledPinRec 2
Adafruit_VS1053_FilePlayer musicPlayer = Adafruit_VS1053_FilePlayer(RESET, CS, DCS, DREQ, CARDCS);

File recording;  // the file we will save our recording to
#define RECBUFFSIZE 128  // 64 or 128 bytes.
uint8_t recording_buffer[RECBUFFSIZE];
extern bool isRecording = false;
char uid[20];
char * uid2audio[][2] = {
  {
    "720040CFA15C","RECORD02.OGG"  }
  ,{
    "75007358DB85","RECORD03.OGG"  }
  ,{
    "75007361583F","RECORD04.OGG"  }
  ,{
    "6A0049C07291","RECORD05.OGG"  }
  ,{
    "6A0049F778AC","RECORD06.OGG"  }
   
  
};
byte val;
//byte index = 0;
char filename[15];
#define RFIDResetPin 5

//debounce declare.....

int PlayState = HIGH;
int RecState = LOW; // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers


char tagString[15] ;

void RecordingMode(){}

void setup() {
  Serial.begin(9600);
  pinMode(REC_BUTTON, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(ledPinRec, OUTPUT);
  digitalWrite(ledPin, HIGH);
  digitalWrite(ledPinRec,LOW);
  musicPlayer.begin(); // initialise the music player
  SD.begin(CARDCS);    // initialise the SD card
  musicPlayer.setVolume(10,10);
  musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT); // timer int 
  //musicPlayer.playFullFile("RECORD001.OGG");
  
 PCintPort::attachInterrupt(REC_BUTTON, &RecordingMode, CHANGE);
}


 
void loop(){
  //if(!SD.open("v44k1q05.img"))Serial.println("hahaha");
  
 //if there is a tag  .....
  //Serial.println(uid2audio[0][1]);
  int index = 0;
  boolean reading = false;

    
     int readingbutton = digitalRead(REC_BUTTON);
  if (readingbutton == HIGH && previous == LOW && millis() - time > debounce) {
    //.... playmode&&&recordmode
 
    if (PlayState == HIGH){
      PlayState = LOW;
      RecState = HIGH;
      digitalWrite(ledPin, LOW);
      digitalWrite(ledPinRec,HIGH);
       
      Serial.println("Recording When Swipe Tag");
    //plugin = true;
  }
   else if(PlayState == LOW && isRecording == false){
      PlayState = HIGH;
      RecState = LOW;
      digitalWrite(ledPin, HIGH);
      digitalWrite(ledPinRec,LOW);
     // musicPlayer.prepareRecordOgg(false);
     
      Serial.println("Playing When Swipe Tag");
    }
    
     //.... playmode&&&recordmode
    time = millis(); 
    
    
    
  }
  previous = readingbutton;

  
    
  while(Serial.available()){
    //Serial.println("read");
    int readByte = Serial.read(); //read next available byte
    if(readByte == 2) reading = true; //begining of tag
    if(readByte == 3){
    reading = false; //end of tag
    //break;
    }
    if(reading && readByte != 2 && readByte != 10 && readByte != 13){
      tagString[index] = readByte;
      index ++; 
       //Serial.println(tagString); 
    }
    //checktagfunc();
  }
  
//

  if(strlen(tagString)>0 ) //return; //empty, no need to contunue
  { //Serial.print("this is what i read ");
    //Serial.println(tagString); 
    //int i = 0;
    //strcpy(filename, uid2audio[i][1]);
    
    checktagfunc();
     
    if(PlayState == HIGH)
    {
      Serial.print("Playing ");
      Serial.println(filename);
       blinkled(ledPin);
      
    if(!musicPlayer.playFullFile(filename))Serial.println("caonima");

    clearTag(tagString);
    
  }
    
 
    if(isRecording ==false && RecState == HIGH){ 
      musicPlayer.stopPlaying();
        if (! musicPlayer.prepareRecordOgg("v44k1q05.img")) {
     Serial.println("Couldn't load plugin!");
     while (1);    
  }
      if(!SD.remove(filename))Serial.println("cannot remove file");
      isRecording = true;
    blinkled(ledPinRec);
      recording = SD.open(filename, FILE_WRITE);
       if (! recording) {
       Serial.println("Couldn't open file to record!");
       while (1);
    }
      musicPlayer.startRecordOgg(false);
      //
    }
   if (isRecording){
    saveRecordedData(isRecording);
   //

   }
   //Serial.println(readingbutton);
   
   if(isRecording == true&&readingbutton==HIGH){
      PlayState = LOW;
      RecState = HIGH;
      digitalWrite(ledPin, LOW);
      digitalWrite(ledPinRec,HIGH);
      
    
      Serial.println("End recording");
      musicPlayer.stopRecordOgg();
      isRecording = false;
      // flush all the data!
      saveRecordedData(isRecording);
      // close it up
      recording.close();
      delay(1000);
        
      Serial.println("Finish recording");
      clearTag(tagString);
      musicPlayer.softReset();
   
    }
  }
    
  
     
      
      
 
 
  //.....if button is pressed
//Serial.println(digitalRead(REC_BUTTON));
//Serial.println(digitalRead(REC_BUTTON));
  // //Clear the char of all value
  resetReader(); //eset the RFID reader
  delay(100);



}

void blinkled(int x){
 
      

    digitalWrite(x,HIGH);       
    delay(250);  
  
 digitalWrite(x,LOW);       
    delay(250);       

    digitalWrite(x,HIGH);       
    delay(250);  
  
 digitalWrite(x,LOW);       
    delay(250);   
        digitalWrite(x,HIGH);       
    delay(250);  
  
 digitalWrite(x,LOW);       
    delay(250);   
     digitalWrite(x,HIGH);       
    delay(250);

 Serial.print(".");
}


char *mystrcpy(char *d, const char *s)
{
   char *saved = d;
   while (*s)
   {
       *d++ = *s++;
   }
   *d = 0;
   return saved;
}


void checktagfunc(){

  //strcpy(filename, "RECORD03.OGG");
  //int i = 0;
  //while(i<8)
for(int i = 0; i<8; i++) 
{
    //Serial.println(uid2audio[i][1]);
    if(compareTag(tagString,uid2audio[i][0])){
      mystrcpy(filename, uid2audio[i][1]);
     // 
      //Serial.println(filename);         
      break;
      }
      
     
 // i++;  
  }
  
}

void pluginfunc(){
  
}


void resetReader(){
  ///////////////////////////////////
  //Reset the RFID reader to read again.
  ///////////////////////////////////
  digitalWrite(RFIDResetPin, LOW);
  digitalWrite(RFIDResetPin, HIGH);
  //delay(150);
}



void clearTag(char one[]){
  ///////////////////////////////////
  //clear the char array by filling with null - ASCII 0
  //Will think same tag has been read otherwise
  ///////////////////////////////////
  Serial.println("Tag Cleared");
  
  for(int i = 0; i < strlen(one); i++){
    one[i] = 0;
  }
}

boolean compareTag(const char one[], const char two[]){
  ///////////////////////////////////
  //compare two value to see if same,
  //strcmp not working 100% so we do this
  ///////////////////////////////////

  if(strlen(one) == 0) return false; //empty

  for(int i = 0; i < 12; i++){
    if(one[i] != two[i]) return false;
  }

  return true; //no mismatches
}

uint16_t saveRecordedData(boolean isrecord) {
  uint16_t written = 0;

  // read how many words are waiting for us
  uint16_t wordswaiting = musicPlayer.recordedWordsWaiting();

  // try to process 256 words (512 bytes) at a time, for best speed
  while (wordswaiting > 256) {
    //Serial.print("Waiting: "); Serial.println(wordswaiting);
    // for example 128 bytes x 4 loops = 512 bytes
    for (int x=0; x < 512/RECBUFFSIZE; x++) {
      // fill the buffer!
      for (uint16_t addr=0; addr < RECBUFFSIZE; addr+=2) {
        uint16_t t = musicPlayer.recordedReadWord();
        //Serial.println(t, HEX);
        recording_buffer[addr] = t >> 8; 
        recording_buffer[addr+1] = t;
      }
      if (! recording.write(recording_buffer, RECBUFFSIZE)) {
        Serial.print("Couldn't write "); 
        Serial.println(RECBUFFSIZE); 
        while (1);
      }
    }
    // flush 512 bytes at a time
    recording.flush();
    written += 256;
    wordswaiting -= 256;
  }

  wordswaiting = musicPlayer.recordedWordsWaiting();
  if (!isrecord) {
    Serial.print(wordswaiting); 
    Serial.println(" remaining");
    // wrapping up the recording!
    uint16_t addr = 0;
    for (int x=0; x < wordswaiting-1; x++) {
      // fill the buffer!
      uint16_t t = musicPlayer.recordedReadWord();
      recording_buffer[addr] = t >> 8; 
      recording_buffer[addr+1] = t;
      if (addr > RECBUFFSIZE) {
        if (! recording.write(recording_buffer, RECBUFFSIZE)) {
          Serial.println("Couldn't write!");
          while (1);
        }
        recording.flush();
        addr = 0;
      }
    }
    if (addr != 0) {
      if (!recording.write(recording_buffer, addr)) {
        Serial.println("Couldn't write!"); 
        while (1);
      }
      written += addr;
    }
    musicPlayer.sciRead(VS1053_SCI_AICTRL3);
    if (! (musicPlayer.sciRead(VS1053_SCI_AICTRL3) & _BV(2))) {
      recording.write(musicPlayer.recordedReadWord() & 0xFF);
      written++;
    }
    recording.flush();
  }

  return written;
}

User avatar
adafruit_support_mike
 
Posts: 67454
Joined: Thu Feb 11, 2010 2:51 pm

Re: VS1053b making noise when recording

Post by adafruit_support_mike »

You do need to keep the microphone well away from the speaker, or shut off the output while recording.

If the microphone picks up sound from the speakers, it will create a feedback loop through the amplifier. Since there's always some amount of delay through the amplifier, and it takes a few milliseconds for sound from the speaker to reach the microphone, the feedback loop will resonate at whatever frequency matches the delay.

User avatar
hongduozhou
 
Posts: 3
Joined: Fri Sep 12, 2014 5:48 pm

Re: VS1053b making noise when recording

Post by hongduozhou »

adafruit_support_mike wrote:You do need to keep the microphone well away from the speaker, or shut off the output while recording.

If the microphone picks up sound from the speakers, it will create a feedback loop through the amplifier. Since there's always some amount of delay through the amplifier, and it takes a few milliseconds for sound from the speaker to reach the microphone, the feedback loop will resonate at whatever frequency matches the delay.
I successfully shut down the amplifier. Then the speaker also shut down.
The problem is solved.
Thank you!

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”