spell-music / csound-expression

Haskell Framework for Electronic Music
320 stars 31 forks source link

RT MIDI instruments get silenced #93

Closed rvl closed 2 months ago

rvl commented 3 months ago

I'm trying to use a MIDI keyboard to play a Csound instrument through csound-expression, but the instrument never makes any noise.

This is the code I'm using:

module BugReport where

import Csound.Base hiding (dac)

myOpts :: Options
myOpts = setTrace <> setAlsaRtmidi <> setMidiDevice "hw:2,0,1"
  where
    setAlsaRtmidi = def { csdFlags = def { rtmidi = Just AlsaMidi } }

dac :: RenderCsd a => a -> IO ()
dac = dacBy myOpts

test1 = dac (osc 440)

test2 = dac (midin 1 $ onMsg osc)

If I run test1 from ghci, it produces a tone as expected.

If I run test2 from ghci, and then press keys on my MIDI keyboard, I can see that MIDI events are processed, but no tones are audible.

This is the output:

ghci> test2
UnifiedCSD:  /tmp/tmp.csd
Elapsed time at end of orchestra compile: real: 0.001s, CPU: 0.001s
sorting score ...
    ... done
Elapsed time at end of score sort: real: 0.001s, CPU: 0.001s
--Csound version 6.18 (double samples) Jan  1 1980
[commit: none]
libsndfile-1.2.2
displays suppressed
sr = 44100.0, kr = 689.062, ksmps = 64
0dBFS level = 1.0, A4 tuning = 440.0
chnl 1 using instr 19
ALSA: opened MIDI input device 'hw:2,0,1'
orch now loaded
audio buffered in 256 sample-frame blocks
ALSA output: total buffer size: 1024, period size: 256
writing 256 sample blks of 64-bit floats to dac
SECTION 1:
ftable 2:
new alloc for instr 18:
new alloc for instr 21:
new alloc for instr 22:
  rtevent:     T 12.835 TT 12.835 M:  0.00000
new MIDI alloc for instr 19:
  rtevent:     T 12.980 TT 12.980 M:  0.00000
  rtevent:     T 13.079 TT 13.079 M:  0.00000
  rtevent:     T 13.206 TT 13.206 M:  0.00000
  rtevent:     T 13.288 TT 13.288 M:  0.00000
  rtevent:     T 13.428 TT 13.428 M:  0.00000
^C
csound command: Interrupt
inactive allocs returned to freespace
end of score.          overall amps:  0.00000
       overall samples out of range:        0
0 errors in performance
Elapsed time at end of performance: real: 17.747s, CPU: 0.600s
ghci> 
csound command: Terminated

This is the contents of /tmp/tmp.csd:

<CsoundSynthesizer>

<CsOptions>

--nodisplays --output=dac -+rtmidi=alsa --midi-device=hw:2,0,1

</CsOptions>

<CsInstruments>

sr = 44100
ksmps = 64
nchnls = 1
0dbfs = 1.0
massign 1, 19
gargg0 init 0.0
gkrggBpmVar init 110.0
giPort init 1
opcode FreePort, i, 0
xout giPort
giPort = giPort + 1
endop

instr 22
endin

instr 21
event_i "i", 20, 604800.0, 1.0e-2
endin

instr 20
ir0 = 19
ir1 = 0.0
turnoff2 ir0, ir1, ir1
ir3 = 18
turnoff2 ir3, ir1, ir1
exitnow
endin

instr 19
ar0 = gargg0
ir1 active p1
if (ir1 < 2.0) then
    ir4 = 1.0
else
    ir2 = sqrt(ir1)
    ir3 = (1.0 / ir2)
    ir4 = ir3
endif
ir5 = ir4
ir6 ampmidi 1.0
kr0 = 1.0
ir8 cpsmidi
ar1 oscil3 kr0, ir8, 2
ar2 = (ir6 * ar1)
ar1 = (ir5 * ar2)
ar2 = (ar0 + ar1)
gargg0 = ar2
endin

instr 18
gargg0 = 0.0
arl0 init 0.0
ar1 = gargg0
ir3 = 1.0
ir4 = 0.0
ir5 = 90.0
ir6 = 100.0
ar2 upsamp k(ir3)
ar3 compress ar1, ar2, ir4, ir5, ir5, ir6, ir4, ir4, 0.0
ar1 = (ar3 * 0.8)
arl0 = ar1
ar1 = arl0
out ar1
endin

</CsInstruments>

<CsScore>

f2 0 8192 10 1.0

f0 604800.0

i 22 0.0 -1.0
i 21 0.0 -1.0
i 18 0.0 -1.0

</CsScore>

</CsoundSynthesizer>

If I edit /tmp/tmp.csd and move the line gargg0 = 0.0 to after the line ar1 = gargg0, then run csound /tmp/tmp.csd, then I can hear tones when I play on the keyboard.

The order of opcodes in /tmp/tmp.csd looks wrong to me.

I have the following software versions:

Thanks

anton-k commented 3 months ago

Thanks for report, I'll investigate that. I've fixed lots of bugs recently regarding low-level renderer. Maybe this one is also fixed.

rvl commented 3 months ago

Thanks @anton-k

anton-k commented 2 months ago

I've tried your example, it's a bug indeed. Recently I've dedicated lots of effort to debug the core underlying library csound-expression-dynamic. It renders haskell expression trees to imperative csound programs. I've found the bug in the way SE-expressions were handled, it rendered expressions in the wrong order. I was wrong in the original idea of implementing SE-monad. But now I've got a new algorithm. It's hopefully more sound one.

I've tried your example on the branch where I'm developing new core for CE. It seems to work. It's csound-core branch. I plan to merge it soon to master.

jwaldmann commented 2 months ago

NB: I am looking forward to csound-core.

anton-k commented 2 months ago

NB: I am looking forward to csound-core.

To test it I've implemented a non-trivial app that requires lots of SE-expressions: https://github.com/anton-k/csound-live-sampler. An example of new design and where CE is going. It's sort of sub-set of Abelton live clip launcher.

My plan for upcoming release is to integrate new ce-dynamic with bug fixes and then gradually rebuild CE on top of csound-core in the next releases.

anton-k commented 2 months ago

It's fixed on master. I've uploded new version to hackage also.

rvl commented 2 months ago

Thanks very much @anton-k. I'm building the new version and am about to try it out now.

rvl commented 2 months ago

It is working well now - thanks.