diff --git a/Project.toml b/Project.toml index 3634edf..1262700 100644 --- a/Project.toml +++ b/Project.toml @@ -9,6 +9,7 @@ FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341" FixedPointNumbers = "53c48c17-4a7d-5ca2-90c5-79b7896eea93" IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" +SignalBase = "00c44e92-20f5-44bc-8f45-a1dcef76ba38" TreeViews = "a2a6695c-b41b-5b7d-aed9-dbfdeacea5d7" Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" diff --git a/src/SampleBuf.jl b/src/SampleBuf.jl index 85f22a3..7c2316b 100644 --- a/src/SampleBuf.jl +++ b/src/SampleBuf.jl @@ -47,11 +47,15 @@ SpectrumBuf(T::Type, sr, len::Quantity, ch) = # channel - a set of samples running in parallel # frame - a collection of samples from each channel that were sampled simultaneously +# SignalBase methods +SignalBase.nframes(buf::AbstractSampleBuf) = size(buf.data, 1) +SignalBase.framerate(buf::AbstractSampleBuf) = buf.samplerate +SignalBase.nchannels(buf::AbstractSampleBuf{T, 2}) where {T} = size(buf.data, 2) +SignalBase.nchannels(buf::AbstractSampleBuf{T, 1}) where {T} = 1 +SignalBase.sampletype(buf::AbstractSampleBuf) = eltype(buf.data) + # audio methods samplerate(buf::AbstractSampleBuf) = buf.samplerate -nchannels(buf::AbstractSampleBuf{T, 2}) where {T} = size(buf.data, 2) -nchannels(buf::AbstractSampleBuf{T, 1}) where {T} = 1 -nframes(buf::AbstractSampleBuf) = size(buf.data, 1) function samplerate!(buf::AbstractSampleBuf, sr) buf.samplerate = sr @@ -60,8 +64,8 @@ function samplerate!(buf::AbstractSampleBuf, sr) end # define audio methods on raw buffers as well -nframes(arr::AbstractArray) = size(arr, 1) -nchannels(arr::AbstractArray) = size(arr, 2) +SignalBase.nframes(arr::AbstractArray) = size(arr, 1) +SignalBase.nchannels(arr::AbstractArray) = size(arr, 2) # it's important to define Base.similar so that range-indexing returns the # right type, instead of just a bare array diff --git a/src/SampleStream.jl b/src/SampleStream.jl index db6a838..52ef8e8 100644 --- a/src/SampleStream.jl +++ b/src/SampleStream.jl @@ -256,7 +256,7 @@ function UpMixSink(wrapped::SampleSink, blocksize=DEFAULT_BLOCKSIZE) end samplerate(sink::UpMixSink) = samplerate(sink.wrapped) -nchannels(sink::UpMixSink) = 1 +SignalBase.nchannels(sink::UpMixSink) = 1 Base.eltype(sink::UpMixSink) = eltype(sink.wrapped) blocksize(sink::UpMixSink) = size(sink.buf, 1) @@ -296,7 +296,7 @@ function DownMixSink(wrapped::SampleSink, channels, blocksize=DEFAULT_BLOCKSIZE) end samplerate(sink::DownMixSink) = samplerate(sink.wrapped) -nchannels(sink::DownMixSink) = sink.channels +SignalBase.nchannels(sink::DownMixSink) = sink.channels Base.eltype(sink::DownMixSink) = eltype(sink.wrapped) blocksize(sink::DownMixSink) = size(sink.buf, 1) @@ -340,7 +340,7 @@ function ReformatSink(wrapped::SampleSink, T, blocksize=DEFAULT_BLOCKSIZE) end samplerate(sink::ReformatSink) = samplerate(sink.wrapped) -nchannels(sink::ReformatSink) = nchannels(sink.wrapped) +SignalBase.nchannels(sink::ReformatSink) = nchannels(sink.wrapped) Base.eltype(sink::ReformatSink) = sink.typ blocksize(sink::ReformatSink) = nframes(sink.buf) @@ -385,7 +385,7 @@ function ResampleSink(wrapped::SampleSink, sr, blocksize=DEFAULT_BLOCKSIZE) end samplerate(sink::ResampleSink) = sink.samplerate -nchannels(sink::ResampleSink) = nchannels(sink.wrapped) +SignalBase.nchannels(sink::ResampleSink) = nchannels(sink.wrapped) Base.eltype(sink::ResampleSink) = eltype(sink.wrapped) # TODO: implement blocksize for this @@ -428,7 +428,7 @@ end SampleBufSource(buf::SampleBuf) = SampleBufSource(buf, 0) samplerate(source::SampleBufSource) = samplerate(source.buf) -nchannels(source::SampleBufSource) = nchannels(source.buf) +SignalBase.nchannels(source::SampleBufSource) = nchannels(source.buf) Base.eltype(source::SampleBufSource) = eltype(source.buf) function unsafe_read!(source::SampleBufSource, buf::Array, frameoffset, framecount) @@ -451,7 +451,7 @@ end SampleBufSink(buf::SampleBuf) = SampleBufSink(buf, 0) samplerate(sink::SampleBufSink) = samplerate(sink.buf) -nchannels(sink::SampleBufSink) = nchannels(sink.buf) +SignalBase.nchannels(sink::SampleBufSink) = nchannels(sink.buf) Base.eltype(sink::SampleBufSink) = eltype(sink.buf) function unsafe_write(sink::SampleBufSink, buf::Array, frameoffset, framecount) diff --git a/src/SampledSignals.jl b/src/SampledSignals.jl index a8a88a6..6bc0e5d 100644 --- a/src/SampledSignals.jl +++ b/src/SampledSignals.jl @@ -5,6 +5,9 @@ module SampledSignals using IntervalSets +using SignalBase +export nframes, nchannels + export AbstractSampleBuf, SampleBuf, SpectrumBuf export SampleSource, SampleSink export SampleRate @@ -13,7 +16,7 @@ export SampleBufSource, SampleBufSink export SinSource export ClosedInterval, .. # general methods for types in SampledSignals -export samplerate, samplerate!, nchannels, nframes +export samplerate, samplerate! export domain, channelptr, blocksize, metadata export mix!, mix, mono!, mono # re-export the useful units diff --git a/src/SignalGen/SinSource.jl b/src/SignalGen/SinSource.jl index bb03f03..d0f5070 100644 --- a/src/SignalGen/SinSource.jl +++ b/src/SignalGen/SinSource.jl @@ -21,7 +21,7 @@ end SinSource(eltype, samplerate, freq::Real) = SinSource(eltype, samplerate, [freq]) Base.eltype(::SinSource{T}) where T = T -nchannels(source::SinSource) = length(source.freqs) +SignalBase.nchannels(source::SinSource) = length(source.freqs) samplerate(source::SinSource) = source.samplerate function unsafe_read!(source::SinSource, buf::Array, frameoffset, framecount)