抓取网页音频(android_read出现代码的逻辑很简单,主要是底层音频有问题 )
优采云 发布时间: 2021-11-28 16:04抓取网页音频(android_read出现代码的逻辑很简单,主要是底层音频有问题
)
我们经常会遇到这样的问题。应用程序读取的音频有问题。需要在tinyalsa中读取音频,确认是底层音频问题还是应用程序处理后的问题。
于是,这个补丁出现了
代码逻辑很简单,主要是pcm_read的时候,把数据同时写入文件,这个只能在调试的时候用,如果一直用,文件总是会变大,直到系统是不是存储到底有问题。
Android.mk 不再用于编写 android 代码。相反,使用 android.bp。如果需要收录头文件,这里需要修改。相关的文章是网上的一篮子,就不多说了。
有个指针处理,我觉得比较巧妙,喜欢研究代码的可以去github看源码。
如果你还是不明白代码,你可以去tinycap.c。解析和记录操作虽然简单,但对我们帮助很大。
代码修改的diff如下
diff --git a/external/tinyalsa/Android.bp b/external/tinyalsa/Android.bp
old mode 100644
new mode 100755
index 090d91c0f8..79a6ceaee2
--- a/external/tinyalsa/Android.bp
+++ b/external/tinyalsa/Android.bp
@@ -9,6 +9,7 @@ cc_library {
"mixer.c",
"pcm.c",
],
+ shared_libs: ["liblog"],
cflags: ["-Werror", "-Wno-macro-redefined"],
export_include_dirs: ["include"],
local_include_dirs: ["include"],
diff --git a/external/tinyalsa/pcm.c b/external/tinyalsa/pcm.c
old mode 100644
new mode 100755
index 4ae321bf93..de0deab9d0
--- a/external/tinyalsa/pcm.c
+++ b/external/tinyalsa/pcm.c
@@ -35,11 +35,17 @@
#include
#include
+#include
+#include
+#include
+
#include
#include
#include
#include
+#include
+
#include
#define __force
#define __bitwise
@@ -48,6 +54,10 @@
#include
+#define LOG_TAG "TINYALSA_QIFA"
+#define ALOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, fmt, ##args)
+#define ALOGE(fmt, args...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, fmt, ##args)
+
#define PARAM_MAX SNDRV_PCM_HW_PARAM_LAST_INTERVAL
/* Logs information into a string; follows snprintf() in that
@@ -242,6 +252,10 @@ static void param_init(struct snd_pcm_hw_params *p)
struct pcm {
int fd;
+ //int fd_test;
+ FILE *fd_test;
+ unsigned int size_test;
+ void *test_buffer;
unsigned int flags;
int running:1;
int prepared:1;
@@ -566,6 +580,7 @@ int pcm_read(struct pcm *pcm, void *data, unsigned int count)
x.frames = count / (pcm->config.channels *
pcm_format_to_bits(pcm->config.format) / 8);
+ ALOGE("x.frames:%ld count:%d",x.frames,count);
for (;;) {
if (!pcm->running) {
if (pcm_start(pcm) fd_test) != count) {
+ fprintf(stderr,"Error capturing sample\n");
+ ALOGE("Error capturing sample");
+ }
+
return 0;
}
}
@@ -864,6 +885,8 @@ int pcm_close(struct pcm *pcm)
if (pcm->fd >= 0)
close(pcm->fd);
+ if (pcm->fd_test >= 0)
+ fclose(pcm->fd_test);
pcm->prepared = 0;
pcm->running = 0;
pcm->buffer_size = 0;
@@ -872,6 +895,8 @@ int pcm_close(struct pcm *pcm)
return 0;
}
+
+
struct pcm *pcm_open(unsigned int card, unsigned int device,
unsigned int flags, struct pcm_config *config)
{
@@ -900,7 +925,12 @@ struct pcm *pcm_open(unsigned int card, unsigned int device,
oops(pcm, errno, "cannot open device '%s'", fn);
return pcm;
}
-
+ /*weiqifa*/
+ pcm->fd_test = fopen("/sdcard/ref_temp.pcm", "wb");
+ if (!pcm->fd_test) {
+ fprintf(stderr, "Unable to create file /sdcard/ref_temp.pcm\n");
+ } else ALOGD("creat /sdcard/ref_temp.pcm file success");
+
if (fcntl(pcm->fd, F_SETFL, fcntl(pcm->fd, F_GETFL) &
~O_NONBLOCK) config.channels *
pcm_format_to_bits(pcm->config.format) / 8);
+ ALOGE("x.frames:%ld count:%d",x.frames,count);
for (;;) {
if (!pcm->running) {
if (pcm_start(pcm) fd_test) != count) {
+ fprintf(stderr,"Error capturing sample\n");
+ ALOGE("Error capturing sample");
+ }
+
return 0;
:
int running:1;
int prepared:1;
@@ -566,6 +580,7 @@ int pcm_read(struct pcm *pcm, void *data, unsigned int count)
x.frames = count / (pcm->config.channels *
pcm_format_to_bits(pcm->config.format) / 8);
+ ALOGE("x.frames:%ld count:%d",x.frames,count);
for (;;) {
if (!pcm->running) {
if (pcm_start(pcm) fd_test) != count) {
+ fprintf(stderr,"Error capturing sample\n");
+ ALOGE("Error capturing sample");
+ }
+
return 0;
}
}
@@ -864,6 +885,8 @@ int pcm_close(struct pcm *pcm)
if (pcm->fd >= 0)
close(pcm->fd);
+ if (pcm->fd_test >= 0)
+ fclose(pcm->fd_test);
pcm->prepared = 0;
pcm->running = 0;
pcm->buffer_size = 0;
@@ -872,6 +895,8 @@ int pcm_close(struct pcm *pcm)
return 0;
}
+
+
struct pcm *pcm_open(unsigned int card, unsigned int device,
unsigned int flags, struct pcm_config *config)
{
@@ -900,7 +925,12 @@ struct pcm *pcm_open(unsigned int card, unsigned int device,
oops(pcm, errno, "cannot open device '%s'", fn);
return pcm;
}
-
+ /*weiqifa*/
+ pcm->fd_test = fopen("/sdcard/ref_temp.pcm", "wb");
+ if (!pcm->fd_test) {
+ fprintf(stderr, "Unable to create file /sdcard/ref_temp.pcm\n");
+ } else ALOGD("creat /sdcard/ref_temp.pcm file success");
+
if (fcntl(pcm->fd, F_SETFL, fcntl(pcm->fd, F_GETFL) &
~O_NONBLOCK) flags & PCM_IN))
return -EINVAL;
x.buf = data;
x.frames = count / (pcm->config.channels *
pcm_format_to_bits(pcm->config.format) / 8);
ALOGE("x.frames:%ld count:%d",x.frames,count);
for (;;) {
if (!pcm->running) {
if (pcm_start(pcm) fd, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) {
pcm->prepared = 0;
pcm->running = 0;
if (errno == EPIPE) {
/* we failed to make our window -- try to restart */
pcm->underruns++;
continue;
}
return oops(pcm, errno, "cannot read stream data");
}
if (fwrite(x.buf, 1, count, pcm->fd_test) != count) {
fprintf(stderr,"Error capturing sample\n");
ALOGE("Error capturing sample");
}
return 0;
}
}
x.buf 是一个指针,但是这个指针使用的是应用传下来的地址,所以不需要为 x.buf 分配空间。详情请参考tinycap。
在 tinycap 中,还会将标题写入音频文件。我们没有在这里写。没关系。打开音频时,可以直接导入原创音频。
xxx:/ # ls -al /sdcard/ref_temp.pcm
-rw-rw---- 1 root sdcard_rw 79298560 2021-01-08 15:24 /sdcard/ref_temp.pcm
xxx:/ #
完整代码如下: