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.
Alias for Platform.Program
Load audio so you can later play it.
An audio command.
Possible errors we can get when loading audio files.
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.
Play audio from an audio source at a given time with config.
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. If the the volume is less than 0, 0 will be used instead.
Scale how loud a given Audio is at given points in time. The volume will transition linearly between those points.
import Audio
import List.Nonempty exposing (Nonempty)
import Time
-- Here we define a function that fades in audio to full volume and then fades it out until it's muted.
--
-- 1 ________
-- / \
-- 0 ____________/ \_______
-- t -> fade in fade out
fadeInOut fadeInTime fadeOutTime audio =
Audio.scaleVolumeAt
(Nonempty
( addSeconds -2 fadeInTime, 0 )
[ ( fadeInTime, 1 )
, ( fadeOutTime, 1 )
, ( addSeconds 2 fadeOutTime, 0 )
]
)
audio
addSeconds s =
Time.posixToMillis >> ((+) s * 1000) >> Time.millisToPosixA nonempty list of (time, volume) points for defining how loud a sound should be at any point in time. The points don't need to be sorted but don't include multiple points that have the same time.
WIP support for Lamdera. Ignore this for now.
Lamdera.frontend but with the ability to play sounds (highly experimental, just ignore this for now).
Get the user state stored in Model.
Set the user state stored in Model. Useful for dealing with migrations in Lamdera.
Change the userMsg type in Model. Useful for dealing with migrations in Lamdera.
Browser.element but with the ability to play sounds.