Data.hs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. {-# LANGUAGE TemplateHaskell #-}
  2. {-# LANGUAGE OverloadedLists #-}
  3. {-# LANGUAGE GADTs #-}
  4. module Hypsibius.Data where
  5. import Data.Adnot
  6. import Data.Sequence (Seq)
  7. import Data.Text (Text)
  8. import Data.Word (Word8)
  9. import Lens.Family2.TH
  10. -- | XXX: This is a temporary definition of 'Oscillator' for early
  11. -- prototyping purposes.
  12. data Oscillator
  13. = OscSine
  14. | OscSquare
  15. deriving (Eq, Show)
  16. -- | XXX: This is a temporary definition of 'Instrument' for early
  17. -- prototyping purposes.
  18. data Instrument = Instrument
  19. { _instrSource :: Oscillator
  20. } deriving (Eq, Show)
  21. $(makeLenses ''Instrument)
  22. -- | We'll maintain a list of instruments and refer to them using
  23. -- indices. For type safety, here is a wrapper around those
  24. -- indices.
  25. newtype InstrRef = InstrRef { _fromInstrRef :: Int }
  26. deriving (Eq, Show)
  27. $(makeLenses ''InstrRef)
  28. -- | A 'Note' here is an individual element of a scale, which we'll
  29. -- maintain a unique list of on a per-song basis, and most of the time
  30. -- we'll use indices into that list. A 'Note' has a frequency represented
  31. -- in cents and an appearance that the user will see when running the
  32. -- program, which should be no more than a few characters long.
  33. data Note = Note
  34. { _noteCents :: Double
  35. , _noteAppearance :: Text
  36. , _noteColor :: Maybe Text
  37. } deriving (Eq, Show)
  38. instance FromAdnot Note where
  39. parseAdnot = withSum "Note" go
  40. where go "note" [name, cents] =
  41. Note <$> parseAdnot cents <*> parseAdnot name <*> pure Nothing
  42. go "note" [name, cents, color] =
  43. Note <$> parseAdnot cents
  44. <*> parseAdnot name
  45. <*> (Just <$> parseAdnot color)
  46. go "note" _ = fail "Unknown argument structure"
  47. go c _ = fail ("Expected note, got " ++ show c)
  48. $(makeLenses ''Note)
  49. -- | We'll maintain a list of notes and refer to them using indices. For type
  50. -- safety, here is a wrapper around those indices.
  51. newtype NoteRef = NoteRef { _fromNoteRef :: Int }
  52. deriving (Eq, Show)
  53. $(makeLenses ''NoteRef)
  54. -- | A 'Scale' has a name, a total number of cents (which will almost always be
  55. -- 1200 for traditional scales) and a list of notes associated with it.
  56. data Scale = Scale
  57. { _scaleName :: Text
  58. , _scaleTotalCents :: Double
  59. , _scaleNotes :: Seq Note
  60. } deriving (Eq, Show)
  61. instance FromAdnot Scale where
  62. parseAdnot = withProduct "Scale" $ \o ->
  63. Scale <$> o .: "name"
  64. <*> o .: "size"
  65. <*> o .: "notes"
  66. $(makeLenses ''Scale)
  67. -- | An 'Event' is a typical event associated with a song.
  68. data Event = Event
  69. deriving (Eq, Show)
  70. data Beats
  71. = BeatsSimple Word8
  72. | BeatsAdditive [Word8]
  73. | BeatsFractional Word8 Word8
  74. deriving (Eq, Show)
  75. $(makeTraversals ''Beats)
  76. data Signature = Signature
  77. { _sigPerBar :: Beats
  78. , _sigBeatUnit :: Word8
  79. } deriving (Eq, Show)
  80. $(makeLenses ''Signature)
  81. data TrackChunk = TrackChunk
  82. { _tcSignature :: Signature
  83. } deriving (Eq, Show)
  84. data Track = Track
  85. {
  86. } deriving (Eq, Show)
  87. data Song = Song
  88. { _songScale :: Scale
  89. , _songTracks :: Seq Track
  90. } deriving (Eq, Show)
  91. $(makeLenses ''Song)