1

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

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

发送方:

麦克风.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");
}

接收端由两部分组成:MixerChannel

混音器.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;
}

频道.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++ 的新手,我坚持使用“天真”“任何可行”的方法,这让我走到了这一步。

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

4

1 回答 1

0

您的方法有两个主要问题:

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

  • 您需要将传输“打包”,您需要处理双方的网络数据包。网络传输既不快速也不可靠,因此您需要巧妙的机制来处理这个问题。

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

于 2017-10-02T15:17:12.917 回答