gpt4 book ai didi

C:从内核模块写入文件是否正确?

转载 作者:行者123 更新时间:2023-11-30 17:59:55 24 4
gpt4 key购买 nike

我尝试将 pcm 字节(来自 ALSA 缓冲区)从内核空间复制/写入到内核模块 (LKM) 内的文件中。

文件已写入,大小对我来说看起来不错,但因为它是 PCM 数据,我无法查看它是否正确(原始音频数据,不可读)。

我的音频播放器(MPlayer =“无效寻求负位置ffffffffffffffff!”,VLC,Foobar2000)无法播放我编写的文件,所以我认为我的代码中有错误。

当我通过 SCITE 打开文件时,我看到很多“NUL”和其他哭泣的东西(字节;)。

也许你们中有人发现了错误?

我有这个脚本:

unsigned char *dma_area; // this is the source, an mmapped-area
int pcm_offset_bytes; // the actual offset in the dma_area
int size_bytes; // the amount of bytes to copy
struct file *target_file; // the target-file


int ret; // used in write-process below
int wrote = 0; // used in write-process below
mm_segment_t fs;
void *data; // dma_area + pcm_offset_bytes

// (..) calculate offset and size

// open the target file
target_file = filp_open("/dev/snd/pcmC0D0p_output", (O_CREAT | O_TRUNC | O_WRONLY), (S_IRWXU | S_IRWXG | S_IRWXO));

data = dma_area + pcm_offset_bytes


fs = get_fs();
set_fs (get_ds ());

if (!target_file || !target_file->f_op || !target_file->f_op->write) {
LOGI("something's missing\n");
return -EIO;
}

// loop until every byte is written
while (size_bytes > 0) {

ret = target_file->f_op->write(target_file, (char *)data, size_bytes, &target_file->f_pos);

LOGI ("wrote %d bytes to target_file@%p, return %d\n", size_bytes, target_file, ret);
if (ret <= 0)
goto done;

size_bytes -= ret;
data += ret;
wrote += ret;
}

ret = wrote;

done:
set_fs(fs);
LOGI("result %d\n", ret);

最佳答案

首先,您“不应该”写入内核中的文件。

对于如何播放headerless raw pcm这个更正确的问题,可以使用audacity的导入功能。

或者使用aplay

aplay some_file -t raw -f S16_LE -c 2 -r 44100

使用任何合适的参数。

关于C:从内核模块写入文件是否正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11016927/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com