Skip to content

Instantly share code, notes, and snippets.

@strfry
Created September 6, 2017 22:53
Show Gist options
  • Save strfry/0217a51e3a8c1f19138c824e9a485673 to your computer and use it in GitHub Desktop.
Save strfry/0217a51e3a8c1f19138c824e9a485673 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/soundcard.h>
#include <webrtc_audio_processing/module_common_types.h>
#include <webrtc_audio_processing/audio_processing.h>
int main()
{
int errno;
int format = AFMT_S16_NE; /* native endian */
int speed = 32000;
int blocksize = speed / 100;
int channels = 1;
int fd = open("/dev/dsp", O_RDWR);
if (!fd) {
printf("Could not open sound device\n");
return -1;
}
//if (0 != ioctl(fd, FIONBIO, &nonblock))
// return errno;
if (0 != ioctl(fd, SNDCTL_DSP_SETFMT, &format))
return errno;
if (0 != ioctl(fd, SNDCTL_DSP_CHANNELS, &channels))
return errno;
if (2 == channels) {
int stereo = 1;
if (0 != ioctl(fd, SNDCTL_DSP_STEREO, &stereo))
return errno;
}
if (0 != ioctl(fd, SNDCTL_DSP_SPEED, &speed))
return errno;
printf("oss: init: %d Hz %d ch, blocksize=%d\n",
speed, channels, blocksize);
blocksize = speed / 100;
char buf[320];
// BEGIN WEBRTC TESTBENCH
webrtc::AudioProcessing* apm = webrtc::AudioProcessing::Create(0);
apm->set_sample_rate_hz(speed);
apm->set_num_channels(1, 1);
apm->set_num_reverse_channels(1);
apm->echo_cancellation()->Enable(true);
apm->echo_cancellation()->set_device_sample_rate_hz(32000);
apm->set_stream_delay_ms(0);
while (true) {
webrtc::AudioFrame frame;
frame._audioChannel = 1;// ss->channels
frame._frequencyInHz = 32000;
frame._payloadDataLengthInSamples = blocksize;
read(fd, frame._payloadData, blocksize);
errno = apm->AnalyzeReverseStream(&frame);
if (errno) {
printf("AnalyzeReverseStream() = %d\n", errno);
}
apm->set_stream_delay_ms(0);
errno = apm->ProcessStream(&frame);
if (errno) {
printf("ProcessStream() = %d\n", errno);
}
write(fd, frame._payloadData, blocksize);
}
webrtc::AudioProcessing::Destroy(apm);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment