module Sound.Sc3.Server.Recorder where
import Sound.Osc.Datum
import Sound.Osc.Packet
import Sound.Sc3.Common.Rate
import Sound.Sc3.Ugen.Bindings
import Sound.Sc3.Ugen.Util
import Sound.Sc3.Server.Command
import Sound.Sc3.Server.Enum
import Sound.Sc3.Server.Nrt
import Sound.Sc3.Server.Synthdef
data SC3_Recorder = SC3_Recorder
{ SC3_Recorder -> SoundFileFormat
rec_sftype :: SoundFileFormat
, SC3_Recorder -> SampleFormat
rec_coding :: SampleFormat
, SC3_Recorder -> FilePath
rec_fname :: FilePath
, SC3_Recorder -> Int
rec_nc :: Int
, SC3_Recorder -> Int
rec_bus :: Int
, SC3_Recorder -> Int
rec_buf_id :: Int
, SC3_Recorder -> Int
rec_buf_frames :: Int
, SC3_Recorder -> Int
rec_node_id :: Int
, SC3_Recorder -> Int
rec_group_id :: Int
, SC3_Recorder -> Maybe Double
rec_dur :: Maybe Time
}
default_SC3_Recorder :: SC3_Recorder
default_SC3_Recorder :: SC3_Recorder
default_SC3_Recorder =
SC3_Recorder
{ rec_sftype :: SoundFileFormat
rec_sftype = SoundFileFormat
Wave
, rec_coding :: SampleFormat
rec_coding = SampleFormat
PcmFloat
, rec_fname :: FilePath
rec_fname = FilePath
"/tmp/sc3-recorder.wav"
, rec_nc :: Int
rec_nc = Int
2
, rec_bus :: Int
rec_bus = Int
0
, rec_buf_id :: Int
rec_buf_id = Int
10
, rec_buf_frames :: Int
rec_buf_frames = Int
65536
, rec_node_id :: Int
rec_node_id = Int
2001
, rec_group_id :: Int
rec_group_id = Int
0
, rec_dur :: Maybe Double
rec_dur = Double -> Maybe Double
forall a. a -> Maybe a
Just Double
60
}
rec_synthdef_nm :: Int -> String
rec_synthdef_nm :: Int -> FilePath
rec_synthdef_nm Int
nc = FilePath
"sc3-recorder-" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Int -> FilePath
forall a. Show a => a -> FilePath
show Int
nc
rec_synthdef :: Int -> Synthdef
rec_synthdef :: Int -> Synthdef
rec_synthdef Int
nc =
let bufnum :: Ugen
bufnum = Rate -> FilePath -> Double -> Ugen
control Rate
kr FilePath
"bufnum" Double
0
bus :: Ugen
bus = Rate -> FilePath -> Double -> Ugen
control Rate
kr FilePath
"bus" Double
0
in FilePath -> Ugen -> Synthdef
synthdef (Int -> FilePath
rec_synthdef_nm Int
nc) (Ugen -> Ugen -> Ugen
diskOut Ugen
bufnum (Int -> Rate -> Ugen -> Ugen
in' Int
nc Rate
ar Ugen
bus))
rec_init_m :: SC3_Recorder -> [Message]
rec_init_m :: SC3_Recorder -> [Message]
rec_init_m SC3_Recorder
r =
let buf :: Int
buf = SC3_Recorder -> Int
rec_buf_id SC3_Recorder
r
in [ Synthdef -> Message
d_recv (Int -> Synthdef
rec_synthdef (SC3_Recorder -> Int
rec_nc SC3_Recorder
r))
, Int -> Int -> Int -> Message
b_alloc Int
buf (SC3_Recorder -> Int
rec_buf_frames SC3_Recorder
r) (SC3_Recorder -> Int
rec_nc SC3_Recorder
r)
, Int
-> FilePath
-> SoundFileFormat
-> SampleFormat
-> Int
-> Int
-> Buffer_Leave_File_Open
-> Message
b_write Int
buf (SC3_Recorder -> FilePath
rec_fname SC3_Recorder
r) (SC3_Recorder -> SoundFileFormat
rec_sftype SC3_Recorder
r) (SC3_Recorder -> SampleFormat
rec_coding SC3_Recorder
r) (-Int
1) Int
0 Buffer_Leave_File_Open
True
]
rec_begin_m :: SC3_Recorder -> Message
rec_begin_m :: SC3_Recorder -> Message
rec_begin_m SC3_Recorder
r =
FilePath
-> Int -> AddAction -> Int -> [(FilePath, Double)] -> Message
s_new
(Int -> FilePath
rec_synthdef_nm (SC3_Recorder -> Int
rec_nc SC3_Recorder
r))
(SC3_Recorder -> Int
rec_node_id SC3_Recorder
r)
AddAction
AddToTail
(SC3_Recorder -> Int
rec_group_id SC3_Recorder
r)
[ (FilePath
"bus", Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (SC3_Recorder -> Int
rec_bus SC3_Recorder
r))
, (FilePath
"bufnum", Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (SC3_Recorder -> Int
rec_buf_id SC3_Recorder
r))
]
rec_end_m :: SC3_Recorder -> [Message]
rec_end_m :: SC3_Recorder -> [Message]
rec_end_m SC3_Recorder
r =
[ [Int] -> Message
n_free [SC3_Recorder -> Int
rec_node_id SC3_Recorder
r]
, Int -> Message
b_close (SC3_Recorder -> Int
rec_buf_id SC3_Recorder
r)
, Int -> Message
b_free (SC3_Recorder -> Int
rec_buf_id SC3_Recorder
r)
]
sc3_recorder :: SC3_Recorder -> Nrt
sc3_recorder :: SC3_Recorder -> Nrt
sc3_recorder SC3_Recorder
r =
let b0 :: BundleOf Message
b0 = Double -> [Message] -> BundleOf Message
forall t. Double -> [t] -> BundleOf t
bundle Double
0 (SC3_Recorder -> [Message]
rec_init_m SC3_Recorder
r [Message] -> [Message] -> [Message]
forall a. [a] -> [a] -> [a]
++ [SC3_Recorder -> Message
rec_begin_m SC3_Recorder
r])
in case SC3_Recorder -> Maybe Double
rec_dur SC3_Recorder
r of
Maybe Double
Nothing -> [BundleOf Message] -> Nrt
Nrt [BundleOf Message
b0]
Just Double
d -> [BundleOf Message] -> Nrt
Nrt [BundleOf Message
b0, Double -> [Message] -> BundleOf Message
forall t. Double -> [t] -> BundleOf t
bundle Double
d (SC3_Recorder -> [Message]
rec_end_m SC3_Recorder
r)]