SuperpoweredAndroidAudioIO和UDP套接字音频传输

时间:2017-09-30 20:38:15

标签: android c++ sockets audio superpowered

我正在尝试制作将SuperpoweredAndroidAudioIO缓冲区从一个Android设备传输到另一个设备的应用程序。使用以下代码,我设法从音频回调发送和接收短int缓冲区。但是在接收端,声音在播放过程中会失真。

注意:为简洁起见,我没有包含一些似乎与问题无关的代码,包括与套接字初始化相关的函数。如果需要,我可以添加代码。

发送方:

Mic.cpp

static bool SendBuffer(
    int sd,
    sockaddr_in address,
    short int *buffer,
    size_t bufferSize) {

// Send data buffer to the socket
ssize_t sentSize = sendto(sd, 
    buffer, 
    bufferSize,
    0,  
    (struct sockaddr*)&address,
    sizeof address);

    // If send is failed
    if (sentSize == -1){
        __android_log_print(ANDROID_LOG_INFO, "Sent size ", "%i error %i", 
        sentSize , errno);
    }
    else if (sentSize > 0) {
    __android_log_print(ANDROID_LOG_INFO, "DatagramSent : ", "%hi size: %hi", 
    buffer, sentSize);
    }
    return true;
}

// audio callback
static bool micProcessing(
    void *clientdata,
    short int *audioInputOutput, 
    int numberOfSamples, 
    int __unused samplerate) {

    return SendBuffer(micSocket, dsocket, audioInputOutput, numberOfSamples);

}

// Constructor
Mic::Mic(JNIEnv *env,
    unsigned int samplerate,
    unsigned int buffersize,
    unsigned int port){

    micSocket = NewUdpSocket(env);
    dsocket = initDestinationSocket(port); // where to send
    __android_log_write(ANDROID_LOG_DEBUG, "Sockets", "initialised");

    // init IO
    microphone =  new SuperpoweredAndroidAudioIO(samplerate, 
                                                 buffersize, 
                                                 true, 
                                                 false, 
                                                 micProcessing,
                                                 this, 
                                                 -1, 
                                                 SL_ANDROID_STREAM_MEDIA, 
                                                 buffersize*2);

    __android_log_write(ANDROID_LOG_DEBUG, "Mic", "initialised");
}

接收方包括两部分:调音台频道

Mixer.cpp

//audio callback
static bool mainprocess(
    void *clientdata, 
    short int *audioInputOutput, 
    int numberOfSamples, 
    int __unused samplerate) {

    return ((Mixer*)clientdata)->processMain(audioInputOutput, numberOfSamples);
}
// Setting up Mixer
Mixer::Mixer(JNIEnv *env,unsigned int samplerate, unsigned int buffersize) {
    //Short int buffers for recieving
    channel1 = new Channel(samplerate,buffersize);
    //output buffer
    outputFloat = ((float *)memalign(16, (buffersize + 16) * sizeof(float) * 2));

    //volumes
    outputLevel = 0.5f;

    channel1level = 0.2f;
    channel2level = 0.2f;
    channel3level = 0.2f;
    channel4level = 0.2f;

    mainmixer = new SuperpoweredMonoMixer();
    __android_log_print(ANDROID_LOG_INFO, "Mixer", " Created");

    main = new SuperpoweredAndroidAudioIO(
        samplerate,
        buffersize, 
        false, 
        true, 
        mainprocess,
        this,
        -1, 
        SL_ANDROID_STREAM_MEDIA, 
        buffersize*2);

    __android_log_write(ANDROID_LOG_INFO, "Main AudioIO created", " ");
        main->stop();

        SuperpoweredCPU::setSustainedPerformanceMode(true); // Prevents CPU drops
}
// processing.
bool Mixer::processMain(short int *outputbuffer, unsigned int numberOfSamples{
    // a bit awkward
    channel1->returnFloatBuffer();
    inputs[0] = channel1->floatBuffer;

    inputs[1] = NULL;
    inputs[2] = NULL;
    inputs[3] = NULL;

    __android_log_print(ANDROID_LOG_INFO, "Channels are inside", " of mixer");
    inputLevels[0] = channel1level;
    inputLevels[1] = channel2level;
    inputLevels[2] = channel3level;
    inputLevels[3] = channel4level;

    mainmixer->process(inputs,
                       outputFloat,
                       inputLevels, 
                       outputLevel,
                       numberOfSamples);

    SuperpoweredFloatToShortInt(outputFloat, outputbuffer, numberOfSamples);
    return true;
}

Channel.cpp

//receiving buffer

static bool  ReceiveDatagramFromSocket(int sd, short int *buffer, size_t bufferSize) {

    ssize_t  recvSize = recvfrom(sd, buffer, bufferSize, 0, NULL, NULL);

    if (-1 == recvSize){ // If failed
        __android_log_print(ANDROID_LOG_INFO, "receive failed", " %i  ", errno);
    }
    else {
        // If data is received
        if (recvSize > 0) {
        __android_log_print(ANDROID_LOG_INFO, "Received"," %i bytes: %hi", recvSize, buffer);
        }
    }
    return true;
}
// init channel
Channel::Channel(unsigned int samplerate, unsigned int buffersize){
    socketIn = NewUdpSocket();
    BindSocketToPort(socketIn);

    samplerRate = samplerate;
    bufferSize = buffersize;

    shortIntBuffer = (short int *)malloc((buffersize + 16) * sizeof(short int)*4);
    floatBuffer = (float *)memalign(16, (buffersize + 16) * sizeof(float) * 2);

    bandEQ = new Superpowered3BandEQ(samplerate);
    bandEQ->enable(true);
    __android_log_print(ANDROID_LOG_INFO, "Channel ", "created");
}

// this function is called from Mixer.cpp
void Channel::returnFloatBuffer(){
    ReceiveDatagramFromSocket(socketIn, shortIntBuffer, bufferSize);
    Converting the 16-bit integer samples to 32-bit floating point.
    SuperpoweredShortIntToFloat(shortIntBuffer, floatBuffer, bufferSize, 1);
    bandEQ->process(floatBuffer, floatBuffer, bufferSize );
    __android_log_print(ANDROID_LOG_INFO, "EQ", " processing");
}

起初我认为,因为双面的AudioIO都使用不同的缓冲区大小(不同的设备240和512)进行了初始化,但后来我尝试将512硬编码到其中,但它没有帮助。

我还尝试将sendto()和recvfrom()中的缓冲区大小增加到4096,这使得声音更容易被识别,但仍然太失真。

我还应该补充一点,我是C ++的新手,我坚持'天真''无论什么工作'方法让我走得这么远。

我想了解我是否走上了正确的轨道,我应采取什么方法来传输音频而不失真。

1 个答案:

答案 0 :(得分:0)

您的方法存在两个主要问题:

  • 应避免音频处理回调等阻塞功能,例如网络连接。您需要从不同的线程执行网络(双方),并且您需要在音频处理回调和网络线程之间进行一些缓冲以传递音频。

  • 你需要"打包"在传输中,您需要处理双方的网络数据包。网络传输速度不快也不可靠,因此您需要聪明的机制来处理这个问题。

一般来说,这种音频传输的实现对于您当前的代码来说要复杂得多。