Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Caption for WAV display #100

Closed
rmsrosa opened this issue Jun 13, 2021 · 3 comments · Fixed by #104
Closed

Caption for WAV display #100

rmsrosa opened this issue Jun 13, 2021 · 3 comments · Fixed by #104

Comments

@rmsrosa
Copy link
Contributor

rmsrosa commented Jun 13, 2021

Hi,

For my notebooks, I added a composite type similar to WAVArray which adds a "caption" field, which is then used in show to display a caption in an html figure element wrapping the html audio controls.

Ideally, it should replace the existing WAVArray, but that could be a breaking change, so I made it into a new struct CWAVArray, also for the lack of a better name.

It doesn't look well in the same cell as WAVArray, due to the identation, but one can use CWAVArray with the empty string if no caption is desired.

If you like the idea, I could make a PR with the above. Let me know.

This is essentially it:

struct CWAVArray{T,N}
    Fs::Number
    data::AbstractArray{T,N}
    caption::AbstractString
end

CWAVArray(Fs::Number, data::AbstractArray) = CWAVArray(Fs, data, "")

wavwrite(x::CWAVArray, io::IO) = wavwrite(x.data, io; Fs=x.Fs)

function show(io::IO, ::MIME"text/html", x::CWAVArray)
    buf = IOBuffer()
    wavwrite(x, buf)
    data = base64encode(String(take!(copy(buf))))
    markup = """<figure>
                $(ifelse(x.caption !== "", "<figcaption>$(x.caption)</figcaption>", ""))
                <audio controls="controls" {autoplay}>
                <source src="data:audio/wav;base64,$data" type="audio/wav" />
                Your browser does not support the audio element.
                </audio>
                </figure>"""
    print(io, markup)
end

And here is how it displays in a jupyter notebook, including the case with an empty caption:

instruments

sinewaves

nocaption

@dancasimiro
Copy link
Owner

@rmsrosa This looks like a nice addition. Could you help me understand why you cannot extend the existing WAVArray type? I'm probably missing some obvious detail, but it seems like this functionality could be added without breaking the API.

@rmsrosa
Copy link
Contributor Author

rmsrosa commented Jun 26, 2021

Yeah, I guess it doesn't break it. It adds a field, but it should be fine with the constructor that only takes the first two fields.

Should I make a PR?

@dancasimiro
Copy link
Owner

Yeah, I guess it doesn't break it. It adds a field, but it should be fine with the constructor that only takes the first two fields.

Should I make a PR?

Yes, please! A PR would be very much appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants