- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我使用 C 和 Linux 制作实时数据处理应用程序(使用 fftw3 库的频谱分析仪)。我的输入数据目前取自硬件音频线路输入。我使用 PortAudio 库与硬件对话。 (目前我不使用 PortAudio 的回调)。我选择 Portaudio,因为它有很多录音示例。 RtAudio,虽然可以提供较低的延迟,但不幸的是,它是用 CPP 编写的,而不是 C(所以我有多个可移植性问题)。 (我应该尝试其他包装器吗?有没有直接的方法来捕获声音缓冲区,有例子吗?)。
除非 DFT 计算花费的时间足以用新数据填充音频缓冲区,否则我的工作设置很好。因此数据在系统的某个地方停留和积累,音频输入和图像之间的延迟发生并增加。在频谱分析中,不可能“丢弃”数据。所以我唯一能做的就是警告用户 CPU 马力低。但是这里我有问题。
Pa_GetStreamReadAvailable 函数用来显示有多少未读数据可用。但这对我来说根本不起作用。我准备了一个简单的例子,主要基于文件 www.kfr.co.il/files/speed_photo/complete.c
#include <sys/ioctl.h>
#include <linux/parport.h>
#include <linux/ppdev.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <portaudio.h>
/* #define SAMPLE_RATE (17932) // Test failure to open with this value. */
#define SAMPLE_RATE (44100)
#define FRAMES_PER_BUFFER (1024)
#define NUM_SECONDS (5)
#define NUM_CHANNELS (2)
/* #define DITHER_FLAG (paDitherOff) */
#define DITHER_FLAG (0) /**/
/* Select sample format. */
#if 1
#define PA_SAMPLE_TYPE paFloat32
typedef float SAMPLE;
#define SAMPLE_SILENCE (0.0f)
#define PRINTF_S_FORMAT "%.8f"
#elif 1
#define PA_SAMPLE_TYPE paInt16
typedef short SAMPLE;
#define SAMPLE_SILENCE (0)
#define PRINTF_S_FORMAT "%d"
#elif 0
#define PA_SAMPLE_TYPE paInt8
typedef char SAMPLE;
#define SAMPLE_SILENCE (0)
#define PRINTF_S_FORMAT "%d"
#else
#define PA_SAMPLE_TYPE paUInt8
typedef unsigned char SAMPLE;
#define SAMPLE_SILENCE (128)
#define PRINTF_S_FORMAT "%d"
#endif
int running = 1;
void signalHandler(int sig)
{
running = 0;
}
/*******************************************************************/
int main(void);
int main(void)
{
printf("Initializing PortAudio...\n");
PaStreamParameters inputParameters, outputParameters;
PaStream *stream;
PaError err;
SAMPLE *recordedSamples;
int i;
int maxFrames;
int numSamples;
int numBytes;
SAMPLE max, average, val;
// Set ctrl-c handler
signal(SIGINT, signalHandler);
//totalFrames = NUM_SECONDS * SAMPLE_RATE; /* Record for a few seconds. */
maxFrames = SAMPLE_RATE*1;
numSamples = maxFrames * NUM_CHANNELS;
numBytes = numSamples * sizeof(SAMPLE);
recordedSamples = (SAMPLE *) malloc( numBytes );
if( recordedSamples == NULL )
{
printf("Could not allocate record array.\n");
exit(1);
}
for( i=0; i<numSamples; i++ ) recordedSamples[i] = 0;
err = Pa_Initialize();
if( err != paNoError ) goto error;
inputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */
if (inputParameters.device == paNoDevice) {
fprintf(stderr,"Error: No default input device.\n");
goto error;
}
inputParameters.channelCount = NUM_CHANNELS;
inputParameters.sampleFormat = PA_SAMPLE_TYPE;
inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency;
inputParameters.hostApiSpecificStreamInfo = NULL;
/* Record some audio. -------------------------------------------- */
err = Pa_OpenStream(
&stream,
&inputParameters,
NULL, /* &outputParameters, */
SAMPLE_RATE,
FRAMES_PER_BUFFER,
paClipOff, /* we won't output out of range samples so don't bother clipping them */
NULL, /* no callback, use blocking API */
NULL ); /* no callback, so no callback userData */
if( err != paNoError ) goto error;
printf("Starting!\n\n");
printf("Numbers should increasing:\n");
err = Pa_StartStream( stream );
if( err != paNoError ) goto error;
Pa_ReadStream(stream, recordedSamples, maxFrames);
i = 1;
while (i<8)
{
long toRead = Pa_GetStreamReadAvailable(stream);
printf("%ld %d\n", toRead, maxFrames);
if (toRead > maxFrames)
toRead = maxFrames;
err = Pa_ReadStream(stream, recordedSamples, toRead);
if( err != paNoError ) goto error;
// Here is place for heavy calculations,
// they can be longer than time needed for filling one buffer.
// (So data, awaiting for processing, should be (and really is)
// accumulated somewhere in system/OS buffer.)
// Emulate big delays:
usleep(i*1000000);
i++;
}
printf("Stopping PortAudio...\n");
err = Pa_CloseStream( stream );
if( err != paNoError ) goto error;
free( recordedSamples );
Pa_Terminate();
return 0;
error:
Pa_Terminate();
fprintf( stderr, "An error occured while using the portaudio stream\n" );
fprintf( stderr, "Error number: %d\n", err );
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
return -1;
}
我预计打印输出中的数字会增加,但我的结果显然是错误的:
598 44100
3071 44100
3071 44100
3071 44100
3071 44100
3071 44100
3071 44100
使用“Pa_OpenDefaultStream”代替“Pa_OpenStream”给出其他错误数字 (8191)。我哪里错了?
或者它是 PA 中的错误,但可以肯定的是,在提交错误报告之前,我更愿意先询问。谢谢。
附言将 PA 库回归到以前的版本(用于测试)是不可能的,我无法在现代 Ubuntu 中用它编译这个例子。
最佳答案
我不清楚这里有一个错误(除了你在你的盒子上做一个 FFT 花费的时间太长)。
通常音频子系统有少量缓冲区(在您的情况下看起来像 3 3072 是 3 * 1024,您将其设置为 FRAMES_PER_BUFFER,2 是另一个常见值),如果您未能跟上,它只会丢弃来自最近最少填充的缓冲区,没有不断增长的音频缓冲区。
您有责任及时将数据从这些缓冲区中复制出来,并在 ram 或磁盘上进行缓冲,这是您需要为您的应用程序做的事情。
我有点惊讶现代机器在音频速率下进行 1024 点 FFT 有任何问题。
问候,丹。
关于c - 使用 PortAudio 录音 : Pa_GetStreamReadAvailable not work?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37470911/
我在安装 PyAudio 和 portaudio 时遇到问题。 当我执行 python -m pip install pyaudio 时出现此错误: src/_portaudiomodule.
我有一个主要使用 PyAudio 的实验室项目,为了进一步了解其工作方式,我做了一些测量,在本例中是回调之间的时间(使用回调模式)。 我计时了,得到了一个有趣的结果 (@256 chunk size,
我目前正在使用 live555、mpg123 和 portaudio 构建一个 mp3 流媒体/接收器,并且刚刚开始流式传输、解码和播放 mp3 的概念。 我的问题是当我需要用 portaudio 播
我正在使用Portaudio以便将声音录制到.raw文件中,但是我想仅在有声音时才开始录制,而在无声时才停止录制。 有没有办法用Portaudio做到这一点? 如果没有,您是否知道我该怎么做? 提前致
我再次需要你的帮助。 短版: 您可以将声音片段导入 Portaudio 吗?如果没有,我在哪里可以获得“真实”的声音示例,这些示例被转换为可以存储到我的声音缓冲区中的离散值? 加长版: 作为一个小型项
我正在尝试学习 DSP 编程以尝试创建自己的吉他效果,并且我正在使用 Portaudio 库来实现它。我遇到的问题是,无论我做什么,每当我在回调中读取输入缓冲区时,它都是空的。我已经尝试了几个不同的
我大约一周前开始使用 PortAudio 库。我已经检查了大部分教程/测试示例,但没有看到我需要的解决方案。我正在制作简单的音序器 - 我已经在钢琴卷轴上绘制了作为 block 的声音,但现在我需要以
由于我是 PortAudio 的新手,所以我尝试了一个来自互联网的示例程序。该程序能够通过回调函数记录麦克风的输入。 我想获取录制音频的每个样本,以数值(例如 float )表示。我不知道麦克风的记录
我正在尝试在 portaudio 中创建多个流。这是打开流所需要的 PaError Pa_OpenDefaultStream( PaStream** stream,
我有类似钢琴的应用程序,当我按下钢琴键时,它会播放声音。在我的应用程序中,我编写了自己的正弦波发生器。应用程序是用 Qt 编写的。我认为问题出在 portAudio 上,但我找不到任何解决方案。 我已
我正在使用 PortAudio 开发声音应用程序。我有一个结构数组,表示传递到主回调函数的各个振荡器的数据。 当我尝试在一条线上将两个波相加并播放时,它起作用了,但是当我尝试使用 for 循环来做到这
我在使用此 cmake 列表时遇到的一个问题是我收到错误消息 error: undefined reference to 'Pa_GetVersion'对于这段代码: 记录.cpp: #include
我在使用 PortAudio 时遇到问题,我不确定是我不太了解回调的工作原理,还是我做错了什么。我的假设是回调应该连续“on tick”触发,包含当前样本,但似乎我在打开和启动流时只收到一些回调,然后
我正在使用 PortAudio,这就是我现在打开流进行阅读的方式。 Pa_OpenDefaultStream(&stream, 1, 0, paFloat32, SAMPLE_RATE, SAMPLE
我是 PortAudio 的新手。我的目的是不断从我的电脑上的线路输入中捕获数据并实时处理数据。 我使用的是 44100 的采样率和 11025 的缓冲区大小 (frameCount)。我能够成功地做
我目前正在使用带有录音应用程序的 portaudio,我似乎在收集样本时遇到了一些问题。据我所知,只有一个样本被存储,回调只被调用一次,仅此而已,即使变量 NUM_OF_SECONDS 设置为 30
我想制作一个吉他效果器程序,所以我尝试从 PORTAUDIO API 运行示例代码 pa_fuzz.c。 它适用于 paWDMKS 主机 api。但是当我使用 Behringer ucg 102(无延
我正在使用 PortAudio 来实现实时音频处理。 我的首要任务是连续采集mic数据,提供100个样本进行处理(each FRAME = 100 samples at a time)到其他一些处理线
我在编写 C++ 程序时遇到了这个问题,所以我创建了一个最小的代码实例来更好地阐明问题: #include #include int main() { Pa_Initialize();
我想知道,你们知道我可以使用的用于 portaudio 的 cmake 文件吗? 提前致谢! 最佳答案 这是一个: # - Try to find Portaudio # Once done this
我是一名优秀的程序员,十分优秀!