在 CSR bluecore VM 中的音频源和接收器之间创建间接(托管)连接

物联网 微控制器 音频DSP csr-adk
2021-06-05 04:11:00

在浏览了这个站点和元数据(例如,这里这里)之后,我假设这个问题是这里的主题(但如果不是,请告诉我)

我使用的是带有 ADK4.0.1 的 CSR8675 蓝牙芯片(音频开发套件;用于对 CSR 音频芯片进行编程的软件)
(该芯片将用于连接其他使用蓝牙的设备,但这个特定问题与蓝牙无关)

CSR 的示例程序展示了如何在音频源和接收器之间进行直接连接,但我想做一个间接连接(以便更好地了解从源到接收器复制音频数据时所涉及的缓冲区的大小和组织)

这是我对他们的示例代码所做的修改:

/*
Copyright (c) 2006 - 2015 Qualcomm Technologies International, Ltd.

  An example app for routing audio through the Kalimba DSP from ADC to DAC

*/

#include <kalimba.h> 
#include <kalimba_standard_messages.h>
#include <file.h> 
#include <string.h>  
#include <panic.h>
#include <source.h>
#include <sink.h>
#include <stream.h>
#include <connection.h>
#include <micbias.h>
#include <pio.h>
#include <stdio.h>
#include <transform.h>

void PioSetPio (uint16 pPIO , bool pOnOrOff);

/* Select Amp PIO depending on board used.  If not defined, assume the CNS10001v4 board is assumed. */
#ifdef H13179V2
    #define POWER_AMP_PIO 14
#else  /* Assume CNS10001v4 */
    #define POWER_AMP_PIO 4
#endif 

/* Define the macro "BYPASS_KALIMBA" to bypass Kalimba DSP otherwise direct ADC->DAC */
/* #define BYPASS_KALIMBA */
/* Define the macro "MIC_INPUT" for microphone input otherwise line-in input */
 #define MIC_INPUT 

/* Location of DSP kap file in the file system */
static const char kal[] = "my_first_dsp_app_kalimba/my_first_dsp_app_kalimba.kap";

uint16 sampleRate = 48000; 

void start_kalimba(void);
/* void connect_streams(void); */

static TaskData aud_data_inp;
void aud_inp_handler(Task task, MessageId id, Message message);
Source audSrc_L;
Sink audSink_L;
uint16 offset_aud_inp;
uint8* dest_aud_inp;
uint16 length_aud_inp; 
uint16 srcSize;
const uint8* audSrcData_L;
uint16 length; 

#define ENABLE_MAIN_C_PRINTFx

#ifdef ENABLE_MAIN_C_PRINTF
    #define MAIN_C_MYPRINTF(x) printf x
#else
    #define MAIN_C_MYPRINTF(x) /*  */
#endif


Transform t1, t2, t3;

/* Main VM routine */
int main(void)
{
    /* Load the Kalimba */
    /* start_kalimba(); */

    aud_data_inp.handler = aud_inp_handler;

    audSrc_L = StreamAudioSource( AUDIO_HARDWARE_CODEC, AUDIO_INSTANCE_0, AUDIO_CHANNEL_A );   /* ORIGINAL */
    PanicNull(audSrc_L);
    PanicFalse( SourceConfigure(audSrc_L, STREAM_CODEC_INPUT_RATE, sampleRate) ); 
    PanicFalse( SourceConfigure(audSrc_L, STREAM_CODEC_MIC_INPUT_GAIN_ENABLE, 1) );    /* ORIGINALLY USED: 1 */
    PanicFalse(MicbiasConfigure(MIC_BIAS_0, MIC_BIAS_ENABLE, MIC_BIAS_FORCE_ON));
    PanicFalse( SourceConfigure(audSrc_L, STREAM_CODEC_INPUT_GAIN, 10) );     /* ORIGINALLY USED: 10 */

    PioSetPio(POWER_AMP_PIO, TRUE);

    audSink_L = StreamAudioSink( AUDIO_HARDWARE_CODEC, AUDIO_INSTANCE_0, AUDIO_CHANNEL_A );   /* ORIGINAL */
    PanicNull(audSink_L);
    PanicFalse( SinkConfigure(audSink_L, STREAM_CODEC_OUTPUT_RATE, sampleRate) );
    PanicFalse( SinkConfigure(audSink_L, STREAM_CODEC_OUTPUT_GAIN, 15) );     /* ORIGINALLY USED: 15 */

   #if 1
    /* BLOCK (1)   */   
    /* printf("Transconf res = 0x%x\n", TransformConfigure(t1, VM_TRANSFORM_CHUNK_CHUNK_SIZE, 1) ); */
    t1 = TransformChunk(audSrc_L, audSink_L);
    /* printf("t1 = 0x%x\n", (unsigned int)t1 ); */
    TransformConfigure(t1, VM_TRANSFORM_CHUNK_CHUNK_SIZE, 1);
    /* printf("Transconf res = 0x%x\n", TransformConfigure(t1, VM_TRANSFORM_CHUNK_CHUNK_SIZE, 1) ); */
    TransformStart( t1 );

    MessageSinkTask(audSink_L, &aud_data_inp);
    MessageSinkTask(StreamSinkFromSource(audSrc_L), &aud_data_inp);
   #endif
    /* PanicFalse( StreamConnect(audSrc_L, audSink_L) ); */


    /* Connect up the ADCs and DACS */
    /* connect_streams(); */


    /* Start the Kalimba */
   /* PanicFalse( KalimbaSendMessage(KALIMBA_MSG_GO,0,0,0,0) ); */

    /* Remain in MessageLoop (handles messages) */
    MessageLoop();

    return 0;
}

void start_kalimba(void)
{
    /* Find the codec file in the file system */
    FILE_INDEX index = FileFind( FILE_ROOT, (const char *)kal, strlen(kal) );

    /* Did we find the desired file? */
    PanicFalse( index != FILE_NONE );

    /* Load the codec into Kalimba */
    PanicFalse( KalimbaLoad( index ) );
}


void PioSetPio (uint16 pPIO , bool pOnOrOff) 
{
    uint16 lPinVals = 0 ;
    uint16 lWhichPin  = (1<< pPIO) ;

    if ( pOnOrOff )    
    {
        lPinVals = lWhichPin  ;
    }
    else
    {
        lPinVals = 0x0000;/*clr the corresponding bit*/
    }

    /*(mask,bits) setting bit to a '1' sets the corresponding port as an output*/
    PioSetDir32( lWhichPin , lWhichPin );   
    /*set the value of the pin*/         
    PioSet32 ( lWhichPin , lPinVals ) ;     
}

#if 1
/* original app handler */
void aud_inp_handler(Task task, MessageId id, Message message){
   task = task;
   MAIN_C_MYPRINTF(("\nENTERED aud_inp_handler() HANDLER\n"));
   switch(id){
      case MESSAGE_MORE_DATA:
         MAIN_C_MYPRINTF(("Received MESSAGE_MORE_DATA Message in aud_inp_handler()\n"));
         srcSize = SourceSize( audSrc_L );
         MAIN_C_MYPRINTF(("srcSize = %d ( aud_inp_handler() )\n", srcSize));
         audSrcData_L = SourceMap(audSrc_L);
         if( srcSize == 0 || srcSize < 128) break;     /* srcSize == 0 -> invalid source */
         else if( srcSize == 128){
            MAIN_C_MYPRINTF(("Inside else if( srcSize == 64){\n" ));
            length = srcSize;
            offset_aud_inp = SinkClaim(audSink_L, length);
            if(offset_aud_inp == 0xFFFF) Panic(); /* Space not available; this is a memory error, should not happen */
            /* Map the sink into memory space */
            dest_aud_inp = SinkMap(audSink_L);
            (void) PanicNull(dest_aud_inp);
            /* Copy the string into the claimed space */
            memcpy(dest_aud_inp+offset_aud_inp, audSrcData_L, length);
            /* Flush the data out to the uart */
            PanicZero(SinkFlush(audSink_L, length));        /* L_Src -> kalSink(0) */
            /* Source can be dropped after sending all the data in source to the sink */
            SourceDrop(audSrc_L, srcSize);
         }
         break;

      case MESSAGE_MORE_SPACE:
         break;

      default:
         MAIN_C_MYPRINTF(("Ignored Message in aud_inp_handler()\n"));
         break;

   }
}
#endif  

我已经尝试启用和禁用代码块,/* BLOCK (1) */结果相同

如果我通过启用线路来启用直接连接,/* PanicFalse( StreamConnect(audSrc_L, audSink_L) ); */那么从源到接收器的音频将毫无问题地路由

在这种情况下,如何设置源和接收器的间接(托管)连接?我假设当音频数据在源上可用时,固件应该生成MESSAGE_MORE_DATA并且程序控制应该转移到aud_inp_handler(),但它没有发生。我应该指出,当我使用StreamConnect()将源和接收器连接在一起,但强行终止此连接时,固件生成一条MESSAGE_STREAM_DISCONNECT消息,该消息被aud_inp_handler()函数捕获,因此函数本身已正确注册以检测固件消息,这导致我首先相信音频不是从音频硬件到音频源缓冲区的。

如何在此处设置源和接收器的间接(托管)连接?

2个回答

该代码显示了从源到接收器的受控传输,但请注意,我每 100 毫秒人为或独立地生成一条 MESSAGE_MORE_DATA 消息。

#include <panic.h>
#include <stdio.h>
#include <stream.h>
#include <pio.h>
#include <source.h>
#include <string.h>
#include <sink.h>
#include <csrtypes.h>
#include <connection.h>
#include <message.h>

#define VM_UART_(RATE)        (uint16)((RATE*0.004096)+0.5)
#define VM_UART_RATE_1K2       0x0005
#define VM_UART_RATE_1K2       0x0005
#define VM_UART_RATE_2K4       0x000a
#define VM_UART_RATE_4K8       0x0014
#define VM_UART_RATE_76K8      0x013b
#define VM_UART_RATE_1843K2    0x1d7e
#define VM_UART_RATE_2764K8    0x2c3d

#define LED1     0x01       /* bit 1 */
#define DELAY1   200        /* ms */

#define LED2     0x02       /* bit 2 */
#define DELAY2   100        /* ms */

static void led_controller1( Task t, MessageId id, Message payload )
{
        PioSet( LED1, (PioGet() ^ LED1) );
        MessageSendLater( t, 0, 0, DELAY1 );
}

static void led_controller2( Task t, MessageId id, Message payload )
{    
    Source source= StreamUartSource();
    uint16 length = SourceSize (source) ;
     if(MESSAGE_MORE_DATA==id )
     {
        char *string;
        uint16 offset;
        uint8 *dest;
        Sink sink=StreamUartSink();
        PanicNull(sink);
        string=(char *)SourceMap(source);
        /* Claim space in the sink, getting the offset to it */
        offset = SinkClaim(sink, length);
        if(offset == 0xFFFF) Panic(); /* Space not available */
        /* Map the sink into memory space */
         dest = SinkMap(sink);
        (void) PanicNull(dest);
        /* Copy the string into the claimed space */
        memcpy(dest+offset,SourceMap (source), length);
        /* Flush the data out to the uart */
        PanicZero(SinkFlush(sink, length));
        SourceDrop (source, length);
    }
    PioSet( LED2, (PioGet() ^ LED2) );
    MessageSendLater( t, MESSAGE_MORE_DATA, 0, DELAY2 );
}
static TaskData taskSPP1 ={led_controller1};
static TaskData taskSPP2 = {led_controller2};

int main(void)
{
    PioSetDir(0xFF, 0xFF);         /* Set all PIO to be output */
    PioSet(0xFF, 0);               /* Set all PIO off (0) */

    MessageSend( &taskSPP1,  0 , 0 );
    MessageSend( &taskSPP2,  MESSAGE_MORE_DATA , 0 );  

    MessageLoop();

    return 0;

在发送之前,使用函数: MessageSinkTask(Sink,task) 在方法main()中,在第一条消息之前。就我而言,它有所帮助。如果对您有帮助,请写下来。例如,在您的情况下,它看起来像这样:MessageSinkTask (audSink_L,task)。