sound support

167 views
Skip to first unread message

Brandon Keith Biggs

unread,
Apr 3, 2015, 5:07:18 PM4/3/15
to bry...@googlegroups.com
Hello,
Are there any modules that deal with loading audio and playing it back?
I am horrible at javascript, but
This looks like it does everything
and
here is the audio javascript API.
OpenAl looks more like what the API is, but here is where brython may out-perform python libraries...
thanks,

Juan Carlos

unread,
Apr 3, 2015, 5:18:09 PM4/3/15
to bry...@googlegroups.com
On Fri, Apr 3, 2015 at 6:07 PM, Brandon Keith Biggs <brandonk...@gmail.com> wrote:
Hello,
Are there any modules that deal with loading audio and playing it back?

browser.html.AUDIO( *args )

Brandon Keith Biggs

unread,
Apr 4, 2015, 5:27:37 AM4/4/15
to bry...@googlegroups.com
Hello,
The html.audio module just is for
<audio>
I am looking for the audio within javascript.
With the <audio> tag you can't change the pitch, panning, create a 3d object, set a listener, set the bitrate, set the speed or any of the normal audio things one would need in a game.
http://www.w3.org/TR/webaudio/#AudioContext-section
the webaudio api handles all of this. Is it possible to just treat the webaudio like a module? That way we can just read the api above and import, set properties and whatnot?
Thanks,

--
You received this message because you are subscribed to the Google Groups "brython" group.
To unsubscribe from this group and stop receiving emails from it, send an email to brython+u...@googlegroups.com.
To post to this group, send email to bry...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/CALFJTa0HPQegZTcQnv-5bnWNUXF%3DP6pVQWS%2B9uCq3qFzfoVX-g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Kiko

unread,
Apr 7, 2015, 2:05:06 AM4/7/15
to bry...@googlegroups.com
You can access the API. See for example the following code:

from browser import window
from javascript import JSConstructor

sound = JSConstructor(window.AudioContext)()
print(dir(sound))

The result of the print shows the following:

['addEventListener', 'createAnalyser', 'createBiquadFilter', 'createBuffer', 'createBufferSource', 'createChannelMerger', 'createChannelSplitter', 'createConvolver', 'createDelay', 'createDynamicsCompressor', 'createGain', 'createMediaElementSource', 'createMediaStreamDestination', 'createMediaStreamSource', 'createOscillator', 'createPanner', 'createPeriodicWave', 'createScriptProcessor', 'createWaveShaper', 'currentTime', 'decodeAudioData', 'destination', 'dispatchEvent', 'listener', 'removeEventListener', 'sampleRate']

Brandon Keith Biggs

unread,
Apr 8, 2015, 5:50:13 PM4/8/15
to bry...@googlegroups.com
Hello,
This is great! But I honestly can't figure out how to get a sound into the audioContext.
There are some examples at:
http://www.html5rocks.com/en/tutorials/webaudio/intro/?redirect_from_locale=it

But I don't know how to get a buffer object from my audio and It looks like there is a built-in function in the audioContext using
AudioContext.decodeAudioData
But there are two needed arguments and I don't understand what the second argument is.
Has anyone managed to get audio playing through this API in brython?
On another note,

Audio = JSConstructor(window.Audio)
sound = Audio('sounds/hit.ogg')
sound.play()

Seems to work great. If worst comes to worst, I can try to use the Audio element along with AudioContext.createMediaElementSource(), but I don't think it will work exactly.
Thanks,

SoundMUD

unread,
Oct 8, 2015, 2:34:59 PM10/8/15
to brython
Hello,

Here is an example using Brython and WebAudio. I had to use Javascript (XHR2) to load a sound because the Ajax module doesn't seem to allow loading a file as an ArrayBuffer. Callbacks from Javascript functions to Brython functions seem to work great though.

http://jlpo.free.fr/webaudio/webaudio7.htm
http://jlpo.free.fr/webaudio/

Kiko

unread,
Oct 9, 2015, 4:05:30 AM10/9/15
to bry...@googlegroups.com
2015-10-08 20:34 GMT+02:00 SoundMUD <soun...@gmail.com>:
Hello,

Here is an example using Brython and WebAudio. I had to use Javascript (XHR2) to load a sound because the Ajax module doesn't seem to allow loading a file as an ArrayBuffer. Callbacks from Javascript functions to Brython functions seem to work great though.

http://jlpo.free.fr/webaudio/webaudio7.htm
http://jlpo.free.fr/webaudio/


It sounds great!!!!
If I find the time I will dig into your code to see if I can make the ArrayBuffer work properly.

Best.
 

SoundMUD

unread,
Oct 9, 2015, 2:34:17 PM10/9/15
to brython
Thank you! No need to spend time: since I'm growing more familiar with Javascript and Brython, I have translated the loadBuffer() part, so the entire code is in Brython. Here is a short version. I hope indentation will be kept.

http://jlpo.free.fr/webaudio/webaudio7.htm
http://jlpo.free.fr/webaudio/

from browser import document, timer, window
from javascript import JSConstructor


try:
	context_constructor = window.AudioContext
except:
	context_constructor = window.webkitAudioContext
context = JSConstructor(context_constructor)()


def load_buffer(context, url, callback):
	request = JSConstructor(window.XMLHttpRequest)()
	request.open("GET", url, True)
	request.responseType = "arraybuffer"
	def on_load():
		def on_error():
			window.console.error('decodeAudioData error', error)
		print(request)
		context.decodeAudioData(request.response, callback,	on_error)
	request.onload = on_load
	def on_error():
		window.alert('BufferLoader: XHR error')
	request.onerror = on_error
	request.send()


def load_sounds(urls, function_to_call_when_done):
	global buffers
	buffers = {}
	for key, value in urls.items():
		# Because of late binding, it is necessary to force early binding with "key=key",
		# or else "key" will have the last value of the loop.
		# http://stackoverflow.com/questions/3431676/creating-functions-in-a-loop
		def callback(buffer, key=key):
			buffers[key] = buffer
			if len(buffers) == len(urls):
				function_to_call_when_done()
		load_buffer(context, value, callback)


def start():
...


urls = {"techno": 'techno.wav', "open": 'open.wav', "close": 'close.wav'} load_sounds(urls, start)

Kiko

unread,
Oct 9, 2015, 3:06:29 PM10/9/15
to bry...@googlegroups.com
Great you feel more comfortable and familiar with Brython, her cousin
javascript and how they relate in the playground!!!

:-P

2015-10-09 20:34 GMT+02:00, SoundMUD <soun...@gmail.com>:
> Thank you! No need to spend time: since I'm growing more familiar with
> Javascript and Brython, I have translated the loadBuffer() part, so the
> entire code is in Brython. Here is a short version. I hope indentation will
>
> be kept.
>
> http://jlpo.free.fr/webaudio/webaudio7.htm
> http://jlpo.free.fr/webaudio/
>
> from browser import document, timer, windowfrom javascript import
> JSConstructortry: context_constructor =
> window.AudioContextexcept: context_constructor =
> window.webkitAudioContextcontext = JSConstructor(context_constructor)()def
> load_buffer(context, url, callback): request =
> JSConstructor(window.XMLHttpRequest)() request.open("GET", url,
> True) request.responseType = "arraybuffer" def on_load(): def
> on_error(): window.console.error('decodeAudioData error',
> error) print(request) context.decodeAudioData(request.response,
> callback, on_error) request.onload = on_load def
> on_error(): window.alert('BufferLoader: XHR error') request.onerror =
> on_error request.send()def load_sounds(urls,
> function_to_call_when_done): global buffers buffers = {} for key, value in
> urls.items(): # Because of late binding, it is necessary to force early
> binding with "key=key", # or else "key" will have the last value of the
> loop. #
> http://stackoverflow.com/questions/3431676/creating-functions-in-a-loop def
> callback(buffer, key=key): buffers[key] = buffer if len(buffers) ==
> len(urls): function_to_call_when_done() load_buffer(context, value,
> callback)
>
>
> def start():
> ...
>
>
> urls = {"techno": 'techno.wav', "open": 'open.wav', "close":
> 'close.wav'}load_sounds(urls, start)
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "brython" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to brython+u...@googlegroups.com.
> To post to this group, send email to bry...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/brython/fb6b05fc-2fc5-415b-bd28-7e05ecc2bb2a%40googlegroups.com.

SoundMUD

unread,
Oct 10, 2015, 1:43:26 PM10/10/15
to brython
Just a suggestion about "Using Javascript objects and libraries" ( http://www.brython.info/static_doc/en/jsobjects.html )

Maybe the fact that a reference to a Brython function can be sent as a callback (for example) parameter to a Javascript function isn't clear enough in the documentation. There is an "Accessing Brython objects from Javascript" subsection, but nothing about "accessing Brython functions from Javascript" (a simple sentence would do), and the closer I found was a not recommended method about "
forcing the introduction of the name echo in the Javascript namespace, by defining it as an attribute of the object window in module browser", followed by a warning about not doing this. I didn't read the "Other examples" linked at the end of the page though.
Reply all
Reply to author
Forward
0 new messages