)
Python wave - Read and write WAV files {读写 WAV 格式文件}1. wave.open(file, modeNone)2. Wave_read Objects3. Wave_write ObjectsReferenceshttps://docs.python.org/3/library/wave.htmlThewavemodule provides a convenient interface to the WAV sound format. It does not support compression/decompression, but it does support mono/stereo.wave模块提供了一个处理 WAV 声音格式的便利接口。它不支持压缩/解压但是支持单声道/立体声。Thewavemodule defines the following function and exception.wave模块定义了以下函数和异常。1.wave.open(file, modeNone)If file is a string, open the file by that name, otherwise treat it as a file-like object. mode can be:如果file是一个字符串打开对应文件名的文件。否则就把它作为文件类对象来处理。mode可以为以下值rbRead only mode. - 只读模式。wbWrite only mode. - 只写模式。Note that it does not allow read/write WAV files. - 注意不支持同时读写 WAV 文件。A mode ofrbreturns aWave_readobject, while a mode ofwbreturns aWave_writeobject. If mode is omitted and a file-like object is passed as file,file.modeis used as the default value for mode.mode 设为rb时返回一个Wave_read对象而 mode 设为wb时返回一个Wave_write对象。如果省略 mode 并指定file来传入一个文件类对象则file.mode会被用作mode的默认值。If you pass in a file-like object, the wave object will not close it when itsclose()method is called; it is the caller’s responsibility to close the file object.如果操作的是文件对象当使用 wave 对象的close()方法时并不会真正关闭文件对象这需要调用者负责来关闭文件对象。Theopen()function may be used in a with statement. When the with block completes, theWave_read.close()orWave_write.close()method is called.open()函数可以在 with 语句中使用。当 with 阻塞结束时Wave_read.close()或Wave_write.close()方法会被调用。2. Wave_read ObjectsWave_readobjects, as returned byopen(), have the following methods:由open()返回的Wave_read对象有以下几种方法Wave_read.close()Close the stream if it was opened by wave, and make the instance unusable. This is called automatically on object collection.关闭 wave 打开的数据流并使对象不可用。当对象销毁时会自动调用。Wave_read.getnchannels()Returns number of audio channels (1 for mono, 2 for stereo).返回声道数量 (1 为单声道2 为立体声)Wave_read.getsampwidth()Returns sample width in bytes.返回采样字节长度。Wave_read.getframerate()Returns sampling frequency.返回采样频率。Wave_read.getnframes()Returns number of audio frames.返回音频总帧数。Wave_read.getcomptype()Returns compression type (NONEis the only supported type).返回压缩类型 (只支持NONE类型)Wave_read.getcompname()Human-readable version of getcomptype(). Usually ‘not compressed’ parallels ‘NONE’.getcomptype()的通俗版本。使用not compressed代替NONE。Wave_read.getparams()Returns anamedtuple() (nchannels, sampwidth, framerate, nframes, comptype, compname), equivalent to output of theget*()methods.返回一个namedtuple() (nchannels, sampwidth, framerate, nframes, comptype, compname)与get*()方法的输出相同。Wave_read.readframes(n)Reads and returns at most n frames of audio, as a bytes object.读取并返回以 bytes 对象表示的最多 n 帧音频。Wave_read.rewind()Rewind the file pointer to the beginning of the audio stream.重置文件指针至音频开头。The following two methods are defined for compatibility with the aifc module, and don’t do anything interesting.后面两个方法是为了和 aifc 保持兼容实际不做任何事情。Wave_read.getmarkers()Returns None.Wave_read.getmark(id)Raise an error. - 引发错误异常。The following two methods define a term “position” which is compatible between them, and is otherwise implementation dependent.以下两个方法都使用指针具体实现由其底层决定。Wave_read.setpos(pos)Set the file pointer to the specified position.设置文件指针到指定位置。Wave_read.tell()Return current file pointer position.返回当前文件指针位置。3. Wave_write ObjectsFor seekable output streams, the wave header will automatically be updated to reflect the number of frames actually written. For unseekable streams, the nframes value must be accurate when the first frame data is written. An accurate nframes value can be achieved either by callingsetnframes()orsetparams()with the number of frames that will be written beforeclose()is called and then usingwriteframesraw()to write the frame data, or by callingwriteframes()with all of the frame data to be written. In the latter casewriteframes()will calculate the number of frames in the data and set nframes accordingly before writing the frame data.对于可查找的输出流wave 头将自动更新以反映实际写入的帧数。对于不可查找的流当写入第一帧时 nframes 值必须准确。获取准确的 nframes 值可以通过调用setnframes()或setparams()并附带close()被调用之前将要写入的帧数然后使用writeframesraw()来写入帧数据或者通过调用writeframes()并附带所有要写入的帧。在后一种情况下writeframes()将计算数据中的帧数并在写入帧数据之前相应地设置 nframes。Wave_writeobjects, as returned byopen(), have the following methods:由open()返回的Wave_write对象有以下几种方法Wave_write.close()Make sure nframes is correct, and close the file if it was opened by wave. This method is called upon object collection. It will raise an exception if the output stream is not seekable and nframes does not match the number of frames actually written.确保 nframes 是正确的并在文件被 wave 打开时关闭它。此方法会在对象收集时被调用。如果输出流不可查找且 nframes 与实际写入的帧数不匹配时引发异常。Wave_write.setnchannels(n)Set the number of channels.设置声道数。Wave_write.setsampwidth(n)Set the sample width to n bytes.设置采样字节长度为 n。Wave_write.setframerate(n)Set the frame rate to n.设置采样频率为 n。Wave_write.setnframes(n)Set the number of frames to n. This will be changed later if the number of frames actually written is different (this update attempt will raise an error if the output stream is not seekable).设置总帧数为 n。如果与之后实际写入的帧数不一致此值将会被更改 (如果输出流不可查找则此更改尝试将引发错误)。Wave_write.setcomptype(type, name)Set the compression type and description. At the moment, only compression type NONE is supported, meaning no compression.设置压缩格式。目前只支持 NONE 即无压缩格式。Wave_write.setparams(tuple)The tuple should be(nchannels, sampwidth, framerate, nframes, comptype, compname), with values valid for theset*()methods. Sets all parameters.tuple 应该是(nchannels, sampwidth, framerate, nframes, comptype, compname)每项的值应可用于set*()方法。设置所有形参。Wave_write.tell()Return current position in the file, with the same disclaimer for theWave_read.tell()andWave_read.setpos()methods.返回当前文件指针其指针含义和Wave_read.tell()以及Wave_read.setpos()是一致的。Wave_write.writeframesraw(data)Write audio frames, without correcting nframes.写入音频数据但不更新 nframes。Wave_write.writeframes(data)Write audio frames and make sure nframes is correct. It will raise an error if the output stream is not seekable and the total number of frames that have been written after data has been written does not match the previously set value for nframes.写入音频帧并确保 nframes 是正确的。如果输出流不可查找且在 data 被写入之后写入的总帧数与之前设定的值不匹配将会引发错误。Note that it is invalid to set any parameters after callingwriteframes()orwriteframesraw(), and any attempt to do so will raisewave.Error.注意在调用writeframes()或writeframesraw()之后再设置任何格式参数是无效的而且任何这样的尝试将引发wave.Error。References[1] Yongqiang Cheng (程永强), https://yongqiang.blog.csdn.net/