开发者

How to open wav file with Lua

开发者 https://www.devze.com 2022-12-30 16:54 出处:网络
I am trying to do some wav processing using Lua, but have fallen a the first hurdle! I cannot find a function or library that will allow me to load a开发者_运维知识库 wav file and access the raw data.

I am trying to do some wav processing using Lua, but have fallen a the first hurdle! I cannot find a function or library that will allow me to load a开发者_运维知识库 wav file and access the raw data. There is one library, but it onl allows playing of wavs, not access to the raw data.

Are there any out there?

Cheers, Pete.


I don't think Lua is the right tool for raw audio data processing, mainly because Lua uses only a single numeric data type - doubles. Also, Lua cannot directly access the elements of a data stream, although you can use something like the struct library ( http://www.inf.puc-rio.br/~roberto/struct/ )

A better way to process the data would be to write the filters in C, with binding for Lua, and then use Lua for higher level processing, like (imaginary toolkit):

require 'wave'
-- load the wave
wav = wave.load('file.wav', 's16')
-- apply some filters
thresh = wave.threshold(wav, 0.5)
faded = wave.fadeout(thresh, 5)


Alternatively, you can load the data and view it like this. The ascii column will show you the the WAV header, which is stored in the first 44 bytes...

local f = assert(io.open(path, "rb"))
-- read in 16 bytes at a time
local block = 16
while true do
local bytes = f:read(block)
if not bytes then break end

for _, b in pairs{string.byte(bytes, 1, -1)} do
    io.write(string.format("%02X ", b))
end

io.write(string.rep(" ", block - string.len(bytes)))
io.write(" ", string.gsub(bytes, "%c", "."), "\n")
end
0

精彩评论

暂无评论...
验证码 换一张
取 消