diff --git a/src/SampleBuf.jl b/src/SampleBuf.jl index e9da362..7ecee16 100644 --- a/src/SampleBuf.jl +++ b/src/SampleBuf.jl @@ -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 diff --git a/test/SampleBuf.jl b/test/SampleBuf.jl index afc51de..323bc8a 100644 --- a/test/SampleBuf.jl +++ b/test/SampleBuf.jl @@ -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