Skip to content

Commit

Permalink
Merge pull request #1 from tanyabouman/master
Browse files Browse the repository at this point in the history
Improved Documentation for Chebyshev.hs and Clarinet.hs.
  • Loading branch information
anton-k authored Mar 9, 2017
2 parents 2069fcd + 906f3de commit ab49102
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
33 changes: 30 additions & 3 deletions 40-waveshaping/Chebyshev.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,48 @@ module Chebyshev where

import Csound

-- | Takes an amplitude (between 0 and 1), pitch (as 8ve.pc. i.e. 8.00 is middle C, 8.01 is D) and table describing the sound and makes a signal matching Chebyshev coefficients.
instr :: (D, D, Tab) -> Sig
instr (amp, pch, tf) = a1 * env
where a1 = tablei (4096 * osc (sig $ cpspch pch)) tf
instr (amp, pch, tf) = a1 * env -- put the envelope and amplitude together
where
-- make a signal matching the pitch
a1 :: Sig
a1 = tablei (4096 * osc (sig $ cpspch pch)) tf
-- a sound envelope based on the amplitde (amp) and duration (idur)
env :: Sig
env = linen (sig amp) 0.085 idur 0.04

-- | Takes a list of Chebyshev coefficients and produces a table to generate a signal.
chebs :: [Double] -> Tab
chebs = setSize 8193 . chebs1 4096 1

-- | Produces a table for Chebyshev f88.
f88 :: Tab
f88 = chebs [1, 1]
-- | Produces a table for Chebyshev f89.
f89 :: Tab
f89 = chebs [1, 1, 1, 2]
f90 = chebs [1, 1, 0, 0, 0, 0, 0, 6, 5, 4]
-- | Produces a table for Chebyshev f90.
f90 :: Tab
f90 = chebs [1, 1, 0, 0, 0, 0, 0, 6, 5, 4]


-- | Takes a table, duration and pitch and makes a track with that one note.
note :: Tab -> D -> D -> Sco (D, D, Tab)
note tab dur pch = dur *| temp (0.5, pch, tab)

-- | Takes a duration and a pitch and makes a track with that one note with the Chebyshev f88.
i1 :: D -> D -> Sco (D, D, Tab)
i1 = note f88
-- | Takes a duration and a pitch and makes a track with that one note with the Chebyshev f89.
i2 :: D -> D -> Sco (D, D, Tab)
i2 = note f89
-- | Takes a duration and a pitch and makes a track with that one note with the Chebyshev f90.
i3 :: D -> D -> Sco (D, D, Tab)
i3 = note f90

-- | A sample list of notes, played by the Chebyshev instrument, to be rendered by the main function.
res :: Sco (Mix a)
res = sco instr $ line [
i1 0.750 7.04,
i1 0.250 7.07,
Expand All @@ -56,5 +81,7 @@ res = sco instr $ line [
i3 0.125 9.04,
i3 0.125 9.05]

-- | Takes the res sample above and renders it to the soundcard in real time.
main :: IO ()
main = dac $ runMix res

20 changes: 17 additions & 3 deletions 40-waveshaping/Clarinet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,26 @@ module Clarinet where

import Csound

-- | Takes an amplitude (between 0 and 1) and pitch (as 8ve.pc. i.e. 8.00 is middle C, 8.01 is D) and makes a signal to emulate a clarinet.
instr :: (D, D) -> Sig
instr (amp, pch) = sig amp * tablei (256 + a1) (setSize 512 $ lins [-1, 200, -0.5, 112, 0.5, 200, 1])
where env = linen 255 0.085 idur dec
instr (amp, pch) = sig amp * tablei (256 + a1) (setSize 512 $ lins [-1, 200, -0.5, 112, 0.5, 200, 1]) -- takes the basic sound envelope defined below, and puts it as a clarinet sound
where
-- a sound envelope based on the duration (idur) and the right-hand envelope
env :: Sig
env = linen 255 0.085 idur dec
-- a right-hand envelope (decrescendo) if the duration is longer than 0.75
dec :: D
dec = ifB (idur >* 0.75) 0.64 (idur - 0.085)
a1 = env * osc (sig $ cpspch pch)
-- takes the envelope signal defined above puts it at the right pitch
a1 :: Sig
a1 = env * osc (sig $ cpspch pch)

-- | Takes a duration and a pitch and makes a track with that one note.
i1 :: D -> D -> Sco (D, D)
i1 dur pch = dur *| temp (0.5, pch)

-- | A sample list of notes, played by the clarinet instrument, to be rendered by the main function.
res :: Sco (Mix a)
res = sco instr $ line [
i1 0.750 7.04,
i1 0.250 7.07,
Expand All @@ -48,4 +60,6 @@ res = sco instr $ line [
i1 0.125 9.04,
i1 0.125 9.05]

-- | Takes the res sample above and renders it to the soundcard in real time.
main :: IO ()
main = dac $ runMix res

0 comments on commit ab49102

Please sign in to comment.