Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1147882
  • 博文数量: 139
  • 博客积分: 2510
  • 博客等级: 少校
  • 技术积分: 1712
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-13 23:10
个人简介

每天进步一点点。

文章分类

全部博文(139)

文章存档

2015年(3)

2014年(11)

2013年(25)

2011年(1)

2009年(3)

2008年(29)

2007年(45)

2006年(22)

分类: Windows平台

2014-07-05 10:41:57

SNDFILE*  sf_open    (const char *path, int mode, SF_INFO *sfinfo) ;
 typedef struct
{    sf_count_t  frames ;     /* Used to be called samples. */
  int         samplerate ;
  int         channels ;
  int         format ;
  int         sections ;
  int         seekable ;
} SF_INFO ;
mode参数可以是: SFM_READ 
SFM_WRITE
SFM_RDWR
读取文件时,SF_INFO的成员变量format需要设置为零。当时如果读取的RAW DATA时,需要设置正确的samplerate,channels和format.读取之后,其余变量会被赋值的。
int  sf_format_check (const SF_INFO *info) 
在使用命令字SFM_WRITE调用sf_open函数时,该函数用于检查传递给sf_open的结构体SF_INFO设置的参数是否合法。合法时返回TRUE,否则返回FALSE。
sf_count_t  sf_seek  (SNDFILE *sndfile, sf_count_t frames, int whence) ;
该函数和unistd.h当中的lseek函数非常类型。不过该函数会忽略非音频数据的部分,仅仅在文件的数据部分移动。
另外,该函数按照样点数移动的,而且移动的数量包含了通道数。例如,对于立体声文件,从当前位置移动1时,是同时从两个声道都移动一个采样点。
 whence参数可以设置为如下的值,offset的含义和其搭配使用。 
SEEK_SET:从文件开头 + offset设置移动的帧数(按照样点*通道数) 
SEEK_CUR:从当前位置 + offset设置移动的帧数(按照样点*通道数)
SEEK_END:从文件末尾 + offset设置移动的帧数。设置为该参数时,offset可以为负值,用于实现从文件末尾往前移动。
在libsndfile内部是使用独立的读和写指针。如果一个文件按照SFM_RDWR或者 SFM_READ or SFM_WRITE打开文件,读写指针可以分别修改。如果它们自己的使用SEEK_*,读写指针会同时修改。
该函数返回移动的帧数,如果返回-1,表示出错。
 int         sf_error        (SNDFILE *sndfile) ;
返回sndfile的错误。错误信息如下:
 enum
{   SF_ERR_NO_ERROR             = 0,
    SF_ERR_UNRECOGNISED_FORMAT  = 1,
    SF_ERR_SYSTEM               = 2,
    SF_ERR_MALFORMED_FILE       = 3,
    SF_ERR_UNSUPPORTED_ENCODING = 4
} ;
它也会返回内部错误。调用的时候,应该仅仅测试上面公开的错误码,内部错误码会不断变化。如果上面列表中没有列出错误码,可以使用下面的函数转换。
 const char* sf_strerror     (SNDFILE *sndfile) ;
 const char* sf_error_number (int errnum) ;
它们会把内部错误转换为字符串信息。
int         sf_perror     (SNDFILE *sndfile) ;
int         sf_error_str  (SNDFILE *sndfile, char* str, size_t len) ;
这两个函数以后会被废弃。
int  sf_close  (SNDFILE *sndfile) ;
关闭文件,释放内部缓冲区。正确关闭会返回0.
void  sf_write_sync  (SNDFILE *sndfile) ;
使用SFM_WRITE 或者SFM_RDWR打开文件时,调用该函数会强制文件缓存写入硬盘。如果使用SFM_READ打开的文件,没有任何影响。
sf_count_t  sf_read_short   (SNDFILE *sndfile, short *ptr, sf_count_t items) ;  
sf_count_t  sf_read_int     (SNDFILE *sndfile, int *ptr, sf_count_t items) ;  
sf_count_t  sf_read_float   (SNDFILE *sndfile, float *ptr, sf_count_t items) ;  
sf_count_t  sf_read_double  (SNDFILE *sndfile, double *ptr, sf_count_t items) ;  
sf_count_t  sf_readf_short   (SNDFILE *sndfile, short *ptr, sf_count_t frames) ;  
sf_count_t  sf_readf_int     (SNDFILE *sndfile, int *ptr, sf_count_t frames) ;  
sf_count_t  sf_readf_float   (SNDFILE *sndfile, float *ptr, sf_count_t frames) ;  
sf_count_t  sf_readf_double  (SNDFILE *sndfile, double *ptr, sf_count_t frames) ;  

将sndfile文件的样点读取到ptr指向的缓冲区当中。对于单声道的音频文件。 item 和 frame 是一样的。 一个 item 对应一个数据点。
对于多声道音频文件,一个 frame 包含多个数据点,比如双声道音频文件,一个 frame 包括 2 个 item。
另外,音频文件中可能用 8 bits 来存储一个数据点,也可能是 16bits,可能用整数也可能用浮点数来存储音频数据。sf_read_XXX 系列函数会替你完成数据的转换工作。
函数会返回读取到的item或者frame的数量,正常情况下该返回值和指定的大小事相等的。但是到文件末尾,读取的数量会少于指定的数量。
sf_count_t  sf_write_short   (SNDFILE *sndfile, short *ptr, sf_count_t items) ;  
sf_count_t  sf_write_int     (SNDFILE *sndfile, int *ptr, sf_count_t items) ; 
 sf_count_t  sf_write_float   (SNDFILE *sndfile, float *ptr, sf_count_t items) ; 
 sf_count_t  sf_write_double  (SNDFILE *sndfile, double *ptr, sf_count_t items) ; 
 sf_count_t  sf_writef_short  (SNDFILE *sndfile, short *ptr, sf_count_t frames) ; 
 sf_count_t  sf_writef_int    (SNDFILE *sndfile, int *ptr, sf_count_t frames) ;  
sf_count_t  sf_writef_float  (SNDFILE *sndfile, float *ptr, sf_count_t frames) ;  
sf_count_t  sf_writef_double (SNDFILE *sndfile, double *ptr, sf_count_t frames) ;  
这些函数完成文件的写入。使用方式和read函数一样。
 sf_count_t  sf_read_raw     (SNDFILE *sndfile, void *ptr, sf_count_t bytes) ;
 sf_count_t  sf_write_raw    (SNDFILE *sndfile, void *ptr, sf_count_t bytes) ;

读写原始数据(不是没有文件头的PCM RAW DATA)。指定的数据长度必须是声道数*样点字节数的整数倍。

注意,对于SF+FORMAT_ALAW和SF_FORMAT_ULAW不支持,前面提到的8个读写函数也不支持。

除非是使用libsndfile在处理文件头,否则不要使用该函数。

const char* sf_get_string (SNDFILE *sndfile, int str_type) ;

  int         sf_set_string   (SNDFILE *sndfile, int str_type, const char* str) ;

用于读取文件时提取字符串,用于写入是写入字符串。

str_type参数的定义如下:
 enum
{   SF_STR_TITLE,
    SF_STR_COPYRIGHT,
    SF_STR_SOFTWARE,
    SF_STR_ARTIST,
    SF_STR_COMMENT,
    SF_STR_DATE,
    SF_STR_ALBUM,
    SF_STR_LICENSE,
    SF_STR_TRACKNUMBER,
    SF_STR_GENRE
 } ;
如果某个字符串存在,get函数会写入实际字符串,否则写入NULL。
sf_set_string写入成功时,会返回0,否则返回非零。返回非零时,可以使用函数sf_error_number()函数了解原因。
注意事项1:
在整数类型之间转换时,例如,使用sf_read_int()去读取一个16bitPCM编码的wav文件,libsndfile遵循一个最简单的原则。原来数据类型的最高位MSB,在新的数据类型当中依然是最高位MSB。
在整数和浮点数之间转换时,读取浮点数据之后(sf__read_float和sf_read_double),整数值会被归一化。8.16.24,32位的整数都会被归一化为【-1.0,1.0】。类似的,浮点写入到整型时,1.0会被当中该类型的最大值。例如,1.0写入到16bit时,会被当做32768.
可以通过sf_command函数来打开或者关闭归一化功能。
注意事项2:
从浮点数据类型的文件当中使用整型的方法读取时,会出现不可预料的错误。推荐使用sf_command接口,使用参数SFC_SET_SCALE_FLOAT_INT_READ参数为SF_TRUE去强制校正比例因子。
int    sf_command (SNDFILE *sndfile, int cmd, void *data, int datasize) ;
该函数可以用于提前libsndfile库函数的信息,也可以用于修改libsndfile库的行为。例如,可以读取库的版本信息,可以修改浮点转换为定点时的比例因子。
大部分的修改时按文件为单位进行的。
cmd命令定义在头文件当中,以SFC_开头。
数据通过void *指针读取或者写入数据。读写的大小不会超过datasize。有些情况下,不需要数据,这个时候data设置为NULL,datasize可以有其它用途。
cmd的命令如下:
提取库的版本信息。
提取内部每个文件的操作log
计算测量到最大信号值.
计算测量到的归一化最大信号值.
计算每个声道的峰值.
计算每个声道归一化的峰值.
提取文件的峰值.
提取每个声道的峰值 (as stored in the file header).
修改浮点读写的归一化行为.
修改双精度浮点读写的归一化行为.
提取当前浮点读写的归一化行为信息
提取当前双精度读写的归一化行为信息.
设置或者清除使用int或者short读取浮点类型数据时的比例因子.
设置或者清除使用int或者short写入浮点数据类型的比例因子。
Retrieve the number of simple formats supported by libsndfile.
Retrieve information about a simple format.
Retrieve information about a major or subtype format.
Retrieve the number of major formats.
Retrieve information about a major format type.
Retrieve the number of subformats.
Retrieve information about a subformat.
Switch the code for adding the PEAK chunk to WAV and AIFF files on or off.
Used when a file is open for write, this command will update the file header to reflect the data written so far.
Used when a file is open for write, this command will cause the file header to be updated after each write to the file.
Truncate a file open for write or for read/write.
Change the data start offset for files opened up as SF_FORMAT_RAW.
Turn on/off automatic clipping when doing floating point to integer conversion.
Retrieve current clipping setting.
Retrieve information about audio files embedded inside other files.
Test a WAVEX file for Ambisonic format
Modify a WAVEX header for Ambisonic format
Set the the Variable Bit Rate encoding quality
Determine if raw data needs endswapping
Retrieve the Broadcast Chunk info
Set the Broadcast Chunk info
Get loop info
Get instrument info
Set instrument info
Set variable bit rate encoding quality

阅读(5952) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~