Skip to content

Commit

Permalink
Added support for views of AbstractSampleBuf to be automatically be w…
Browse files Browse the repository at this point in the history
…rapped in an AbstractSampleBuf
  • Loading branch information
mchitre committed Mar 20, 2020
1 parent 2b72081 commit c5ab6e4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/SampleBuf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ Base.similar(buf::SampleBuf, ::Type{T}, dims::Dims) where {T} = SampleBuf(Array{
Base.similar(buf::SpectrumBuf, ::Type{T}, dims::Dims) where {T} = SpectrumBuf(Array{T}(undef, dims), framerate(buf))
domain(buf::AbstractSampleBuf) = range(0.0, stop=(nframes(buf)-1)/framerate(buf), length=nframes(buf))

# deal with re-wrapping views
Base.view(buf::AbstractSampleBuf, ind...) = SampleBuf(view(buf.data, ind...), buf.samplerate)

# There's got to be a better way to define these functions, but the dispatch
# and broadcast behavior for AbstractArrays is complex and has subtle differences
# between Julia versions, so we basically just override functions here as they
Expand Down
9 changes: 9 additions & 0 deletions test/SampleBuf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -519,4 +519,13 @@ end
display(TextDisplay(iobuf), buf)
@test String(take!(iobuf)) == expected
end

@testset "views on SampleBuf work" begin
buf = SampleBuf(ones(500), 48000)
vbuf = @view buf[100:200]
@test vbuf isa SampleBuf
vbuf[10] = 2.7
@test buf[109] == 2.7
end

end

0 comments on commit c5ab6e4

Please sign in to comment.