⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 g711communication_main.c

📁 Audio compression on embedded board
💻 C
字号:
/*********************************************************************
*                                                                    *
*                       Software License Agreement                   *
*                                                                    *
*   The software supplied herewith by Microchip Technology           *
*   Incorporated (the "Company") for its dsPIC controller            *
*   is intended and supplied to you, the Company's customer,         *
*   for use solely and exclusively on Microchip dsPIC                *
*   products. The software is owned by the Company and/or its        *
*   supplier, and is protected under applicable copyright laws. All  *
*   rights are reserved. Any use in violation of the foregoing       *
*   restrictions may subject the user to criminal sanctions under    *
*   applicable laws, as well as to civil liability for the breach of *
*   the terms and conditions of this license.                        *
*                                                                    *
*   THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION.  NO           *
*   WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING,    *
*   BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND    *
*   FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE     *
*   COMPANY SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL,  *
*   INCIDENTAL OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.  *
*                                                                    *
*********************************************************************/

/******************************************************************************
**
**  Filename:     G711Communication_Main.c
**
**  System:       RISC
**
**  Platform:     dsPIC33F
**
**  Description:  This main file is used as a wrapper for performing Walkie-Talkie
**                like operation between two dsPIC33f demo boards. uart's 1 and 2
**                are used to transfer data and sync information between boards.
**                uart1 is used for transferring and receiving  compressed speech
**                data and uart2 is used for transferring and receiving sync
**                information.The G.711 codec is used for companding speech data.
**                SI3000 is used for sampling and playing out RAW speech data.
**                Sampling rate is set to 8000Hz.
******************************************************************************/

#include <p33Fxxxx.h>
#include "G711Lib_common.h"
#include "G711Lib_si3000.h"
#include "G711Lib_internal.h"

_FOSCSEL(FNOSC_PRIPLL);
_FOSC(FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMD_EC);
_FWDT(FWDTEN_OFF);

//-----------------------------------------------------------------------------
//Global Variables
//-----------------------------------------------------------------------------
//User defined ping pong buffers.
short buffer1[NUMOFSAMPLESPERBLOCK];       //sampleIpBuffer.
short buffer2[NUMOFSAMPLESPERBLOCK];       //sampleOpBuffer.
short expandbuffer1[NUMOFSAMPLESPERBLOCK]; //sampleExpIpBuffer
short expandbuffer2[NUMOFSAMPLESPERBLOCK]; //sampleExpOpBuffer
short buffer11[NUMOFSAMPLESPERBLOCK];      //sampleComprsIpBuffer.
short buffer12[NUMOFSAMPLESPERBLOCK];	   //sampleComprsOpBuffer.

volatile unsigned int Txcount=0, RXcount=0;
volatile unsigned int fRxCpl=0, fRxstart = 0, fRxstop = 0;
long slen = NUMOFSAMPLESPERBLOCK;
short  *temp;

/*-----------------------------------------------------------------------------
                                  Macro instantiation
------------------------------------------------------------------------------*/
G711SI3000INIT                     //Macro to instantiate SI3000 structure
CODECSETUP                         //Macro to instantiate CODECSETUP structure

/******************************************************************************
**
**  Function Name: G711CommunicationDemo_dsPIC33f.c
**
**  Description:   Wrapper.
**
******************************************************************************/
int main ( void )
{
  /* PLLFBD: Oscillator PLL Divisor Control Register set for 12.288 Mhz*/
  CLKDIV = 0x00C0;
  PLLFBD = 62;                                //Fcy = Fosc/2;

  /* set TRISD to 0 - output to LEDs */
  TRISD = 0x0;

  /*Led's 0 to 3 have been used for debugging purposes.*/
  /*Led0 will flicker when reception of data is happening properly on uart1*/
  /*Led1 will flicker when transmission is happening properly on uart1*/
  /*Led2 will flicker when playout is happening properly on SI3000*/
  
  /* clear all LEDs */
  PORTD = 0xFFFF;

  codecdata.fBlockplayed = 0x01;              //Set codecdata.fBlockplayed flag.
  libuart2Init ( );                           //Initialize UART2 Registers.                      
  libuart1Init ( );                           //Initialize UART1 Registers.
  libSi3000Init ( );                          //Initialize DCI and SI 3000 Regs.
  Initiator ( );			                  //Synchronize the Boards.

/*****************************************************************************
                         a Law companding
*****************************************************************************/
  if ( codecdata.law == 1 )
  {
    while ( 1 )
    {
      asm ( "repeat #5000" );                 //Delay.
      asm ("nop");
    
      if ( fRxstop == 1 )                     //Check if, flag to indicate data   
      {                                       //reception has stopped has set.
        Initiator ( );                        //Synch with the other board.
      }

      //-----------------------------------------------------------------------
      // Start alaw compression
      //-----------------------------------------------------------------------
      if ( codecdata.fBlockdone ) 
      {
        alaw_compress ( slen, codecdata.sampleOpBuffer,\
                              codecdata.sampleComprsIpBuffer );
        codecdata.fBlockdone    = 0x00;       //Clear the flag
        codecdata.fCompressdone = 0x01;       //Compression completed
	  }

      //-----------------------------------------------------------------------
      // Transmit the compressed data
      //-----------------------------------------------------------------------
      while ( codecdata.fCompressdone )     
      {
        while( !U1STAbits.TRMT );             //Transmit 1 byte of data after
        {                                     //Compression.
          libuart1Transmit ( );
          Txcount += 1;
	    }

        if ( Txcount == NUMOFSAMPLESPERBLOCK )//Check whether a full block of  
        {                                     //data has been transmitted.
          Txcount = 0x00;
          codecdata.fCompressdone = 0x00;
          codecdata.sampleComprsIpBuffer -= NUMOFSAMPLESPERBLOCK ;
          PORTDbits.RD1 ^= 0x1;
          break;
        }
      }

      //-----------------------------------------------------------------------
      // Start alaw Expand
      //-----------------------------------------------------------------------
      if ( fRxCpl )
      {
	    alaw_expand ( slen, codecdata.sampleComprsOpBuffer,\
                            codecdata.sampleExpandIpBuffer );

        while ( codecdata.fBlockplayed != 0x00 );

        /* Exchange the addresses of sample buffer pointers in to the pointer
           to sample buffers to manage the user defined ping pong buffers */
  	    temp                            = codecdata.sampleExpandIpBuffer;
  	    codecdata.sampleExpandIpBuffer  = codecdata.sampleExpandOpBuffer;
  	    codecdata.sampleExpandOpBuffer  = temp;
   
        /* Set the flag to indicate Ready for Playout */
  	    codecdata.fBlockplayed  = 0x01;
	    codecdata.fCompressdone = 0x00;
        fRxCpl = 0x00;
      }
    }
  }
/************************End of A Law companding******************************/


/******************************************************************************
                         u Law companding
******************************************************************************/
 else
 {
    while ( 1 )
    {
      asm ( "repeat #5000" );                 //Delay.
      asm( "nop" );
    
      if( fRxstop == 1 )                      //Check if, flag to indicate data   
      {                                       //reception has stopped has set.
        Initiator( );                         //Synch with the other board.
      }
    
      //-----------------------------------------------------------------------
      // Start ulaw compression
      //-----------------------------------------------------------------------
      if ( codecdata.fBlockdone )
      {
        ulaw_compress ( slen, codecdata.sampleOpBuffer,\
                              codecdata.sampleComprsIpBuffer );
        codecdata.fBlockdone    = 0x00;       //Clear the flag.
        codecdata.fCompressdone = 0x01;       //Compression completed.
	  }

      //-----------------------------------------------------------------------
      // Transmit the compressed data
      //-----------------------------------------------------------------------
      while ( codecdata.fCompressdone )
      {
        while( !U1STAbits.TRMT );             //Transmit 1 byte of data after
        {                                     //Compression.
          libuart1Transmit ( );
          Txcount += 1;
	    }

        if ( Txcount==NUMOFSAMPLESPERBLOCK )  //Check whether a full block of
        {                                     // data has been transmitted.
          Txcount = 0x00;
          codecdata.fCompressdone = 0x00;
          codecdata.sampleComprsIpBuffer -= NUMOFSAMPLESPERBLOCK ;
          PORTDbits.RD1 ^= 0x1;
  	      break;
        }
      }

      //---------------------------------------------------------------------
      // Start ulaw Expand
      //---------------------------------------------------------------------
      if ( fRxCpl )
      {
        ulaw_expand  ( slen, codecdata.sampleComprsOpBuffer, \
                             codecdata.sampleExpandIpBuffer );

        while ( codecdata.fBlockplayed != 0x00 );

        /* Exchange the addresses of sample buffer pointers in to the pointer
           to sample buffers to manage the user defined ping pong buffers */
  	    temp                            = codecdata.sampleExpandIpBuffer;
  	    codecdata.sampleExpandIpBuffer  = codecdata.sampleExpandOpBuffer;
  	    codecdata.sampleExpandOpBuffer  = temp;

        /* Set the flag to indicate Ready for Playout */
  	    codecdata.fBlockplayed  = 0x01;
        codecdata.fCompressdone = 0x00;
        fRxCpl = 0x00;
      }
    }
  }
/************************End of u Law companding******************************/

}
/*-----------------------------------------------------------------------------
               END OF FILE:  G711Communication_Main.c
-----------------------------------------------------------------------------*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -