Skip to content

Commit

Permalink
introduced type class for a playable element
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud Bailly authored and Arnaud Bailly committed Feb 19, 2012
1 parent 0db54c7 commit 29f0499
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
17 changes: 10 additions & 7 deletions Music.hs
Expand Up @@ -33,6 +33,9 @@ data Note = Note {
data Chord = Chord [Note] Duration
deriving (Eq,Ord,Show,Read)

class Playable p where
interpret :: Tempo -> p -> Wave

-- tempi in bpm
allegro = 80 :: Int
largo = 40 :: Int
Expand All @@ -51,13 +54,13 @@ value (Pointed d) = value d * 1.5
chord :: Note -> Note -> Chord
chord n n' = Chord [n,n'] (max (duration n) (duration n'))

playChord :: Tempo -> Chord -> Wave
playChord tempo (Chord ns d) = slice (durationInSeconds tempo d) $ foldl1 (°) (map (interpret tempo) ns)
instance Playable Chord where
interpret tempo (Chord ns d) = slice (durationInSeconds tempo d) $ foldl1 (°) (map (interpret tempo) ns)

interpret :: Tempo -> Note -> Wave
interpret tempo (Note p o d) = slice t $ wave f
where
t = durationInSeconds tempo d
f = truncate (fromIntegral (frequency p) * (2 ** fromIntegral (o - 4)))
instance Playable Note where
interpret tempo (Note p o d) = slice t $ wave f
where
t = durationInSeconds tempo d
f = truncate (fromIntegral (frequency p) * (2 ** fromIntegral (o - 4)))

durationInSeconds tempo d = value d * 60.0 / fromIntegral tempo
4 changes: 1 addition & 3 deletions synthesizer.hs
Expand Up @@ -5,8 +5,6 @@ import SoundIO

simplenote p = Note p 4 Crotchet

note (p,o,d) = Note p o d

harrypotter = [(B,4,Crotchet),
(E,5,Pointed Crotchet),
(G,5,Quaver),
Expand All @@ -31,7 +29,7 @@ cMinor = Chord (map note [(C,4,Crotchet),
(G,4,Crotchet)]) Crotchet

main = do
outputSound $ (playChord 60 cMajor) ++ (playChord 60 cMinor)
outputSound $ (interpret 60 cMajor) ++ (interpret 60 cMinor)

-- main = do
-- B.putStr $ B.concat $ map (prepareSound.interpret 160.note) harrypotter
Expand Down

0 comments on commit 29f0499

Please sign in to comment.