conduit-audio-0.1: Combinators to efficiently slice and dice audio streams

Safe HaskellNone
LanguageHaskell2010

Data.Conduit.Audio

Contents

Description

A high-level functional interface for manipulating streams of audio.

Synopsis

Types

data AudioSource m a Source

An abstraction of a stream of audio. Inside is a Conduit Source which loads or generates smallish chunks of audio on demand. m is the Monad used by the Source to produce audio. a is the type of audio samples, contained in storable vectors (and thus should be Storable). Both (signed) Integral and Fractional sample types are supported.

Constructors

AudioSource 

Fields

source :: Source m (Vector a)

The stream of audio chunks; samples interleaved by channel. Each chunk can be any positive whole number of frames.

rate :: Rate
 
channels :: Channels
 
frames :: Frames

The stated length in frames of this audio stream. Not guaranteed to be exactly frame-accurate; the output of some operations like resampling or time-stretching may store only approximate frame counts.

type Seconds = Double Source

A duration in real time.

type Frames = Int Source

A frame consists of one sample for each audio channel.

type Rate = Double Source

The number of samples per second.

type Channels = Int Source

The number of audio channels (1 = mono, 2 = stereo, etc.)

data Duration Source

Used for functions that accept durations in either real time or audio frames.

Constructors

Seconds Seconds 
Frames Frames 

Generating audio

silent :: (Monad m, Num a, Storable a) => Duration -> Rate -> Channels -> AudioSource m a Source

Generates a stream of silence with the given parameters.

sine :: (Monad m, Floating a, Storable a) => a -> Duration -> Rate -> AudioSource m a Source

Generates a mono sine wave with the given frequency.

Combining audio

concatenate :: Monad m => AudioSource m a -> AudioSource m a -> AudioSource m a Source

Connects the end of the first audio source to the beginning of the second. The two sources must have the same sample rate and channel count.

mix :: (Monad m, Num a, Storable a) => AudioSource m a -> AudioSource m a -> AudioSource m a Source

Mixes two audio streams together by adding them samplewise. The two streams must have the same sample rate and channel count. It is recommended to only mix floating-point sample types. If you mix integral types and the result goes outside of the type's range, the result will not be a normal "clipping" effect, but will instead overflow, producing glitchy audio.

merge :: (Monad m, Num a, Storable a) => AudioSource m a -> AudioSource m a -> AudioSource m a Source

Combines the channels of two audio streams into a single source with all the channels. The two streams must have the same sample rate, but can have any number of channels.

splitChannels :: (Monad m, Storable a) => AudioSource m a -> [AudioSource m a] Source

Splits an audio stream into several, each providing a single channel of the original stream.

Editing audio

padStart :: (Monad m, Num a, Storable a) => Duration -> AudioSource m a -> AudioSource m a Source

Adds silence to the start of the audio stream.

padEnd :: (Monad m, Num a, Storable a) => Duration -> AudioSource m a -> AudioSource m a Source

Adds silence to the end of the audio stream.

takeStart :: (Monad m, Storable a) => Duration -> AudioSource m a -> AudioSource m a Source

Takes no more than the given duration of audio from the start of the stream.

takeEnd :: (Monad m, Storable a) => Duration -> AudioSource m a -> AudioSource m a Source

Takes no more than the given duration of audio from the end of the stream. This function relies on the frames value stored with the stream.

dropStart :: (Monad m, Storable a) => Duration -> AudioSource m a -> AudioSource m a Source

Drops the given duration of audio from the start of the stream.

dropEnd :: (Monad m, Storable a) => Duration -> AudioSource m a -> AudioSource m a Source

Drops the given duration of audio from the end of the stream. This function relies on the frames value stored with the stream.

fadeIn :: (Monad m, Ord a, Fractional a, Storable a) => AudioSource m a -> AudioSource m a Source

Fades the audio from start (silent) to end (original volume). This function relies on the frames value stored with the stream.

fadeOut :: (Monad m, Ord a, Fractional a, Storable a) => AudioSource m a -> AudioSource m a Source

Fades the audio from start (original volume) to end (silent). This function relies on the frames value stored with the stream.

mapSamples :: (Monad m, Storable a, Storable b) => (a -> b) -> AudioSource m a -> AudioSource m b Source

Applies a function to every sample in the audio stream.

gain :: (Monad m, Num a, Storable a) => a -> AudioSource m a -> AudioSource m a Source

Multiplies all the audio samples by the given scaling factor. It is best to use this function on floating-point sample types, for the same reasons that apply to mix.

Utility functions

vectorFrames :: Storable a => Vector a -> Channels -> Frames Source

Divides the vector length by the channel count to calculate the number of audio frames.

framesToSeconds :: Frames -> Rate -> Seconds Source

Uses the sample rate to convert frames to seconds.

secondsToFrames :: Seconds -> Rate -> Frames Source

Uses the sample rate to convert seconds to frames, rounding if necessary.

chunkSize :: Frames Source

An arbitrary size, in frames, for smallish audio chunks.

deinterleave :: Storable a => Channels -> Vector a -> [Vector a] Source

Given a vector with interleaved samples, like [L0, R0, L1, R1, ...], converts it into [[L0, L1, ...], [R0, R1, ...]].

interleave :: Storable a => [Vector a] -> Vector a Source

Opposite of deinterleave. All the input vectors should have the same length.

integralSample :: (RealFrac a, Integral b, Bounded b) => a -> b Source

Converts fractional samples in the range [-1, 1] to integral samples in a two's-complement type. Fractional samples beyond that range are clamped.

fractionalSample :: (Integral a, Bounded a, Fractional b) => a -> b Source

Converts integral samples in a two's-complement type to fractional samples in the range [-1, 1].