MartinSStewart/elm-audio/3.0.1

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 model) (Msg 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 model) (Msg 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 model) (Msg msg)

Browser.application but with the ability to play sounds.

type Model userMsg userModel
type Msg userMsg

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.

Note that in some browsers audio will be muted until user interacts with the webpage.

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.

Note that in some browsers audio will be muted until user interacts with the webpage.

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.

-- 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
    songStartTime
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 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
    startTime

Audio effects

Effects you can apply to Audio.

scaleVolume : Float -> Audio -> 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.

scaleVolumeAt : List ( Posix, Float ) -> Audio -> Audio

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 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
        [ ( addSeconds -2 fadeInTime, 0 )
        , ( fadeInTime, 1 )
        , ( fadeOutTime, 1 )
        , ( addSeconds 2 fadeOutTime, 0 )
        ]
        audio


addSeconds s =
    Time.posixToMillis >> ((+) s * 1000) >> Time.millisToPosix

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

(msgOld -> msgNew)
-> (modelOld -> ( modelNew, Cmd msgNew ))
-> Model msgOld modelOld
-> ( Model msgNew modelNew, Cmd msgNew )

Use this function when migrating your model in Lamdera.

(msgOld -> ( msgNew, Cmd msgNew ))
-> Msg msgOld
-> ( Msg msgNew, Cmd msgNew )

Use this function when migrating messages in Lamdera.