diff options
author | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2016-09-03 19:20:11 +0200 |
---|---|---|
committer | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2016-09-03 19:20:11 +0200 |
commit | d104bd746605cb38977173beb079ebac7e8fa0ce (patch) | |
tree | 2f3d25cd40f67a946f1fd455cca4187c371143d5 | |
parent | 148195537704906dd9a50cea8442a4df0cd8b16f (diff) | |
download | applause2-d104bd746605cb38977173beb079ebac7e8fa0ce.tar.gz |
SndfileStream() supports raw files now, by passing through samplerate, channels and libsndfile format
-rw-r--r-- | applause.lua | 23 | ||||
-rw-r--r-- | sndfile.lua | 4 |
2 files changed, 23 insertions, 4 deletions
diff --git a/applause.lua b/applause.lua index e257fb2..1a67ed7 100644 --- a/applause.lua +++ b/applause.lua @@ -303,6 +303,17 @@ function Stream:scale(v1, v2) end) end +-- same as Stream:scale() but for values between [0, 127] +-- (ie. MIDI CC values) +function Stream:ccscale(v1, v2) + local lower = v2 and v1 or 0 + local upper = v2 or v1 + + return self:map(function(x) + return (x/127)*(upper - lower) + lower + end) +end + function Stream:scan(fnc) return ScanStream:new(self, fnc) end @@ -943,12 +954,17 @@ end SndfileStream = DeriveClass(Stream) -function SndfileStream:ctor(filename) +function SndfileStream:ctor(filename, samplerate, channels, format) -- FIXME: This fails if the file is not at the -- correct sample rate. Need to resample... - local handle = sndfile:new(filename, "SFM_READ") + -- NOTE: samplerate and channels are ignored unless SF_FORMAT_RAW + -- files are read. + local handle = sndfile:new(filename, "SFM_READ", + samplerate, channels, format) self.filename = filename + self.samplerate = handle.info.samplerate self.channels = handle.info.channels + self.format = handle.info.format self.frames = tonumber(handle.info.frames) handle:close() @@ -969,7 +985,8 @@ function SndfileStream:gtick() -- read pointer which is important when reusing the stream. -- NOTE: We could do this with a single handle per object but -- by maintaining our own read position and seeking before reading. - local handle = sndfile:new(self.filename, "SFM_READ") + local handle = sndfile:new(self.filename, "SFM_READ", + self.samplerate, self.channels, self.format) -- Make sure that we are still reading the same file; -- at least with the same meta-data. diff --git a/sndfile.lua b/sndfile.lua index e91fb70..13c952d 100644 --- a/sndfile.lua +++ b/sndfile.lua @@ -1,6 +1,7 @@ -- module table local sndfile = {} +local bit = require "bit" local ffi = require "ffi" ffi.cdef[[ @@ -165,7 +166,8 @@ function sndfile:new(path, mode, samplerate, channels, format) local info = ffi.new("SF_INFO[1]") - if mode ~= "SFM_READ" then + if mode == "SFM_WRITE" or + (format and bit.band(format, ffi.C.SF_FORMAT_TYPEMASK) == ffi.C.SF_FORMAT_RAW) then info[0].samplerate = samplerate or 44100 info[0].channels = channels or 1 info[0].format = format or sndfile.guess_format(path) |