音频数据如何缓存

顾小白xx 2018-07-09 11:52:51
声音的数据每次大小不固定,有时候不够用,有时候够用。请问该怎么过缓存,因为还有其他线程要用,如果直接缓存会不会有问题、
...全文
828 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
游荡的風 2018-07-18
  • 打赏
  • 举报
回复
记得之前工作涉及过这类,我是先建立个大一点的缓冲区,写音频数据和读取数据是两个线程,加同步锁防止同时写入读取,然后读取判断是否足够大小,满足大小了再读取处理。
大概是这个思路,上上家公司项目,没有源码。
uldm1027 2018-07-13
  • 打赏
  • 举报
回复
你可以用一个环形BUFF存 流媒体数据。。。
顾小白xx 2018-07-13
  • 打赏
  • 举报
回复
引用 13 楼 uldm1027 的回复:
你可以用一个环形BUFF存 流媒体数据。。。

我现在用的就是一个环形的buff但是还是有好多问题,现在弄的一点思路都没有了 。能不能加你个好友帮我看看
赵4老师 2018-07-10
  • 打赏
  • 举报
回复
仅供参考:
//循环向a函数每次发送200个字节长度(这个是固定的)的buffer,
//a函数中需要将循环传进来的buffer,组成240字节(也是固定的)的新buffer进行处理,
//在处理的时候每次从新buffer中取两个字节打印
#ifdef _MSC_VER
#pragma warning(disable:4996)
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _MSC_VER
#include <windows.h>
#include <process.h>
#include <io.h>
#define MYVOID void
#define vsnprintf _vsnprintf
#else
#include <unistd.h>
#include <sys/time.h>
#include <pthread.h>
#define CRITICAL_SECTION pthread_mutex_t
#define MYVOID void *
#endif
//Log{
#define MAXLOGSIZE 20000000
#define MAXLINSIZE 16000
#include <time.h>
#include <sys/timeb.h>
#include <stdarg.h>
char logfilename1[]="MyLog1.log";
char logfilename2[]="MyLog2.log";
static char logstr[MAXLINSIZE+1];
char datestr[16];
char timestr[16];
char mss[4];
CRITICAL_SECTION cs_log;
FILE *flog;
#ifdef _MSC_VER
void Lock(CRITICAL_SECTION *l) {
EnterCriticalSection(l);
}
void Unlock(CRITICAL_SECTION *l) {
LeaveCriticalSection(l);
}
void sleep_ms(int ms) {
Sleep(ms);
}
#else
void Lock(CRITICAL_SECTION *l) {
pthread_mutex_lock(l);
}
void Unlock(CRITICAL_SECTION *l) {
pthread_mutex_unlock(l);
}
void sleep_ms(int ms) {
usleep(ms*1000);
}
#endif
void LogV(const char *pszFmt,va_list argp) {
struct tm *now;
struct timeb tb;

if (NULL==pszFmt||0==pszFmt[0]) return;
vsnprintf(logstr,MAXLINSIZE,pszFmt,argp);
ftime(&tb);
now=localtime(&tb.time);
sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
sprintf(timestr,"%02d:%02d:%02d",now->tm_hour ,now->tm_min ,now->tm_sec );
sprintf(mss,"%03d",tb.millitm);
printf("%s %s.%s %s",datestr,timestr,mss,logstr);
flog=fopen(logfilename1,"a");
if (NULL!=flog) {
fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr);
if (ftell(flog)>MAXLOGSIZE) {
fclose(flog);
if (rename(logfilename1,logfilename2)) {
remove(logfilename2);
rename(logfilename1,logfilename2);
}
} else {
fclose(flog);
}
}
}
void Log(const char *pszFmt,...) {
va_list argp;

Lock(&cs_log);
va_start(argp,pszFmt);
LogV(pszFmt,argp);
va_end(argp);
Unlock(&cs_log);
}
//Log}
#define ASIZE 200
#define BSIZE 240
#define CSIZE 2
char Abuf[ASIZE];
char Cbuf[CSIZE];
CRITICAL_SECTION cs_HEX;
CRITICAL_SECTION cs_BBB;
struct FIFO_BUFFER {
int head;
int tail;
int size;
char data[BSIZE];
} BBB;
int No_Loop=0;
void HexDump(int cn,char *buf,int len) {
int i,j,k;
char binstr[80];

Lock(&cs_HEX);
for (i=0;i<len;i++) {
if (0==(i%16)) {
sprintf(binstr,"%03d %04x -",cn,i);
sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
} else if (15==(i%16)) {
sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
sprintf(binstr,"%s ",binstr);
for (j=i-15;j<=i;j++) {
sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
}
Log("%s\n",binstr);
} else {
sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
}
}
if (0!=(i%16)) {
k=16-(i%16);
for (j=0;j<k;j++) {
sprintf(binstr,"%s ",binstr);
}
sprintf(binstr,"%s ",binstr);
k=16-k;
for (j=i-k;j<i;j++) {
sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
}
Log("%s\n",binstr);
}
Unlock(&cs_HEX);
}
int GetFromRBuf(int cn,CRITICAL_SECTION *cs,struct FIFO_BUFFER *fbuf,char *buf,int len) {
int lent,len1,len2;

lent=0;
Lock(cs);
if (fbuf->size>=len) {
lent=len;
if (fbuf->head+lent>BSIZE) {
len1=BSIZE-fbuf->head;
memcpy(buf ,fbuf->data+fbuf->head,len1);
len2=lent-len1;
memcpy(buf+len1,fbuf->data ,len2);
fbuf->head=len2;
} else {
memcpy(buf ,fbuf->data+fbuf->head,lent);
fbuf->head+=lent;
}
fbuf->size-=lent;
}
Unlock(cs);
return lent;
}
MYVOID thdB(void *pcn) {
char *recv_buf;
int recv_nbytes;
int cn;
int wc;
int pb;

cn=(int)pcn;
Log("%03d thdB thread begin...\n",cn);
while (1) {
sleep_ms(10);
recv_buf=(char *)Cbuf;
recv_nbytes=CSIZE;
wc=0;
while (1) {
pb=GetFromRBuf(cn,&cs_BBB,&BBB,recv_buf,recv_nbytes);
if (pb) {
Log("%03d recv %d bytes\n",cn,pb);
HexDump(cn,recv_buf,pb);
sleep_ms(1);
} else {
sleep_ms(1000);
}
if (No_Loop) break;//
wc++;
if (wc>3600) Log("%03d %d==wc>3600!\n",cn,wc);
}
if (No_Loop) break;//
}
#ifndef _MSC_VER
pthread_exit(NULL);
#endif
}
int PutToRBuf(int cn,CRITICAL_SECTION *cs,struct FIFO_BUFFER *fbuf,char *buf,int len) {
int lent,len1,len2;

Lock(cs);
lent=len;
if (fbuf->size+lent>BSIZE) {
lent=BSIZE-fbuf->size;
}
if (fbuf->tail+lent>BSIZE) {
len1=BSIZE-fbuf->tail;
memcpy(fbuf->data+fbuf->tail,buf ,len1);
len2=lent-len1;
memcpy(fbuf->data ,buf+len1,len2);
fbuf->tail=len2;
} else {
memcpy(fbuf->data+fbuf->tail,buf ,lent);
fbuf->tail+=lent;
}
fbuf->size+=lent;
Unlock(cs);
return lent;
}
MYVOID thdA(void *pcn) {
char *send_buf;
int send_nbytes;
int cn;
int wc;
int a;
int pa;

cn=(int)pcn;
Log("%03d thdA thread begin...\n",cn);
a=0;
while (1) {
sleep_ms(100);
memset(Abuf,a,ASIZE);
a=(a+1)%256;
if (16==a) {No_Loop=1;break;}//去掉这句可以让程序一直循环直到按Ctrl+C或Ctrl+Break或当前目录下存在文件No_Loop
send_buf=(char *)Abuf;
send_nbytes=ASIZE;
Log("%03d sending %d bytes\n",cn,send_nbytes);
HexDump(cn,send_buf,send_nbytes);
wc=0;
while (1) {
pa=PutToRBuf(cn,&cs_BBB,&BBB,send_buf,send_nbytes);
Log("%03d sent %d bytes\n",cn,pa);
HexDump(cn,send_buf,pa);
send_buf+=pa;
send_nbytes-=pa;
if (send_nbytes<=0) break;//
sleep_ms(1000);
if (No_Loop) break;//
wc++;
if (wc>3600) Log("%03d %d==wc>3600!\n",cn,wc);
}
if (No_Loop) break;//
}
#ifndef _MSC_VER
pthread_exit(NULL);
#endif
}
int main() {
#ifdef _MSC_VER
InitializeCriticalSection(&cs_log);
InitializeCriticalSection(&cs_HEX);
InitializeCriticalSection(&cs_BBB);
#else
pthread_t threads[2];
int threadsN;
int rc;
pthread_mutex_init(&cs_log,NULL);
pthread_mutex_init(&cs_HEX,NULL);
pthread_mutex_init(&cs_BBB,NULL);
#endif
Log("Start===========================================================\n");

BBB.head=0;
BBB.tail=0;
BBB.size=0;

#ifdef _MSC_VER
_beginthread((void(__cdecl *)(void *))thdA,0,(void *)1);
_beginthread((void(__cdecl *)(void *))thdB,0,(void *)2);
#else
threadsN=0;
rc=pthread_create(&(threads[threadsN++]),NULL,thdA,(void *)1);if (rc) Log("%d=pthread_create %d error!\n",rc,threadsN-1);
rc=pthread_create(&(threads[threadsN++]),NULL,thdB,(void *)2);if (rc) Log("%d=pthread_create %d error!\n",rc,threadsN-1);
#endif

if (!access("No_Loop",0)) {
remove("No_Loop");
if (!access("No_Loop",0)) {
No_Loop=1;
}
}
while (1) {
sleep_ms(1000);
if (No_Loop) break;//
if (!access("No_Loop",0)) {
No_Loop=1;
}
}
sleep_ms(3000);
Log("End=============================================================\n");
#ifdef _MSC_VER
DeleteCriticalSection(&cs_BBB);
DeleteCriticalSection(&cs_HEX);
DeleteCriticalSection(&cs_log);
#else
pthread_mutex_destroy(&cs_BBB);
pthread_mutex_destroy(&cs_HEX);
pthread_mutex_destroy(&cs_log);
#endif
return 0;
}
满衣兄 2018-07-09
  • 打赏
  • 举报
回复
你这个问题跟解码不解码无关。多线程访问需要加一下锁。buffer也不可以开无限大。
donjin9 2018-07-09
  • 打赏
  • 举报
回复
那就等凑足了4096送去编码,有什么问题吗?
顾小白xx 2018-07-09
  • 打赏
  • 举报
回复
引用 7 楼 yfqvip 的回复:
你这个是个不定长的buffer,实现肯定不难,但最后自己试着实现一下,我给你个思路吧:
用std::vector<char>做存储,记录当前保存的数据的大小,写入数据时根据当前已写的数据大小和将要写入的数据大小决定分配内存的大小。

因为std::vector<char>的内存是连续的,所以可以把它当做一整块内存使用

例如:
std::vector<char> buffer_;

void :Put(const char* buf, const uint32_t size)
{
fixbuf(...) ;// 判断当前已分配的内存是否够用,不够则重新分配
memcpy(&buffer_[curr_ptr_], buf, size);
curr_ptr_ += size;
}

读取数据的时候利用memmove函数将后面的数据前移

我这里有写好的代码,但在正式项目中用的不能给你,只能给你些思路。

另外,如果真的不愿意自己实现的话就找个内存池的源码用着吧

我之前试过用了一个环形缓冲区,解完码后如果不够就接入写入缓冲区,然后等够了之后在读取来,(是一个线程中),这样做有2个问题,一是缓冲区总是溢出(开始分配了32768的大小),第二个问题是因为会有其他的线程来读,导致这个线程还没有写够那边就开始取了,但是因为数据不够,所以那边的线程肯定会崩溃。所以我才想可不可让它在没解码之前就把数据凑够了,然后就不会出现上面的情况。
顾小白xx 2018-07-09
  • 打赏
  • 举报
回复
引用 5 楼 zhao4zhong1 的回复:
[quote=引用 4 楼 xiaoyu21520 的回复:]
音频数据第一次的大小是1920,以后的数据,也会出现1920的大小。但是这个大小不够用,必须要4096以上的大小才行。

malloc+realloc
你没看到我之前的回帖?[/quote]抱歉,老师忘记说明了这个是音频数据,用virtual-audio-capturer来录制声卡的数据之前采集重采样基本都 做完了,下一步就是吧数据写入到一个缓冲区,但是这个缓冲区做了限制,进行内存对其,大小必须要大于4096因为要做aac编码所以1920个肯定不够,iResult = av_read_frame(m_pFormatCtx, pPacket);这里是获取未编码的数据然后给解码器用的下一步就直接给缓冲区去做数据从采样了,后面的代码应该没有问题了。问题就是这里的数据不够,所以要在这里吧不够的缓冲一下或者如果第一次的不够,就把第二次的数据一起加上然后去解码,1920这个大小只有第一次是固定的这个大小,后面的数据虽然也会有1920的但是大多数还是够用的。
满衣兄 2018-07-09
  • 打赏
  • 举报
回复
你这个是个不定长的buffer,实现肯定不难,但最后自己试着实现一下,我给你个思路吧:
用std::vector<char>做存储,记录当前保存的数据的大小,写入数据时根据当前已写的数据大小和将要写入的数据大小决定分配内存的大小。

因为std::vector<char>的内存是连续的,所以可以把它当做一整块内存使用

例如:
std::vector<char> buffer_;

void :Put(const char* buf, const uint32_t size)
{
fixbuf(...) ;// 判断当前已分配的内存是否够用,不够则重新分配
memcpy(&buffer_[curr_ptr_], buf, size);
curr_ptr_ += size;
}

读取数据的时候利用memmove函数将后面的数据前移

我这里有写好的代码,但在正式项目中用的不能给你,只能给你些思路。

另外,如果真的不愿意自己实现的话就找个内存池的源码用着吧
赵4老师 2018-07-09
  • 打赏
  • 举报
回复
realloc
Reallocate memory blocks.

void *realloc( void *memblock, size_t size );

Routine Required Header Compatibility
realloc <stdlib.h> and <malloc.h> ANSI, Win 95, Win NT


For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version


Return Value

realloc returns a void pointer to the reallocated (and possibly moved) memory block. The return value is NULL if the size is zero and the buffer argument is not NULL, or if there is not enough available memory to expand the block to the given size. In the first case, the original block is freed. In the second, the original block is unchanged. The return value points to a storage space that is guaranteed to be suitably aligned for storage of any type of object. To get a pointer to a type other than void, use a type cast on the return value.

Parameters

memblock

Pointer to previously allocated memory block

size

New size in bytes

Remarks

The realloc function changes the size of an allocated memory block. The memblock argument points to the beginning of the memory block. If memblock is NULL, realloc behaves the same way as malloc and allocates a new block of size bytes. If memblock is not NULL, it should be a pointer returned by a previous call to calloc, malloc, or realloc.

The size argument gives the new size of the block, in bytes. The contents of the block are unchanged up to the shorter of the new and old sizes, although the new block can be in a different location. Because the new block can be in a new memory location, the pointer returned by realloc is not guaranteed to be the pointer passed through the memblock argument.

realloc calls malloc in order to use the C++ _set_new_mode function to set the new handler mode. The new handler mode indicates whether, on failure, malloc is to call the new handler routine as set by _set_new_handler. By default, malloc does not call the new handler routine on failure to allocate memory. You can override this default behavior so that, when realloc fails to allocate memory, malloc calls the new handler routine in the same way that the new operator does when it fails for the same reason. To override the default, call

_set_new_mode(1)

early in your program, or link with NEWMODE.OBJ.

When the application is linked with a debug version of the C run-time libraries, realloc resolves to _realloc_dbg. For more information about how the heap is managed during the debugging process, see Using C Run-Time Library Debugging Support.

Example

/* REALLOC.C: This program allocates a block of memory for
* buffer and then uses _msize to display the size of that
* block. Next, it uses realloc to expand the amount of
* memory used by buffer and then calls _msize again to
* display the new amount of memory allocated to buffer.
*/

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

void main( void )
{
long *buffer;
size_t size;

if( (buffer = (long *)malloc( 1000 * sizeof( long ) )) == NULL )
exit( 1 );

size = _msize( buffer );
printf( "Size of block after malloc of 1000 longs: %u\n", size );

/* Reallocate and show new size: */
if( (buffer = realloc( buffer, size + (1000 * sizeof( long )) ))
== NULL )
exit( 1 );
size = _msize( buffer );
printf( "Size of block after realloc of 1000 more longs: %u\n",
size );

free( buffer );
exit( 0 );
}


Output

Size of block after malloc of 1000 longs: 4000
Size of block after realloc of 1000 more longs: 8000


Memory Allocation Routines

See Also calloc, free, malloc
赵4老师 2018-07-09
  • 打赏
  • 举报
回复
引用 4 楼 xiaoyu21520 的回复:
音频数据第一次的大小是1920,以后的数据,也会出现1920的大小。但是这个大小不够用,必须要4096以上的大小才行。

malloc+realloc
你没看到我之前的回帖?
顾小白xx 2018-07-09
  • 打赏
  • 举报
回复
音频数据第一次的大小是1920,以后的数据,也会出现1920的大小。但是这个大小不够用,必须要4096以上的大小才行。
满衣兄 2018-07-09
  • 打赏
  • 举报
回复
可以缓存一下,没问题的
赵4老师 2018-07-09
  • 打赏
  • 举报
回复
仅供参考:
//文件1中的内容排序并去重,结果保存到文件2中
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXCHARS 128 //能处理的最大行宽,包括行尾的\n和字符串尾的\0
int MAXLINES=10000,MAXLINES2;
char *buf,*buf2;
int c,n,hh,i,L;
FILE *f;
char ln[MAXCHARS];
int ignore_case=0;
int icompare(const void *arg1,const void *arg2) {
return stricmp((char *)arg1,(char *)arg2);
}
int compare(const void *arg1,const void *arg2) {
return strcmp((char *)arg1,(char *)arg2);
}
int main(int argc,char **argv) {
if (argc<3) {
printf("Unique line. Designed by zhao4zhong1@163.com. 2012-08-20\n");
printf("Usage: %s src.txt uniqued.txt [-i]\n",argv[0]);
return 1;
}
if (argc>3) ignore_case=1;//若存在命令行参数3,忽略大小写
f=fopen(argv[1],"r");
if (NULL==f) {
printf("Can not find file %s!\n",argv[1]);
return 1;
}
buf=(char *)malloc(MAXLINES*MAXCHARS);
if (NULL==buf) {
fclose(f);
printf("Can not malloc(%d LINES*%d CHARS)!\n",MAXLINES,MAXCHARS);
return 2;
}
n=0;
hh=0;
i=0;
while (1) {
if (NULL==fgets(ln,MAXCHARS,f)) break;//
hh++;
L=strlen(ln)-1;
if ('\n'!=ln[L]) {//超长行忽略后面内容
printf("%s Line %d too long(>%d),spilth ignored.\n",argv[1],hh,MAXCHARS);
while (1) {
c=fgetc(f);
if ('\n'==c || EOF==c) break;//
}
}
while (1) {//去掉行尾的'\n'和空格
if ('\n'==ln[L] || ' '==ln[L]) {
ln[L]=0;
L--;
if (L<0) break;//
} else break;//
}
if (L>=0) {
strcpy(buf+i,ln);i+=MAXCHARS;
n++;
if (n>=MAXLINES) {
MAXLINES2=MAXLINES*2;
if (MAXLINES2==1280000) MAXLINES2=2500000;
buf2=(char *)realloc(buf,MAXLINES2*MAXCHARS);
if (NULL==buf2) {
printf("Can not malloc(%d LINES*%d CHARS)!\n",MAXLINES2,MAXCHARS);
printf("WARNING: Lines >%d ignored.\n",MAXLINES);
break;//
}
buf=buf2;
MAXLINES=MAXLINES2;
}
}
}
fclose(f);
if (n>1) {
if (ignore_case) qsort(buf,n,MAXCHARS,icompare);
else qsort(buf,n,MAXCHARS,compare);
}
f=fopen(argv[2],"w");
if (NULL==f) {
free(buf);
printf("Can not create file %s!\n",argv[2]);
return 2;
}
fprintf(f,"%s\n",buf);
if (n>1) {
if (ignore_case) {
hh=0;
L=MAXCHARS;
for (i=1;i<n;i++) {
if (stricmp((const char *)buf+hh,(const char *)buf+L)) {
fprintf(f,"%s\n",buf+L);
}
hh=L;
L+=MAXCHARS;
}
} else {
hh=0;
L=MAXCHARS;
for (i=1;i<n;i++) {
if ( strcmp((const char *)buf+hh,(const char *)buf+L)) {
fprintf(f,"%s\n",buf+L);
}
hh=L;
L+=MAXCHARS;
}
}
}
fclose(f);
free(buf);
return 0;
}
赵4老师 2018-07-09
  • 打赏
  • 举报
回复
malloc+realloc

64,676

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧