-- Copyright 2013 Evan Laforge
-- This program is distributed under the terms of the GNU General Public
-- License 3.0, see COPYING or http://www.gnu.org/licenses/gpl-3.0.txt

-- | Utilities for talams for Carnatic music.
module Cmd.Repl.LTala where
import qualified Cmd.Cmd as Cmd
import qualified Cmd.Edit as Edit
import qualified Cmd.ModifyEvents as ModifyEvents
import qualified Cmd.Repl.LEvent as LEvent
import qualified Cmd.Repl.LRuler as LRuler
import qualified Cmd.Ruler.Tala as Tala
import qualified Cmd.Selection as Selection

import qualified Ui.Events as Events
import qualified Ui.Meter.Meter as Meter


-- TODO broken:
-- takes 4 to 2 2/3, instead of 3
-- I get a fragment section, and following sections are at .99999

-- | Convert the selection from chatusram nadai to tisram nadai.
chatusram_to_tisram :: Cmd.CmdL ()
chatusram_to_tisram :: CmdL ()
chatusram_to_tisram = do
    (BlockId
block_id, [TrackNum]
_, [TrackId]
_, Range
range) <- forall (m :: * -> *).
M m =>
m (BlockId, [TrackNum], [TrackId], Range)
Selection.tracks
    let (Duration
start, Duration
end) = Range -> (Duration, Duration)
Events.range_times Range
range
    forall (m :: * -> *). M m => Track m -> m ()
ModifyEvents.selection forall a b. (a -> b) -> a -> b
$
        forall (m :: * -> *). Monad m => (Event -> Event) -> Track m
ModifyEvents.event forall a b. (a -> b) -> a -> b
$ Duration -> Duration -> Event -> Event
LEvent.stretch_event Duration
start (Duration
2forall a. Fractional a => a -> a -> a
/Duration
3)
    let dur :: Duration
dur = (Duration
end forall a. Num a => a -> a -> a
- Duration
start) forall a. Num a => a -> a -> a
* (Duration
2forall a. Fractional a => a -> a -> a
/Duration
3)
    forall (m :: * -> *). M m => m Modify -> m [RulerId]
LRuler.local forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). M m => (Meter -> Meter) -> m Modify
LRuler.modify_selected forall a b. (a -> b) -> a -> b
$
        Duration -> Duration -> [MSection] -> Meter -> Meter
LRuler.replace_range Duration
start (Duration
start forall a. Num a => a -> a -> a
+ Duration
dur) forall a b. (a -> b) -> a -> b
$
        Meter -> [MSection]
Meter.meter_sections (Tala -> TrackNum -> Duration -> Duration -> Meter
Tala.make_until Tala
Tala.adi_tala TrackNum
3 Duration
1 Duration
dur)
    -- Delete final 1/3.
    forall (m :: * -> *).
M m =>
BlockId -> Duration -> Duration -> m ()
Edit.delete_block_time BlockId
block_id (Duration
start forall a. Num a => a -> a -> a
+ Duration
dur) (Duration
end forall a. Num a => a -> a -> a
- (Duration
startforall a. Num a => a -> a -> a
+Duration
dur))

-- | Create adi tala in chatusram-tisram.  Assuming 1t per aksharam, there
-- is 3/4 per c-t aksharam.  So 8 of them fits in 6t, so each one is 6/8t.
chatis :: Tala.Avartanams -> Tala.Nadai -> Meter.Meter
chatis :: TrackNum -> TrackNum -> Meter
chatis TrackNum
avartanams TrackNum
nadai =
    -- TODO I think originally the 6/8 was akshara dur, not avartanam dur
    Tala -> TrackNum -> Duration -> TrackNum -> TrackNum -> Meter
Tala.make Tala
Tala.adi_tala TrackNum
nadai (Duration
6forall a. Fractional a => a -> a -> a
/Duration
8) TrackNum
avartanams TrackNum
1