首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >SuperpoweredAndroidAudioIO和UDP套接字音频传输

SuperpoweredAndroidAudioIO和UDP套接字音频传输
EN

Stack Overflow用户
提问于 2017-09-30 20:38:15
回答 1查看 138关注 0票数 1

我正试图制作一个应用程序,将SuperpoweredAndroidAudioIO缓冲区从一个安卓设备传输到另一个安卓设备。通过下面的代码,我成功地从音频回调中发送和接收了短int缓冲区。但是在接收端,声音在回放过程中会受到很大的扭曲。

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

发送端:

Mic.cpp

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
//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

代码语言:javascript
运行
复制
//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()中的缓冲区大小从()增加到4096,这样做听起来更容易识别,但仍然过于扭曲。

我还应该补充一点,我是C++的新手,我坚持“天真”和“任何可行”的方法,这使我走到了这一步。

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

EN

回答 1

Stack Overflow用户

发布于 2017-10-02 15:17:12

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

  • 应该避免阻塞函数,如网络,从音频处理回调。您需要从不同的线程执行网络(两边),并且需要在音频处理回调和网络线程之间进行一些缓冲以传递音频。
  • 您需要“打包”传输,您需要处理双方的网络数据包。网络传输既不快也不可靠,所以您需要聪明的机制来处理这个问题。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46506848

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档