Skip to content

Commit

Permalink
Add common speaker position configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkkrp committed Dec 6, 2016
1 parent f6b2848 commit b7906b8
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 20 deletions.
104 changes: 95 additions & 9 deletions Codec/Audio/Wave.hs
Expand Up @@ -58,6 +58,15 @@ module Codec.Audio.Wave
, waveChannels
, waveSamplesTotal
, waveDuration
-- * Common speaker configurations
, speakerMono
, speakerStereo
, speakerQuad
, speakerSurround
, speaker5Point1
, speaker7Point1
, speaker5Point1Surround
, speaker7Point1Surround
-- * Reading
, readWaveFile
-- * Writing
Expand Down Expand Up @@ -254,6 +263,85 @@ waveDuration :: Wave -> Double
waveDuration wave =
fromIntegral (waveSamplesTotal wave) / fromIntegral (waveSampleRate wave)

----------------------------------------------------------------------------
-- Common speaker configurations

-- | Front center (C).

speakerMono :: Set SpeakerPosition
speakerMono = E.fromList [SpeakerFrontCenter]

-- | Front left (L), front right (R).

speakerStereo :: Set SpeakerPosition
speakerStereo = E.fromList [SpeakerFrontLeft,SpeakerFrontRight]

-- | L, R, back left (Lb), back right (Rb).

speakerQuad :: Set SpeakerPosition
speakerQuad = E.fromList
[ SpeakerFrontLeft
, SpeakerFrontRight
, SpeakerBackLeft
, SpeakerBackRight ]

-- | Surround: L, R, front center (C), back center (Cb).

speakerSurround :: Set SpeakerPosition
speakerSurround = E.fromList
[ SpeakerFrontLeft
, SpeakerFrontRight
, SpeakerFrontCenter
, SpeakerBackCenter ]

-- | L, R, C, Lb, Rb, low frequency (LFE).

speaker5Point1 :: Set SpeakerPosition
speaker5Point1 = E.fromList
[ SpeakerFrontLeft
, SpeakerFrontRight
, SpeakerFrontCenter
, SpeakerBackLeft
, SpeakerBackRight
, SpeakerLowFrequency ]

-- | L, R, C, Lb, Rb, front left-of-center, front right-of-center, LFE.

speaker7Point1 :: Set SpeakerPosition
speaker7Point1 = E.fromList
[ SpeakerFrontLeft
, SpeakerFrontRight
, SpeakerFrontCenter
, SpeakerBackLeft
, SpeakerBackRight
, SpeakerFrontLeftOfCenter
, SpeakerFrontRightOfCenter
, SpeakerLowFrequency ]

-- | L, R, C, side left (Ls), side right (Rs), LFE.

speaker5Point1Surround :: Set SpeakerPosition
speaker5Point1Surround = E.fromList
[ SpeakerFrontLeft
, SpeakerFrontRight
, SpeakerFrontCenter
, SpeakerSideLeft
, SpeakerSideRight
, SpeakerLowFrequency ]

-- | L, R, C, Lb, Rb, Ls, Rs, LFE.

speaker7Point1Surround :: Set SpeakerPosition
speaker7Point1Surround = E.fromList
[ SpeakerFrontLeft
, SpeakerFrontRight
, SpeakerFrontCenter
, SpeakerBackLeft
, SpeakerBackRight
, SpeakerSideLeft
, SpeakerSideRight
, SpeakerLowFrequency ]

----------------------------------------------------------------------------
-- Reading

Expand Down Expand Up @@ -546,15 +634,13 @@ fromSpeakerMask channelMask = E.fromList $ mapMaybe f [minBound..maxBound]
-- | Get default speaker set for given number of channels.

defaultSpeakerSet :: Word16 -> Set SpeakerPosition
defaultSpeakerSet = E.fromList . f . fromIntegral
where
f n = case n of
0 -> []
1 -> [SpeakerFrontCenter]
2 -> [SpeakerFrontLeft,SpeakerFrontRight]
3 -> [SpeakerFrontLeft,SpeakerFrontCenter,SpeakerFrontRight]
4 -> [SpeakerFrontLeft,SpeakerFrontRight,SpeakerBackLeft,SpeakerBackRight]
x -> take x [minBound..maxBound]
defaultSpeakerSet n = case n of
0 -> E.empty
1 -> speakerMono
2 -> speakerStereo
3 -> E.fromList [SpeakerFrontLeft,SpeakerFrontCenter,SpeakerFrontRight]
4 -> speakerSurround
x -> E.fromList $ take (fromIntegral x) [minBound..maxBound]

-- | Does this 'Wave' record requires extensible format chunk to be used?

Expand Down
21 changes: 10 additions & 11 deletions tests/Codec/Audio/WaveSpec.hs
Expand Up @@ -39,7 +39,6 @@ where

import Codec.Audio.Wave
import Test.Hspec
import qualified Data.Set as E

-- The test suite has two parts. In the first part we establish that the
-- library is capable of reading various sample files. In the second part,
Expand All @@ -60,7 +59,7 @@ spec = do
waveFileFormat `shouldBe` WaveVanilla
waveSampleRate `shouldBe` 8000
waveSampleFormat `shouldBe` SampleFormatPcmUnsigned 8
waveChannelMask `shouldBe` E.fromList [SpeakerFrontLeft,SpeakerFrontRight]
waveChannelMask `shouldBe` speakerStereo
waveDataOffset `shouldBe` 44
waveDataSize `shouldBe` 11376
waveOtherChunks `shouldBe` []
Expand All @@ -77,7 +76,7 @@ spec = do
waveFileFormat `shouldBe` WaveVanilla
waveSampleRate `shouldBe` 11025
waveSampleFormat `shouldBe` SampleFormatPcmSigned 24
waveChannelMask `shouldBe` E.fromList [SpeakerFrontLeft,SpeakerFrontRight]
waveChannelMask `shouldBe` speakerStereo
waveDataOffset `shouldBe` 44
waveDataSize `shouldBe` 23274
waveOtherChunks `shouldBe` []
Expand All @@ -94,7 +93,7 @@ spec = do
waveFileFormat `shouldBe` WaveVanilla
waveSampleRate `shouldBe` 44100
waveSampleFormat `shouldBe` SampleFormatPcmSigned 16
waveChannelMask `shouldBe` E.fromList [SpeakerFrontCenter]
waveChannelMask `shouldBe` speakerMono
waveDataOffset `shouldBe` 44
waveDataSize `shouldBe` 5046
waveOtherChunks `shouldBe` []
Expand All @@ -111,7 +110,7 @@ spec = do
waveFileFormat `shouldBe` WaveVanilla
waveSampleRate `shouldBe` 48000
waveSampleFormat `shouldBe` SampleFormatIeeeFloat32Bit
waveChannelMask `shouldBe` E.fromList [SpeakerFrontCenter]
waveChannelMask `shouldBe` speakerMono
waveDataOffset `shouldBe` 80
waveDataSize `shouldBe` 48140
waveOtherChunks `shouldBe`
Expand All @@ -129,7 +128,7 @@ spec = do
waveFileFormat `shouldBe` WaveVanilla
waveSampleRate `shouldBe` 16000
waveSampleFormat `shouldBe` SampleFormatIeeeFloat64Bit
waveChannelMask `shouldBe` E.fromList [SpeakerFrontCenter]
waveChannelMask `shouldBe` speakerMono
waveDataOffset `shouldBe` 80
waveDataSize `shouldBe` 104080
waveOtherChunks `shouldBe`
Expand All @@ -148,7 +147,7 @@ spec = do
waveFileFormat `shouldBe` WaveVanilla
waveSampleRate `shouldBe` 8000
waveSampleFormat `shouldBe` SampleFormatPcmUnsigned 8
waveChannelMask `shouldBe` E.fromList [SpeakerFrontLeft,SpeakerFrontRight]
waveChannelMask `shouldBe` speakerStereo
waveDataOffset `shouldBe` 80
waveDataSize `shouldBe` 11376
waveOtherChunks `shouldBe` [("fact","8\SYN\NUL\NUL")]
Expand All @@ -165,7 +164,7 @@ spec = do
waveFileFormat `shouldBe` WaveVanilla
waveSampleRate `shouldBe` 11025
waveSampleFormat `shouldBe` SampleFormatPcmSigned 24
waveChannelMask `shouldBe` E.fromList [SpeakerFrontLeft,SpeakerFrontRight]
waveChannelMask `shouldBe` speakerStereo
waveDataOffset `shouldBe` 80
waveDataSize `shouldBe` 23274
waveOtherChunks `shouldBe` [("fact","'\SI\NUL\NUL")]
Expand All @@ -182,7 +181,7 @@ spec = do
waveFileFormat `shouldBe` WaveVanilla
waveSampleRate `shouldBe` 44100
waveSampleFormat `shouldBe` SampleFormatPcmSigned 16
waveChannelMask `shouldBe` E.fromList [SpeakerFrontCenter]
waveChannelMask `shouldBe` speakerMono
waveDataOffset `shouldBe` 80
waveDataSize `shouldBe` 5046
waveOtherChunks `shouldBe` [("fact","\219\t\NUL\NUL")]
Expand All @@ -199,7 +198,7 @@ spec = do
waveFileFormat `shouldBe` WaveVanilla
waveSampleRate `shouldBe` 48000
waveSampleFormat `shouldBe` SampleFormatIeeeFloat32Bit
waveChannelMask `shouldBe` E.fromList [SpeakerFrontCenter]
waveChannelMask `shouldBe` speakerMono
waveDataOffset `shouldBe` 104
waveDataSize `shouldBe` 48140
waveOtherChunks `shouldBe`
Expand All @@ -217,7 +216,7 @@ spec = do
waveFileFormat `shouldBe` WaveVanilla
waveSampleRate `shouldBe` 16000
waveSampleFormat `shouldBe` SampleFormatIeeeFloat64Bit
waveChannelMask `shouldBe` E.fromList [SpeakerFrontCenter]
waveChannelMask `shouldBe` speakerMono
waveDataOffset `shouldBe` 104
waveDataSize `shouldBe` 104080
waveOtherChunks `shouldBe`
Expand Down

0 comments on commit b7906b8

Please sign in to comment.