/***************************************************************************
Copyright (C) 2004-2006 by Jean-Marc Valin
Copyright (C) 2006 Commonwealth Scientific and Industrial Research
Organisation (CSIRO) Australia
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- Neither the name of the Xiph.org Foundation nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
****************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h> /* close() */
#include <string.h> /* memset() */
#include "alsa_device.h"
#include <speex/speex.h>
#include <speex/speex_jitter.h>
#include <speex/speex_preprocess.h>
#include <speex/speex_echo.h>
#include "speex_jitter_buffer.h"
#include <sched.h>
#define MAX_MSG 1500
#define SAMPLING_RATE 16000
#define FRAME_SIZE 320
int main(int argc, char *argv[])
{
int sd, rc, n;
int i;
struct sockaddr_in cliAddr, remoteAddr;
char msg[MAX_MSG];
struct hostent *h;
int local_port, remote_port;
int nfds;
struct pollfd *pfds;
SpeexPreprocessState *preprocess;
AlsaDevice *audio_dev;
int tmp;
if (argc != 5)
{
fprintf(stderr, "wrong options\n");
exit(1);
}
h = gethostbyname(argv[2]);
if(h==NULL) {
fprintf(stderr, "%s: unknown host '%s' \n", argv[0], argv[1]);
exit(1);
}
local_port = atoi(argv[3]);
remote_port = atoi(argv[4]);
printf("%s: sending data to '%s' (IP : %s) \n", argv[0], h->h_name,
inet_ntoa(*(struct in_addr *)h->h_addr_list[0]));
{
remoteAddr.sin_family = h->h_addrtype;
memcpy((char *) &remoteAddr.sin_addr.s_addr,
h->h_addr_list[0], h->h_length);
remoteAddr.sin_port = htons(remote_port);
}
/* socket creation */
sd=socket(AF_INET, SOCK_DGRAM, 0);
if(sd<0) {
printf("%s: cannot open socket \n",argv[0]);
exit(1);
}
/* bind any port */
cliAddr.sin_family = AF_INET;
cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
cliAddr.sin_port = htons(local_port);
rc = bind(sd, (struct sockaddr *) &cliAddr, sizeof(cliAddr));
if(rc<0) {
printf("%s: cannot bind port\n", argv[0]);
exit(1);
}
/* Setup audio device */
audio_dev = alsa_device_open(argv[1], SAMPLING_RATE, 1, FRAME_SIZE);
/* Setup the encoder and decoder in wideband */
void *enc_state, *dec_state;
enc_state = speex_encoder_init(&speex_wb_mode);
tmp = 8;
speex_encoder_ctl(enc_state, SPEEX_SET_QUALITY, &tmp);
tmp = 2;
speex_encoder_ctl(enc_state, SPEEX_SET_COMPLEXITY, &tmp);
dec_state = speex_decoder_init(&speex_wb_mode);
tmp = 1;
speex_decoder_ctl(dec_state, SPEEX_SET_ENH, &tmp);
SpeexBits enc_bits, dec_bits;
speex_bits_init(&enc_bits);
speex_bits_init(&dec_bits);
struct sched_param param;
/*param.sched_priority = 40; */
param.sched_priority = sched_get_priority_min(SCHED_FIFO);
if (sched_setscheduler(0,SCHED_FIFO,¶m))
perror("sched_setscheduler");
int send_timestamp = 0;
int recv_started=0;
/* Setup all file descriptors for poll()ing */
nfds = alsa_device_nfds(audio_dev);
pfds = malloc(sizeof(*pfds)*(nfds+1));
alsa_device_getfds(audio_dev, pfds, nfds);
pfds[nfds].fd = sd;
pfds[nfds].events = POLLIN;
/* Setup jitter buffer using decoder */
SpeexJitter jitter;
speex_jitter_init(&jitter, dec_state, SAMPLING_RATE);
/* Echo canceller with 200 ms tail length */
SpeexEchoState *echo_state = speex_echo_state_init(FRAME_SIZE, 10*FRAME_SIZE);
tmp = SAMPLING_RATE;
speex_echo_ctl(echo_state, SPEEX_ECHO_SET_SAMPLING_RATE, &tmp);
/* Setup preprocessor and associate with echo canceller for residual echo suppression */
preprocess = speex_preprocess_state_init(FRAME_SIZE, SAMPLING_RATE);
speex_preprocess_ctl(preprocess, SPEEX_PREPROCESS_SET_ECHO_STATE, echo_state);
alsa_device_start(audio_dev);
/* Infinite loop on capture, playback and receiving packets */
while (1)
{
/* Wait for either 1) capture 2) playback 3) socket data */
poll(pfds, nfds+1, -1);
/* Received packets */
if (pfds[nfds].revents & POLLIN)
{
/*fprintf (stderr, "x");*/
n = recv(sd, msg, MAX_MSG, 0);
int recv_timestamp = ((int*)msg)[1];
int payload = ((int*)msg)[0];
if ((payload & 0x80000000) == 0)
{
/* Put content of the packet into the jitter buffer, except for the pseudo-header */
speex_jitter_put(&jitter, msg+8, n-8, recv_timestamp);
recv_started = 1;
}
}
/* Ready to play a frame (playback) */
if (alsa_device_playback_ready(audio_dev, pfds, nfds))
{
short pcm[FRAME_SIZE];
if (recv_started)
{
/* Get audio from the jitter buffer */
speex_jitter_get(&jitter, pcm, NULL);
} else {
for (i=0;i<FRAME_SIZE;i++)
pcm[i] = 0;
}
/* Playback the audio and reset the echo canceller if we got an underrun */
if (alsa_device_write(audio_dev, pcm, FRAME_SIZE))
speex_echo_state_reset(echo_state);
/* Put frame into playback buffer */
speex_echo_playback(echo_state, pcm);
}
/* Audio available from the soundcard (capture) */
if (alsa_device_capture_ready(audio_dev, pfds, nfds))
{
short pcm[FRAME_SIZE], pcm2[FRAME_SIZE];
char outpacket[MAX_MSG];
/* Get audio from the soundcard */
alsa_device_read(audio_dev, pcm, FRAME_SIZE);
/* Perform echo cancellation */
speex_echo_capture(echo_state, pcm, pcm2);
for (i=0;i<FRAME_SIZE;i++)
pcm[i] = pcm2[i];
speex_bits_reset(&enc_bits);
/* Apply noise/echo suppression */
speex_preprocess_run(preprocess, pcm);
/* Encode */
speex_encode_int(enc_state, pcm, &enc_bits);
int packetSize = speex_bits_write(&enc_bits, outpacket+8, MAX_MSG);
/* Pseudo header: four null bytes and a 32-bit timestamp */
((int*)outpacket)[0] = htonl(0);
((int*)outpacket)[1] = send_timestamp;
send_timestamp += FRAME_SIZE;
rc = sendto(sd, outpacket, packetSize+8, 0,
(struct sockaddr *) &remoteAddr,
sizeof(remoteAddr));
if(rc<0) {
printf("cannot send audio data\n");
close(sd);
exit(1);
}
}
}
return 0;
}
speex 的一个例子, 使用了SPEEX抖动缓存.
时间: 2025-01-29 21:14:08
speex 的一个例子, 使用了SPEEX抖动缓存.的相关文章
怎样用一个例子讲解StarUML中的用例图、类图、时序图 ?
问题描述 怎样用一个例子讲解StarUML中的用例图.类图.时序图 ? 老师让我讲解StarUTML中的用例图.类图.时序图 , 我不想让老师失望 , 求解啊 请大家能给我一个简单例子 谢谢了
给你一个例子:FileFilter接口的使用。
原问题:怎么检查服务器的c:\是否有test.jpg这个文件(不知道扩展名)===================首先,定义一个FileFilter的实例 private static FileFilter fileFilter=new FileFilter(){ public boolean accept(File pathname) { String tmp=pathname.getName().toLowerCase(); i
一个例子
一个例子这一章,我们要把我们已学的知识集合起来.具体来讲,我们来写一个使用ODBC APIs的程序.为简单起见,这个程序中我使用Microsoft的Access数据库(Microsoft Access 97) . 下载例子源程序. 注意:如果你使用的windows.inc 是1.18及其以下版本,在开始编译之前要修改其中的一个小bug.在windows.inc中查找 "SQL_NULL_HANDLE",将得到下面这行: SQL_NULL_HANDLE equ 0L 将0后面的"
Spring中基于aop命名空间的AOP 一(一点准备工作和一个例子)
在某些时候,我们工程中使用的JDK 不一定就是1.5 以上,也就是说可能不支持Annotation 注解,这时自然也就不能使用@AspectJ 注解驱动的AOP 了,那么如果我们仍然想使用AspectJ 灵活的切入点表达式,那么该如何呢?Spring 为我们提供了基于xml schematic 的aop 命名空间,它的使用方式和@AspectJ 注解类似,不同的是配置信息从注解中转移到了Spring 配置文件中.在这里,我们将详细介绍如何使用Spring 提供的<aop:config/> 标签
android应用开发详解里的一个例子
问题描述 android应用开发详解里的一个例子 我导入的书上例子可以运行,照着书打进去的代码就不能运行,同样从例子里面复制过来的代码也不能运行,MainActivity可以显示,但是点击register后跳转的ResultActivity就不能显示,模拟器弹出has stopped字样,求解答 以下是代码 package com.amaker.test; import android.app.Activity; import android.content.Intent; import and
求一个例子:java socket 采用ObjectInputStream序列化收发文件例子
问题描述 求一个例子:java socket 采用ObjectInputStream序列化收发文件例子 求一个:java socket 采用ObjectInputStream序列化收发文件例子求一个:java socket 采用ObjectInputStream序列化收发文件例子 解决方案 我参考 这个 例子 解决我的问题 http://bbs.csdn.net/topics/200033850 解决方案二: http://www.cnblogs.com/feiyun126/p/3921466.
编程c语言-C语言大型程序一个例子为什么不能运行
问题描述 C语言大型程序一个例子为什么不能运行 我编写了两张图里四个文件,一个.h和三个.c,然后按照给出的那个命令运行,却出现一堆错误和warning,怎么回事?一般大点的程序按这种编写方法怎么链接和用什么命令运行? 解决方案 warning不影响运行,没有error就行 解决方案二: 有时候编译器的版本不一样也会出错的,也有课能本上给出的代码是伪代码 解决方案三: C语言实现的一个程序只能运行一次,不能重复运行 解决方案四: 这是用gcc运行是的错误 解决方案五: 要用gcc -o命令先编译
android listview 点一个item 多个item响应,能提供一个例子
问题描述 android listview 点一个item 多个item响应,能提供一个例子 android listview 点一个item 多个item响应,能提供一个例子 解决方案 http://www.itnose.net/detail/6097051.html 解决方案二: 楼主遇到的问题是 点击一个item,多个item都会响应? 把Adapter的代码发出来看看呢 解决方案三: 楼主遇到的问题是 点击一个item,多个item都会响应? 把Adapter的代码发出来看看呢 解决方案
关于那个SMTP类及一个例子
因为显示的原因,发上来的帖里里有一些空格被删除了,造成大家使用上的一些错误.分析如下:1,因为不经意的修改,大家拿到这样的很优秀的类后,总希望改成自己看起来舒心一些.不过我需要提醒一些,在这个程序里,有时增加或者删除几个空格都会造成程序出错.2,现在我将发上来的程序的一些问题说说明一下.Content-Type:后面的1-2行前面要有数个空格.请加上.HTML中每一行前面的空格都不会显示,所以,帖子里没有了.这是前面一个朋友帖出的内容,我修改后如下:This is a multi-part me