WAV文件问题 [英] WAV file question

查看:68
本文介绍了WAV文件问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好 -


我是这个小组的新手,几乎和新人一样要求编程

公开问题,所以请原谅我,如果我错过了一两个会议!


我有一个文本文件,大约40,000行,其中每行是一个由4个ASCII字符组成的
字符串对应于12位十六进制
音频样本。该文件的内容如下:


081F

081C

081A

0818

080E

等...


我想在Linux环境下编写一个简单的C程序

将读入此文件,将字符串转换为带符号的2'恭维

int samples,并输出一个简单的WAV音频文件。我在Canonical WAV格式中找到了几个

资源。通过Google,他们很好地描述了

格式。


我已经有一个程序在文件中读取并且fprintf''每行

输出stdout,作为存根。现在我需要构建WAV文件。


问题1:我是否重新发明了轮子?有没有人知道另一个

的代码片段可以做到这一点开箱即用?我看到了

ogg123似乎有一个原始的设备选项,但似乎没有多少关于它作为原始样本流的预期文档。


问题#2,对于那些熟悉WAV文件的人:WAV文档似乎

表示每个WAV文件中只能有一个DATA块。在一个

DATA块中,你得到4个字节来给出你的数据大小...但是如果你有更多的数据字节比你可以放入一个无符号的那么
4字节

int?


问题3,更多与C语言相关:WAV格式使用

little - 所有数据的-endian位顺序,但是多字节(2字节或4字节)变量的几个字节顺序是big-endian。因此对于

一个int,最重要的字节将存储在

内存的第一个字节中,下一个最重要的字节将跟随它,所以on ...

x86 / gnu-linux使用的字节顺序是什么?如何翻转某些变量的

字节顺序,这样当我写出WAV文件时,

字节按照播放器的预期顺序?


问题4:我是否必须做一些特殊的事情才能写出二进制的

文件而不是ASCII文本文件?我可以只使用fputc()来将
迭代地将WAV块结构的内容放入文件中吗?请

我需要在文件顶部做任何设置mime类型的事情吗?


提前感谢您的建议,

[军医]戴夫

Hi Everyone-

I''m new to this group, and almost-as-new to asking programming
questions publicly, so please forgive me if I miss a convention or two!

I have a text file, around 40,000 lines long, where each line is a
string of 4 ASCII characters corresponding to a 12-bit hexadecimal
audio sample. The file reads something like this...

081F
081C
081A
0818
080E
etc...

I would like to write a simple C program in a Linux environment that
will read in this file, convert the strings to signed 2''s-compliment
int samples, and output a simple WAV audio file. I''ve found several
resources on "The Canonical WAV format" via Google, and they describe
the format pretty well.

I already have a program reading in the file and fprintf''ing each line
out to stdout, as a stub. Now I need to build the WAV file.

Question #1: Am I reinventing the wheel? Does anyone know of another
piece of code out there that could do this out-of-the-box? I saw that
ogg123 seems to have a "raw" device option, but there doesn''t seem to
be much documentation as to what it expects as a raw sample stream.

Question #2, for those familiar with WAV files: The WAV docs seem to
indicate that there can be only one DATA chunk in each WAV file. In a
DATA chunk, you get 4 bytes to give the size of your data... But what
if you have more data bytes than you can drop into an unsigned 4-byte
int?

Question #3, which is more C-language related: The WAV format uses
little-endian bit orders for all data, but the byte-order of several of
the multibyte (either 2-byte or 4-byte) variables is big-endian. So for
an int, the most-significant-byte would be stored in the first byte of
memory, the next-most-significant byte would follow it, and so on...
What is the byte order used by x86/gnu-linux? How can I flip around the
byte order of certain variables so that when I write out the WAV file,
the bytes are in the order the player expects??

Question #4: Do I have to do anything special to write out a binary
file as opposed to an ASCII text file? Can I just use fputc() to
iteratively drop the contents of the WAV chunk structs into a file? Do
I need to do anything at the top of the file to set the mime type?

Thanks in advance for your advice,
[medic]Dave

推荐答案

#问题#2,对于熟悉WAV文件的人:WAV文档似乎

#表示每个WAV文件中只能有一个DATA块。在一个

#DATA块中,你得到4个字节来给出你的数据大小......但是如果你有更多的数据字节,你可以得到什么?b $ b#一个无符号的4字节

#int?


不超过3小时的声音?超过4兆字节将会在某些文件系统上遇到问题。有时会有固有的

限制你必须找到一些其他的方法。


#问题#3,这是更多与C语言相关的: WAV格式对所有数据使用
#little-endian位顺序,但几个字节顺序


您的系统可能提供各种字节交换功能可以

为您准备合适的订单。您也可以自己进行字节交换

,例如4字节整数x

(x>> 24)& 0x000000FF

| (x>> 8)& 0x0000FF00

| (x << 8)& 0x00FF0000

| (X LT;< 24)及0xFF000000


#问题#4:我是否必须做一些特殊的事情才能写出二进制文件
#文件而不是ASCII文本文件?我可以只使用fputc()来获取
#迭代地将WAV块结构的内容放入文件中吗?请

#我需要在文件顶部做任何事情来设置mime类型?


在某些系统上你想要使用fopen模式wb,b表示二进制。

(Unix不区分文本和二进制文件。)

您可以使用fputc或fwrite。据推测,mime类型由初始块描述符决定:只需用正确的

格式编写文件就足够了。也许您还必须确保文件

名称以.wav结尾。


-

SM Ryan http://www.rawbw.com/~wyrmwif/

God''sa skeeball fanatic。
# Question #2, for those familiar with WAV files: The WAV docs seem to
# indicate that there can be only one DATA chunk in each WAV file. In a
# DATA chunk, you get 4 bytes to give the size of your data... But what
# if you have more data bytes than you can drop into an unsigned 4-byte
# int?

Isn''t the maximum over 3 hours of sound? Going over 4 megabytes is going
to run into problems on some file systems. Sometimes there are inherent
limits you have to find some other way to do it.

# Question #3, which is more C-language related: The WAV format uses
# little-endian bit orders for all data, but the byte-order of several of

Your system probably provides various byte swapping functions that can
get them in the proper order for you. You can also do byte swapping
yourself, for example for a 4 byte integer x

(x>>24) & 0x000000FF
| (x>>8) & 0x0000FF00
| (x<<8) & 0x00FF0000
| (x<<24)& 0xFF000000

# Question #4: Do I have to do anything special to write out a binary
# file as opposed to an ASCII text file? Can I just use fputc() to
# iteratively drop the contents of the WAV chunk structs into a file? Do
# I need to do anything at the top of the file to set the mime type?

On some systems you want to use an fopen mode like "wb", b for binary.
(Unix does not distinguish between text and binary files.)
You can use fputc or fwrite. Presumably the mime type is decided by
the initial chunk descriptor: simply writing the file in the correct
format would then suffice. Perhaps also you have to make sure the file
name ends in .wav.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
God''s a skeeball fanatic.


文章< 11 ********************* *@f14g2000cwb.googlegroups .com>
si ******** @ gmail.com < si ******** @ gmail.com>写道:
In article <11**********************@f14g2000cwb.googlegroups .com>
si********@gmail.com <si********@gmail.com> wrote:
问题#3,更多与C语言相关:WAV格式对所有数据使用小端位顺序,但是几个字节顺序
多字节(2字节或4字节)变量是big-endian。因此对于
一个int,最重要的字节将存储在内存的第一个字节中,下一个最重要的字节将跟随它,依此类推......
x86 / gnu-linux使用的字节顺序是什么?如何翻转某些变量的字节顺序,这样当我写出WAV文件时,字节按照播放器的预期顺序?
Question #3, which is more C-language related: The WAV format uses
little-endian bit orders for all data, but the byte-order of several of
the multibyte (either 2-byte or 4-byte) variables is big-endian. So for
an int, the most-significant-byte would be stored in the first byte of
memory, the next-most-significant byte would follow it, and so on...
What is the byte order used by x86/gnu-linux? How can I flip around the
byte order of certain variables so that when I write out the WAV file,
the bytes are in the order the player expects??




通常,问题的最佳答案是如何在C代码中翻转字节

是不要。也就是说,而不是将几个

字节读入一个变量,然后修复变量,只需读取和

写*值*:


int val1,val2;

unsigned int composite; / *至少16位* /


val1 = getc(fp);

val2 = getc(fp);

if(val1 == EOF || val2 == EOF)...处理错误...


/ *从两个8位值构建16位值* /

composite =

((unsigned int)(val1& 0xff)<< 8)|

((unsigned int)(val2& ; 0xff));


在写这些值时也这样做。


结果尽可能在C中可移植。不给up

控制两个(或三个,或四个,或500个,或

但多个)getc()或putc()操作的顺序,你不需要必须

稍后更正机器使用错误的订单。

-

In-Real-Life:Chris Torek,Wind River Systems

美国犹他州盐湖城(40°39.22''N,111°50.29''W)+1 801 277 2603

电子邮件:忘了它 http://web.torek.n et / torek / index.html

由于垃圾邮件发送者,阅读电子邮件就像在垃圾中搜索食物一样。



Usually, the best answer to the question "how do I flip the bytes
around in C code" is "don''t". That is, rather than reading several
bytes into a variable, then fixing up the variable, just read and
write *values*:

int val1, val2;
unsigned int composite; /* at least 16 bits */

val1 = getc(fp);
val2 = getc(fp);
if (val1 == EOF || val2 == EOF) ... handle error ...

/* build 16-bit value from two 8-bit values */
composite =
((unsigned int)(val1 & 0xff) << 8) |
((unsigned int)(val2 & 0xff));

Do likewise when writing the values.

The result is as portable as is possible in C. By not giving up
control of the order of the two (or three, or four, or 500, or
however many) getc() or putc() operations, you do not have to
correct later for "machine used incorrect order earlier".
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22''N, 111°50.29''W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.


[引用字符固定。 ]


SM Ryan< wy ***** @ tango-sierra-oscar-foxtrot-tango.fake.org>写道:
[ Quoting character fixed. ]

SM Ryan <wy*****@tango-sierra-oscar-foxtrot-tango.fake.org> writes:
问题#2,对于熟悉WAV文件的人:WAV文档似乎表明每个WAV中只能有一个DATA块文件。在一个
数据块中,你得到4个字节来给出数据的大小...但是如果你有更多的数据字节,那么你可以将其放入一个无符号的4字节中。
Question #2, for those familiar with WAV files: The WAV docs seem to
indicate that there can be only one DATA chunk in each WAV file. In a
DATA chunk, you get 4 bytes to give the size of your data... But what
if you have more data bytes than you can drop into an unsigned 4-byte
int?



最长不超过3个小时的声音?超过4兆字节会在某些文件系统上遇到问题。有时你必须找到一些固有的限制才能找到其他方法。



Isn''t the maximum over 3 hours of sound? Going over 4 megabytes is going
to run into problems on some file systems. Sometimes there are inherent
limits you have to find some other way to do it.




我认为你的意思是4千兆字节(2 ** 32),不是4兆字节(2 ** 22)。


在某些系统上,stdio开始遇到2 GB的问题,

,因为fseek()需要签名长。如果你的文件都没有,那么你可能不用担心它。否则,找出你需要在系统上处理大文件的内容。使用

fgetpos / fsetpos而不是ftell / fseek可能是也可能不是
足够。


[snip]



I think you mean 4 gigabytes (2**32), not 4 megabytes (2**22).

On some systems, stdio starts running into problems at 2 gigabytes,
because fseek() takes a signed long. If none of your files are that
big, you can probably afford not to worry about it. Otherwise, find
out what you have to do on your system to handle large files. Using
fgetpos/fsetpos rather than ftell/fseek might or might not be
sufficient.

[snip]

问题#4:我是否必须做一些特殊的事情才能写出二进制文件而不是ASCII文本文件?我可以只使用fputc()来迭代地将WAV块结构的内容放入文件中吗?我需要在文件顶部做任何事情来设置mime类型吗?
Question #4: Do I have to do anything special to write out a binary
file as opposed to an ASCII text file? Can I just use fputc() to
iteratively drop the contents of the WAV chunk structs into a file? Do
I need to do anything at the top of the file to set the mime type?



在某些系统上你想要使用像wb这样的fopen模式, b表示二进制。
(Unix不区分文本和二进制文件。)



On some systems you want to use an fopen mode like "wb", b for binary.
(Unix does not distinguish between text and binary files.)




如果要编写二进制文件,则应使用 WB" on * all *

系统。确实,Unix没有区分文本和

二进制文件,但是因为w而已。表示文本和wb表示文本。表示二进制,你可以使用bb b来使用wb来获取文档,如果没有别的话。使用w,

您的代码将在某些系统上运行。用wb表示它将适用于所有

系统,基本上无需额外费用。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



If you want to write a binary file, you should use "wb" on *all*
systems. It''s true that Unix doesn''t distinguish between text and
binary files, but since "w" denotes text and "wb" denotes binary, you
might as well use "wb", for documentation if nothing else. With "w",
your code will work on some systems. With "wb" it will work on all
systems, at essentially no additional cost.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


这篇关于WAV文件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆