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
Add an Audio display class #4302
Conversation
This is a first attempt at addressing issue 4241.
Hmm. I clearly have to normalize the data without using numpy. That I can do. |
import base64 | ||
import struct | ||
from io import BytesIO | ||
import wave |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's put this import inside the methods that use it - just in case someone has built Python without this module.
Too much copy paste I guess :)
The point of this is to improve compatibility with older browsers. also, encodestring is depricated in python 3.
I have tested this functionality on several platforms and there are some serious problems with browser compatibility. The state of my testing is that
Should I investigate what needs to be done to have better support on mobile? |
I'm not aware of what are the policies for the usage of extra js resources, but if it would be possible, this is a great option: https://github.com/happyworm/jplayer/ also It's available through a Cdn for "on demand" (async) load. |
The main targets for the notebook are the desktop versions of Chrome, FF On Tue, Oct 1, 2013 at 1:18 PM, Martín Gaitán notifications@github.comwrote:
Brian E. Granger |
A case of unwanted integer division.
I have noted that performance is an issue for longer signals. For instance, just normalizing a 60 second long high fidelity signal (44100 fps) takes about 8 seconds on my machine. This goes down to 500 milliseconds if numpy is used. What do you think about doing something like this?
|
That's a good idea David :) It's a little more Pythonic to do two things:
(Awesome looking first contribution by the way) On Wed, Oct 2, 2013 at 3:44 PM, David Österberg notifications@github.comwrote:
|
I'm wary of having a fallback that's significantly slower than the primary method, because it leads people to rely on something that becomes unreasonably slow when somebody runs it without the supporting tools. I'd be inclined to make numpy required for passing in raw data - the docstring already says that it expects a numpy array. In short: I'd prefer a clear failure explaining that it needs numpy to an unexplained hang of several seconds. |
Hmm. I think it is polite not to require numpy given that it isn't a hard dependency of IPython. I concede that if we do support a list of numbers as input we should say so in the docstring otherwise there is no point. So what if we always use the fallback if the user passes in a list and attempt to use numpy when a numpy array is passed. I think it is OK if it is slower in the rare edgecase where the user passes in a numpy array but doesn't have the numpy module. |
By the sounds of it, it's impractically slow without numpy, and I doubt anyone would try to create raw sound data without numpy. But yes, it's an acceptable compromise to allow lists but always use the slow normalisation for them, because that way the same notebook will always be about equally slow for the author and the user. There's no way to create a numpy array if you don't have the numpy module, thankfully. You might be able to prevent its import, but that's just one of many things that we'd consider 'being wilfully awkward', so we don't need to plan for that. |
I think it's a reasonable expectation that if someone is constructing an array of data to be encoded into a wav, it is a numpy array. They can always encode and pass wav data themselves, if they don't have numpy. |
Here is what I would recommend:
On Wed, Oct 2, 2013 at 2:39 PM, Min RK notifications@github.com wrote:
Brian E. Granger |
I've added a simple example in the rich display example notebook. Also, I have installed python 3.3 and tested my code. Lo and behold, my code worked! I guess I can finally switch to python3 now. I'm still wondering about this fallback performance stuff. After testing a bit I have realized that the generic normalizing method is actually only very slow if it is run on a numpy array. Hence if the pure python normalization is only used as a fallback it is only about 2x slower than the numpy version. It is not impractically slow for any data that you'd wanna embed in the notebook, in my opinion. I am fine either way. As @minrk said, most people will have numpy. @ellisonbg @takluyver what do you think? I don't think it is necessary to have a limit on the list length as @ellisonbg suggested. I tested embedding a 20 minute 44100 Hz signal without problem. That is an array of length 53e6. I think the user knows it will take a few seconds to embed that. When that question is resolved and unless there are other things I think this is ready to be reviewed. |
If it's only ~2x slower, I'm happy to have the fallback. |
It's not so much the performance that bothers me, it's adding extra code to handle a case that I don't expect to really be an issue. But if it's already there and the performance isn't too bad, then I don't see any great cost in leaving it. If we do encounter any issues with it, I would be inclined to remove the support rather than fix it. |
Is there anything else we want to do with this before merging it? |
I think it is ready to go. I tested the examples and it works as expected. @davidosterberg anything else you want to do on this before we merge? |
I am happy with the class. The example notebook could be improved. Now it links to a rather arbitrary wav file from the web. Also, I did not commit the cell output in the notebook. So someone should run the notebook so nbviewer shows the right thing. Should I do that? |
Yes, why don't you embed the audio in the cell so it works on nbviewer and On Wed, Oct 23, 2013 at 1:18 PM, David Österberg
Brian E. Granger |
Great, I'll merge this. I'm trying to land Python-based pull requests so I can start a branch to remove 2to3 from our Python 3 support. |
Add an Audio display class
I'm happy to see my initial attempt was picked up and brought to a state in which it could get merged 👍 |
This is a first attempt at addressing issue #4241.
In fact it is my first attempt at contributing back so please bear with me for a bit :)