rgill
Is there an elixir audio package?
Hi guys,
New to the forum here and somewhat new to elixir as well. I’ve been going through tutorials and doing the Dave Thomas’ course on elixir, but I’ve been looking to build something in the mean time. I had a project that I wrote in javascript which output sounds through the speakers and I was thinking of porting that to elixir for funsies.
However, I couldn’t find anything related to audio output through google or any package on hex.pm, is there anything already? Does anyone have an idea?
For reference: I was using this package in javascript (https://www.npmjs.com/package/speaker)
Most Liked
OvermindDL1
I’d imagine whipping up the Rust rodio library in rustler would make a good setup. ![]()
rgill
Got a working version, would you mind reviewing it? I’m still unsure about best practices in elixir so might be doing something blatantly wrong or not following better conventions.
NobbZ
Because what you call a stream can be anything, a function, a struct of an unspecified module, a pid… And nothing but the Stream module knows how to deal with them.
Streams have to be considered opaque.
rgill
This is really nice, thanks a bunch for the great review.
I have been following Dave Thomas’ course on Elixir and he recommends an approach to only have public methods in the root file and the rest should essentially go to modules of their own (which at least for now I think is a good approach) and then you defdelegate to the module methods.
I haven’t published it to hex yet since I wanted to iron out some stuff and make it a bit more stable-ish (not that anybody is gonna use it right away hehe).
Haven’t tried it on NIF modules, but I would add some there as well.
Specs are awesome
and yup, they do come through defdelegate.
I added the validation through PlayerValidator because to be honest I just didn’t know how to do that in rust, I’m only starting to explore rust for this library. If you can point me to some examples, I can rework the validation.
And no, at the moment add_to_queue doesn’t accept file:// type paths, though I am definitely going to add that since it’s natively supported by rodio.
That was my first approach as well, but then I kinda got blocked by the elixir → rustler → rust type system and gave up on that to at least have some working version first. If you have any more ideas, I would happily try it again.
Yes, that’s actually easy since rodio provides a function to do it already.
As far as I understood, this is something that rodio does already, I haven’t really tested if it’s based on the file extension or what but you can pass different types of audio URLs and they’ll be played fine. But yeah, I need to research this more and maybe create a struct as AudioURL or something.
Had no idea how to use expect, I would have read up on error handling in rust. Can you expand a little on why they should never be in production code though?
Yeah, hopefully, I’ll keep on exposing more rodio functions ![]()
Thanks again for taking the time and reviewing it ![]()
OvermindDL1
Should be a fairly simple mapping, what did you get stuck on in it?
A source can be user defined, rodio just comes with some default things. For a binary with a ‘type’ you’d instance the proper decoder for the given type (wave, ogg, etc…) and it will return a source for you. For raw data you can just feed it directly in, though it would need some other information like it’s rate and so forth.
expect hard panics, bringing down the entire process, this will include the BEAM itself, it should only be used when the system is in an entirely unrecoverable state at that time.







