MartinSStewart/elm-audio/2.2.0

NOTE: the latest version is 4.0.4

Audio

Applications

Create an Elm app that supports playing audio.

{ init : flags -> ( model, Cmd msg, AudioCmd msg )
, view : model -> Html msg
, update : msg -> model -> ( model, Cmd msg, AudioCmd msg )
, subscriptions : model -> Sub msg
, audio : model -> Audio
, audioPort : Ports msg
}
-> Program flags model msg

Browser.element but with the ability to play sounds.

{ init : flags -> ( model, Cmd msg, AudioCmd msg )
, view : model -> Document msg
, update : msg -> model -> ( model, Cmd msg, AudioCmd msg )
, subscriptions : model -> Sub msg
, audio : model -> Audio
, audioPort : Ports msg
}
-> Program flags model msg

Browser.document but with the ability to play sounds.

{ init : flags -> Url -> Key -> ( model, Cmd msg, AudioCmd msg )
, view : model -> Document msg
, update : msg -> model -> ( model, Cmd msg, AudioCmd msg )
, subscriptions : model -> Sub msg
, onUrlRequest : UrlRequest -> msg
, onUrlChange : Url -> msg
, audio : model -> Audio
, audioPort : Ports msg
}
-> Program flags model msg

Browser.application but with the ability to play sounds.

type alias Program flags userModel userMsg =
Program flags (Model userMsg userModel) (Msg userMsg)

Alias for Platform.Program

Load audio

Load audio so you can later play it.

type AudioCmd userMsg

An audio command.

loadAudio : (Result LoadError Source -> msg) -> String -> AudioCmd msg

Load audio from a url.

= MediaDecodeAudioDataUnknownContentType
| NetworkError
| ErrorThatHappensWhenYouLoadMoreThan1000SoundsDueToHackyWorkAroundToMakeThisPackageBehaveMoreLikeAnEffectPackage

Possible errors we can get when loading audio files.

type Source

Audio data we can use to play sounds

cmdMap : (a -> b) -> AudioCmd a -> AudioCmd b

Map a command from one type to another. Conceptually the same as Cmd.map

cmdBatch : List (AudioCmd userMsg) -> AudioCmd userMsg

Combine multiple commands into a single command. Conceptually the same as Cmd.batch.

A command that does nothing. Conceptually the same as Cmd.none.

Play audio

Define what audio should be playing.

type Audio

Some kind of sound we want to play. To create Audio start with audio.

audio : Source -> Posix -> Audio

Play audio from an audio source at a given time. This is the same as using audioWithConfig audioDefaultConfig.

group : List Audio -> Audio

Combine multiple Audios into a single Audio.

The sound of no sound at all.

audioWithConfig : PlayAudioConfig -> Source -> Posix -> Audio

Play audio from an audio source at a given time with config.

audioDefaultConfig : PlayAudioConfig

Default config used for audioWithConfig.

type alias PlayAudioConfig =
{ loop : Maybe LoopConfig
, playbackRate : Float
, startAt : Duration
}

Extra settings when playing audio from a file.

type alias LoopConfig =
{ loopStart : Duration
, loopEnd : Duration
}

Control how audio loops. loopEnd defines where (relative to the start of the audio) the audio should loop and loopStart 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
    startTime

Audio effects

Effects you can apply to Audio.

scaleVolume : Float -> Audio -> Audio

Scale how loud a given Audio is. If the the volume is less than 0, 0 will be used instead.

scaleVolumeAt : VolumeTimeline -> Audio -> Audio

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.millisToPosix
type alias VolumeTimeline =
Nonempty ( Posix, Float )

A 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.

Lamdera stuff

WIP support for Lamdera. Ignore this for now.

{ init :
Url -> Key -> ( model, Cmd frontendMsg, AudioCmd frontendMsg )
, view : model -> Document frontendMsg
, update :
frontendMsg
-> model
-> ( model, Cmd frontendMsg, AudioCmd frontendMsg )
, updateFromBackend :
toFrontend
-> model
-> ( model, Cmd frontendMsg, AudioCmd frontendMsg )
, subscriptions : model -> Sub frontendMsg
, onUrlRequest : UrlRequest -> frontendMsg
, onUrlChange : Url -> frontendMsg
, audio : model -> Audio
, audioPort : Ports frontendMsg
}
-> { init :
Url -> Key -> ( Model frontendMsg model, Cmd (Msg frontendMsg) )
, view : Model frontendMsg model -> Document (Msg frontendMsg)
, update :
Msg frontendMsg
-> Model frontendMsg model
-> ( Model frontendMsg model, Cmd (Msg frontendMsg) )
, updateFromBackend :
toFrontend
-> Model frontendMsg model
-> ( Model frontendMsg model, Cmd (Msg frontendMsg) )
, subscriptions : Model frontendMsg model -> Sub (Msg frontendMsg)
, onUrlRequest : UrlRequest -> Msg frontendMsg
, onUrlChange : Url -> Msg frontendMsg
}

Lamdera.frontend but with the ability to play sounds (highly experimental, just ignore this for now).

userModel : Model userMsg userModel -> userModel

Get the user state stored in Model.

userModelNew
-> Model userMsg userModelOld
-> Model userMsg userModelNew

Set the user state stored in Model. Useful for dealing with migrations in Lamdera.

(userMsgOld -> userMsgNew)
-> Model userMsgOld userModel
-> Model userMsgNew userModel

Change the userMsg type in Model. Useful for dealing with migrations in Lamdera.