Skip to content

Commit

Permalink
implemented SignalBase API and changed nframes and nchannels to Signa…
Browse files Browse the repository at this point in the history
…lBase.nframes and SignalBase.nchannels
  • Loading branch information
mchitre committed Mar 20, 2020
1 parent 2e17121 commit cab9d71
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
14 changes: 9 additions & 5 deletions src/SampleBuf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions src/SampleStream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion src/SampledSignals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ module SampledSignals

using IntervalSets

using SignalBase
export nframes, nchannels

export AbstractSampleBuf, SampleBuf, SpectrumBuf
export SampleSource, SampleSink
export SampleRate
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/SignalGen/SinSource.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit cab9d71

Please sign in to comment.