Add audio file seek capabilities

This commit is contained in:
Joris Kraak 2014-08-01 14:06:14 +02:00
parent 19c33d71c6
commit b79b47a461

View file

@ -1,4 +1,4 @@
export af_open, FilePlayer
export af_open, FilePlayer, rewind
const SFM_READ = int32(0x10)
const SFM_WRITE = int32(0x20)
@ -12,6 +12,10 @@ const SF_FORMAT_PCM_16 = 0x0002 # Signed 16 bit data
const SF_FORMAT_PCM_24 = 0x0003 # Signed 24 bit data
const SF_FORMAT_PCM_32 = 0x0004 # Signed 32 bit data
const SF_SEEK_SET = 0
const SF_SEEK_CUR = 1
const SF_SEEK_END = 2
const EXT_TO_FORMAT = [
".wav" => SF_FORMAT_WAV,
".flac" => SF_FORMAT_FLAC
@ -76,6 +80,21 @@ function Base.close(file::AudioFile)
end
end
function Base.seek(file::AudioFile, offset::Integer, whence::Integer)
new_offset = ccall((:sf_seek, libsndfile), Int64,
(Ptr{Void}, Int64, Int32), file.filePtr, offset, whence)
if new_offset < 0
error("Could not seek to $(offset) in file")
end
new_offset
end
# Some convenience methods for easily navigating through a sound file
Base.seek(file::AudioFile, offset::Integer) = seek(file, offset, SF_SEEK_SET)
rewind(file::AudioFile) = seek(file, 0, SF_SEEK_SET)
function af_open(f::Function, args...)
file = af_open(args...)
f(file)