• 2013-08-06

    WebRTC C++ audio recording example

    Views: 11242 | 1 Comment

    The WebRTC project included a voice engine(GIPS, Global IP Solutions), here is an example to show how you can use the voice engine to record audio from microphone.

    #include <stdio.h>
    #include <stdlib.h>
    #include <assert.h>
    #include "webrtc/modules/audio_device/include/audio_device.h"
    #include "webrtc/common_audio/resampler/include/resampler.h"
    #include "webrtc/modules/audio_processing/aec/include/echo_cancellation.h"
    
    class AudioTransportImpl: public webrtc::AudioTransport
    {
    private:
        
    public:
        AudioTransportImpl(webrtc::AudioDeviceModule* audio){
        }
        
        ~AudioTransportImpl(){
        }
    
        virtual int32_t RecordedDataIsAvailable(
            const void* audioSamples,
            const uint32_t nSamples,
            const uint8_t nBytesPerSample,
            const uint8_t nChannels,
            const uint32_t samplesPerSec,
            const uint32_t totalDelayMS,
            const int32_t clockDrift,
            const uint32_t currentMicLevel,
            const bool keyPressed,
            uint32_t& newMicLevel)
        {
            printf("record %d %d %d %d %d %d %d %d\n", nSamples,
                nBytesPerSample, nChannels,
                samplesPerSec, totalDelayMS,
                clockDrift, currentMicLevel, keyPressed);
            // now you have the audioSamples...
            return 0;
        }
    };
    
    
    int main(int argc, char **argv){
        webrtc::AudioDeviceModule *audio;
        audio = webrtc::CreateAudioDeviceModule(0, webrtc::AudioDeviceModule::kPlatformDefaultAudio);
        assert(audio);
        audio->Init();
        
        int num;
        int ret;
        
        num = audio->RecordingDevices();
        printf("Input devices: %d\n", num);
        for(int i=0; i<num; i++){
            char name[webrtc::kAdmMaxDeviceNameSize];
            char guid[webrtc::kAdmMaxGuidSize];
            int ret = audio->RecordingDeviceName(i, name, guid);
            if(ret != -1){
                printf("    %d %s %s\n", i, name, guid);
            }
        }
        
        audio->SetRecordingDevice(0);
        audio->InitRecording();
        
        AudioTransportImpl callback(audio);
        audio->StartRecording();
    
        getchar();
    }
    

    For more webrtc demos, visit: https://github.com/ideawu/webrtc-demo

    Posted by ideawu at 2013-08-06 23:08:32 Tags: ,
|<<<1>>>| 1/1 Pages, 1 Results.