Create an Elm app that supports playing audio.
Browser.document but with the ability to play sounds.
Browser.application but with the ability to play sounds.
The top level model for our program. This contains the model for your app as well as extra data needed to keep track of what audio is playing.
The top level msg for our program. This contains the msg type your app uses in addition to msgs that are needed to handle when audio gets loaded.
Information about audio files you have loaded. This is passed as a parameter to your update, view, subscriptions, and audio functions.
Load audio so you can later play it.
An audio command.
These are possible errors we can get when loading an audio source file.
Audio data we can use to play sounds
Map a command from one type to another. Conceptually the same as Cmd.map.
Combine multiple commands into a single command. Conceptually the same as Cmd.batch.
Define what audio should be playing.
Some kind of sound we want to play. To create Audio start with audio.
Play audio from an audio source at a given time. This is the same as using audioWithConfig audioDefaultConfig.
Note that in some browsers audio will be muted until user interacts with the webpage.
Play audio from an audio source at a given time with config.
Note that in some browsers audio will be muted until user interacts with the webpage.
Default config used for audioWithConfig.
Extra settings when playing audio from a file.
-- Here we play a song at half speed and it skips the first 15 seconds of the song.
audioWithConfig
{ loop = Nothing
, playbackRate = 0.5
, startAt = Duration.seconds 15
}
myCoolSong
songStartTimeControl how audio loops. loopEnd defines where (relative to the start of the audio) the audio should loop and loopStart defines where it should loop to.
-- Here we have a song that plays an intro once and then loops between the 10 second point and the end of the song.
let
default =
Audio.audioDefaultConfig
-- This package doesn't support getting how long a sound plays for so we need to hard code it instead.
songLength =
Duration.seconds 120
in
audioWithConfig
{ default | loop = Just { loopStart = Duration.seconds 10, loopEnd = songLength } }
coolBackgroundMusic
startTimeEffects you can apply to Audio.
Scale how loud a given Audio is.
1 preserves the current volume, 0.5 halves it, and 0 mutes it.
If the the volume is less than 0, 0 will be used instead.
Scale how loud some Audio is at different points in time.
The volume will transition linearly between those points.
The points in time don't need to be sorted but they need to be unique.
import Audio
import Duration
import Time
-- Here we define an audio function that fades in to full volume and then fades out until it's muted again.
--
-- 1 ________
-- / \
-- 0 ____________/ \_______
-- t -> fade in fade out
fadeInOut fadeInTime fadeOutTime audio =
Audio.scaleVolumeAt
[ ( Duration.subtractFrom fadeInTime Duration.second, 0 )
, ( fadeInTime, 1 )
, ( fadeOutTime, 1 )
, ( Duration.addTo fadeOutTime Duration.second, 0 )
]
audioAdd an offset to the audio.
import Audio
import Duration
delayByOneSecond audio =
Audio.offsetBy Duration.second audioWIP support for Lamdera. Ignore this for now.
Lamdera.frontend but with the ability to play sounds (highly experimental, just ignore this for now).
Use this function when migrating your model in Lamdera.
Use this function when migrating messages in Lamdera.
Browser.element but with the ability to play sounds.