record voice

General discussion on mikroBasic.
Post Reply
Author
Message
zamfir doru
Posts: 6
Joined: 12 Mar 2017 19:32

record voice

#1 Post by zamfir doru » 08 Jun 2017 13:34

hi !
i can record voice on sd with MP3 click
i make a WAV header
the wav si empty

can someone help me ?


Code: Select all

 #include <built_in.h>



// CODEC V1053E connections
sbit XCS_Direction          at TRISE0_bit;
sbit XCS                    at LATE0_bit;
sbit MP3_RST_Direction        at TRISC0_bit;
sbit MP3_RST                   at LATC0_bit;
sbit DREQ_Direction            at TRISA0_bit;
sbit DREQ                      at TRISA0_bit;
sbit XDCS_Direction           at TRISB0_bit;
sbit XDCS                    at LATB0_bit;

// VS1053E constants
const char WRITE_CODE           = 0x02;
const char READ_CODE            = 0x03;
const unsigned int VS_RESET        = 0x0004;
const unsigned int VS_RECORDING    = 0x1000;
const unsigned int VS_MIC1         = 0xBFFF;
const unsigned int VS_LINE1        = 0x4000;
const char SCI_BASE_ADDR        = 0x00;
const char SCI_MODE_ADDR        = 0x00;
const char SCI_STATUS_ADDR      = 0x01;
const char SCI_BASS_ADDR        = 0x02;
const char SCI_CLOCKF_ADDR      = 0x03;
const char SCI_DECODE_TIME_ADDR = 0x04;
const char SCI_AUDATA_ADDR      = 0x05;
const char SCI_WRAM_ADDR        = 0x06;
const char SCI_WRAMADDR_ADDR    = 0x07;
const char SCI_HDAT0_ADDR       = 0x08;
const char SCI_HDAT1_ADDR       = 0x09;
const char SCI_AIADDR_ADDR      = 0x0A;
const char SCI_VOL_ADDR         = 0x0B;
const char SCI_AICTRL0_ADDR     = 0x0C;
const char SCI_AICTRL1_ADDR     = 0x0D;
const char SCI_AICTRL2_ADDR     = 0x0E;
const char SCI_AICTRL3_ADDR     = 0x0F;

// MMC module connections
sbit Mmc_Chip_Select_Direction at TRISE1_bit;
sbit Mmc_Chip_Select           at LATE1_bit;  // for writing to output pin always use latch (PIC18 family)
sbit Mmc_Card_Detect_Direction at TRISA1_bit;
sbit Mmc_Card_Detect           at RA1_bit;    // for detecting microSD card presence

// Filename
char filename[14]         = "TEST8.WAV";

/// HEADER WAV - As per exanple provided in datasheet
char Chunk_ID[4] = {'R','I','F','F'};
char Chunk_Size[4] =  { 0xff, 0xff, 0xff,0xff};
char Format[4] = {'W','A','V','E'};
char SubChunk [4] = {'f','m','t',' '};
char SubChunk_Size [4] = {0x10, 0x00, 0x00, 0x00};
char Audio_Format [2] = { 0x01, 0x00};
char NumOfChannels [2] = { 0x02, 0x00};
char Sample_Rate [4] = { 0x44, 0xAC, 0x00,0x00};
char Byte_Rate [4] = { 0x10, 0xB1, 0x02,0x00};
char Block_Align [2] = { 0x04, 0x00};
char BitsperSample [2] = { 0x10, 0x00};
char SubChunk3ID [4] = { 'd','a','t','a'};
char SubChunk3ID_size [4] = { 0xff,0xff,0xff,0xff};

 // Other variables

unsigned long file_size;
const BYTES_2_WRITE = 32;
const BUFFER_SIZE = 448;
char mp3_buffer[BUFFER_SIZE];


 // Prototypes

void MP3_Init();
void MP3_Play();
void MP3_SCI_Write(char address, unsigned int data_in);
void MP3_SCI_Read(char start_address, char words_count, unsigned int *data_buffer);
void MP3_SDI_Write(char data_);
void MP3_SDI_Write_32(char *data_);
void MP3_Set_Volume(char left, char right);
void MP3_Check_Volume();

// Creates new file and writes some data to it
void M_Create_New_File() {

  Mmc_Fat_Set_File_Date(2011,1,12,11,9,0);    // Set file date & time info
  Mmc_Fat_Assign(&filename, 0xA0);            // Find existing file or create a new one
  Mmc_Fat_Rewrite();                          // To clear file and start with new data

/// WRITE WAV HEAD
    Mmc_Fat_Write(Chunk_ID, 4);
    Mmc_Fat_Write(Chunk_Size, 4);
    Mmc_Fat_Write(Format, 4);
    Mmc_Fat_Write(SubChunk, 4);
    Mmc_Fat_Write(SubChunk_Size, 4);
    Mmc_Fat_Write(Audio_Format, 2);
    Mmc_Fat_Write(NumOfChannels, 2);
    Mmc_Fat_Write(Sample_Rate, 4);
    Mmc_Fat_Write(Byte_Rate, 4);
    Mmc_Fat_Write(Block_Align, 2);
    Mmc_Fat_Write(BitsperSample, 2);
    Mmc_Fat_Write(SubChunk3ID, 4);
    Mmc_Fat_Write(SubChunk3ID_size, 4);

}


void MP3_Init() {

  XCS_Direction  = 0;               // Configure XCS as output
  XCS            = 1;               // Deselect XCS
  MP3_RST_Direction = 0;               // Configure MP3_RST as output
  MP3_RST           = 1;               // Set MP3_RST pin

  DREQ_Direction    = 1;               // Configure DREQ as input
  XDCS_Direction   = 0;               // Configure XDCS as output
  XDCS             = 0;               // Clear XDCS

  Mmc_Chip_Select_Direction = 0;
  Mmc_Card_Detect_Direction = 1;
  Mmc_Chip_Select = 1;

  // Initialize SPI3 module
  SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);

  // Hardware reset
  MP3_RST = 0;
  Delay_ms(10);
  MP3_RST = 1;

  // wait until DREQ becomes 1, see MP3 codec datasheet, Serial Protocol for SCI
  while (DREQ == 0);

  MP3_SCI_Write(SCI_MODE_ADDR, 0x0800);
  MP3_SCI_Write(SCI_BASS_ADDR, 0x7A00);
  MP3_SCI_Write(SCI_CLOCKF_ADDR, 0x2000);   // default 12 288 000 Hz

  Delay_ms(1000);
}

// Function writes one byte to MP3 SCI
void MP3_SCI_Write(char address, unsigned int data_in) {
  XDCS = 1;

  XCS = 0;                   // select MP3 SCI
  SPI1_Write(WRITE_CODE);
  SPI1_Write(address);
  SPI1_Write(Hi(data_in));      // high byte
  SPI1_Write(Lo(data_in));      // low byte
  XCS = 1;                   // deselect MP3 SCI

  // wait until DREQ becomes 1, see MP3 codec datasheet, Serial Protocol for SCI
  while (DREQ == 0);
}


// Function reads words_count words from MP3 SCI
void MP3_SCI_Read(char start_address, char words_count, unsigned int *data_buffer) {
  unsigned int temp;

  XCS = 0;                   // select MP3 SCI
  SPI1_Write(READ_CODE);        // Enter into reading mode
  SPI1_Write(start_address);    // Start reading from the starting address

  while (words_count) {         // read words_count words byte per byte
    words_count--;
    temp = SPI1_Read(0);
    temp = temp << 8;
    temp = temp + SPI1_Read(0);
    *data_buffer = temp;
    data_buffer++;
  }
  XCS = 1;                   // deselect MP3 SCI

  // wait until DREQ becomes 1, see MP3 codec datasheet, Serial Protocol for SCI
  while (DREQ == 0)
    ;
}

 // Function write one byte to MP3 SDI
void MP3_SDI_Write(char data_) {
  XCS = 1;                   // deselect MP3 SCI
  XDCS = 0;

  // wait until DREQ becomes 1, see MP3 codec datasheet, Serial Protocol for SCI
  while (DREQ == 0);

  SPI1_Write(data_);            // write data to MP3 codec
  XDCS = 1;
}

// Function Write 32 bytes to MP3 SDI
void MP3_SDI_Write_32(char *data_) {
  char i;

  XCS = 1;
  XDCS = 0;

  // wait until DREQ becomes 1, see MP3 codec datasheet, Serial Protocol for SCI
  while (DREQ == 0)
    ;

  for (i=0; i<32; i++)
    SPI1_Write(data_[i]);
  XDCS = 1;
}

void patch()
{

 MP3_SCI_Write(0x7,0x8010);
 MP3_SCI_Write(0x6,0x3e12);
 MP3_SCI_Write(0x6,0xb817);
 MP3_SCI_Write(0x6,0x3e14);
 MP3_SCI_Write(0x6,0xf812);
 MP3_SCI_Write(0x6,0x3e01);
 MP3_SCI_Write(0x6,0xb811);
 MP3_SCI_Write(0x6,0x0007);
 MP3_SCI_Write(0x6,0x9717);
 MP3_SCI_Write(0x6,0x0020);
 MP3_SCI_Write(0x6,0xffd2);
 MP3_SCI_Write(0x6,0x0030);
 MP3_SCI_Write(0x6,0x11d1);
 MP3_SCI_Write(0x6,0x3111);
 MP3_SCI_Write(0x6,0x8024);
 MP3_SCI_Write(0x6,0x3704);
 MP3_SCI_Write(0x6,0xc024);
 MP3_SCI_Write(0x6,0x3b81);
 MP3_SCI_Write(0x6,0x8024);
 MP3_SCI_Write(0x6,0x3101);
 MP3_SCI_Write(0x6,0x8024);
 MP3_SCI_Write(0x6,0x3b81);
 MP3_SCI_Write(0x6,0x8024);
 MP3_SCI_Write(0x6,0x3f04);
 MP3_SCI_Write(0x6,0xc024);
 MP3_SCI_Write(0x6,0x2808);
 MP3_SCI_Write(0x6,0x4800);
 MP3_SCI_Write(0x6,0x36f1);
 MP3_SCI_Write(0x6,0x9811);
 MP3_SCI_Write(0x7,0x8028);
 MP3_SCI_Write(0x6,0x2a00);
 MP3_SCI_Write(0x6,0x040e);

}
  // Function plays mp3 file
void MP3_Play(char *filenam) {
  unsigned long i;

  if (Mmc_Fat_Init() == 0) {
    if (Mmc_Fat_Assign(filenam, 0) ) {

      Mmc_Fat_Reset(&file_size);          // Call Reset before file reading,
                                          // procedure returns size of the file

      while (file_size > BUFFER_SIZE) {
        // Set the SPI faster when reading data from microSD memory
       SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
        for (i=0; i<BUFFER_SIZE; i++) {
          Mmc_Fat_Read(mp3_buffer + i);
        }

        // Set the SPI slower when sending data to mp3 codec
        SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
        for (i=0; i<BUFFER_SIZE/BYTES_2_WRITE; i++) {
          MP3_SDI_Write_32(mp3_buffer + i*BYTES_2_WRITE);
        }

        file_size -= BUFFER_SIZE;

      }

      // send the rest of the file to MP3 SDI
      // Set the SPI faster when reading data from microSD memory
      SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
      for (i=0; i<file_size; i++) {
        Mmc_Fat_Read(mp3_buffer + i);
      }

      // Set the SPI slower when sending data to mp3 codec
      SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
      for (i=0; i<file_size; i++) {
        MP3_SDI_Write(mp3_buffer[i]);
      }
    }
  }
}

void UART2_Write_Line(char *uart_text) {
  UART2_Write_Text(uart_text);
  UART2_Write(13);
  UART2_Write(10);
}

void main(){

  unsigned long nbsample = 0;
  unsigned char sector[512];
  unsigned int i,a,b = 0;
  unsigned int registre[2];
  unsigned int VS_Sample[2];

   // AD1PCFG = 0xFFFF;            // Configure AN pins as digital I/O
  
    ANCON0 = 0x00;                     // Configure ports as digital I/O
  ANCON1 = 0x00;
  ANCON2 = 0x00;
  //JTAGEN_bit = 0;              // Disable JTAG               // Initialize MCU
  UART2_Init(256000);
  MP3_Init();                  // Start using mp3 codec

  MP3_SCI_Write(SCI_AICTRL0_ADDR, 0x3e80);
  MP3_SCI_Write(SCI_AICTRL1_ADDR, 0x0000);
  MP3_SCI_Write(SCI_AICTRL2_ADDR, 0x1000);
  MP3_SCI_Write(SCI_AICTRL3_ADDR, 0x0000);

  MP3_SCI_Read(SCI_MODE_ADDR,1, &registre);
  MP3_SCI_Write(SCI_MODE_ADDR,  registre[0] | VS_RECORDING | VS_LINE1);

  MP3_SCI_Read(SCI_MODE_ADDR,1,&registre);
  MP3_SCI_Write(SCI_MODE_ADDR, registre[0] | VS_RESET);

  patch();/// As defined in the VS1053b datasheet

if (!Mmc_Fat_Init())

  {
    SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
    M_Create_New_File();
  }

UART2_Write(0x0D);
UART2_Write_Text("Beginning");

while (nbsample < 199964)
{

MP3_SCI_Read(SCI_HDAT1_ADDR,1,&registre);

if (registre[0]>255 && nbsample<199964)
{

for (i=0;i<256;i++)
{

MP3_SCI_Read(SCI_HDAT0_ADDR,1,&VS_Sample);

a = 2 *i;
b = (2 * i) + 1;

sector[a]=hi(VS_Sample[0]);
sector[b]=lo(VS_Sample[0]);

  }

Mmc_Fat_Write(sector,512);

nbsample = nbsample + 512;

  }

}

Mmc_fat_close();

UART2_Write_Text("End Encoding");
MP3_Init();
MP3_Play(&filename);
UART2_Write_Text("End Playing");

}

User avatar
dusan.poluga
mikroElektronika team
Posts: 780
Joined: 02 Feb 2017 14:21

Re: record voice

#2 Post by dusan.poluga » 09 Jun 2017 16:50

Hi,

Can you try to create a file on sdcard and write a couple of lines of text to test the writing?
I would start from their.
Because you are able to record from the module but you are not able to write to a file.

Best Regards,
Dusan Poluga.

zamfir doru
Posts: 6
Joined: 12 Mar 2017 19:32

Re: record voice

#3 Post by zamfir doru » 12 Jun 2017 19:19

can you help me ? please!
i create a txt file and i write on it.

User avatar
dusan.poluga
mikroElektronika team
Posts: 780
Joined: 02 Feb 2017 14:21

Re: record voice

#4 Post by dusan.poluga » 14 Jun 2017 15:40

Hi,

What micro controller, development board and what mp3 module are you using?

Regards,
Dusan Poluga.

zamfir doru
Posts: 6
Joined: 12 Mar 2017 19:32

Re: record voice

#5 Post by zamfir doru » 14 Jun 2017 18:50

easy pic pro V7 PIC18f87k22 and MP3 Click
Thank you !

Post Reply

Return to “mikroBasic General”