feat(vm): add dbgain unit, where gain is defined in decibels

Closes #78
This commit is contained in:
5684185+vsariola@users.noreply.github.com
2023-10-23 18:16:31 +03:00
parent f698986718
commit 04fbc9f6a7
12 changed files with 146 additions and 25 deletions

View File

@ -98,6 +98,41 @@ su_op_invgain_mono:
{{- end}}
{{end}}
{{- if .HasOp "dbgain"}}
;-------------------------------------------------------------------------------
; DBGAIN opcode: apply gain on the signal, with gain given in decibels
;-------------------------------------------------------------------------------
; Mono: x -> x*g, where g = 2**((2*d-1)*6.643856189774724) i.e. -40dB to 40dB, d=[0..1]
; Stereo: l r -> l*g r*g
;-------------------------------------------------------------------------------
{{.Func "su_op_dbgain" "Opcode"}}
{{- if .Stereo "dbgain"}}
fld dword [{{.Input "dbgain" "decibels"}}] ; d l r
{{- .Prepare (.Float 0.5)}}
fsub dword [{{.Use (.Float 0.5)}}] ; d-.5
fadd st0, st0 ; 2*d-1
{{- .Prepare (.Float 6.643856189774724)}}
fmul dword [{{.Use (.Float 6.643856189774724)}}] ; (2*d-1)*6.643856189774724
{{.Call "su_power"}}
{{- if .Mono "dbgain"}}
jnc su_op_dbgain_mono
{{- end}}
fmul st2, st0 ; g l r/g
su_op_dbgain_mono:
fmulp st1, st0 ; l/g (r/)
ret
{{- else}}
fld dword [{{.Input "dbgain" "decibels"}}] ; d l
{{- .Prepare (.Float 0.5)}}
fsub dword [{{.Use (.Float 0.5)}}] ; d-.5
fadd st0, st0 ; 2*d-1
{{- .Prepare (.Float 6.643856189774724)}}
fmul dword [{{.Use (.Float 6.643856189774724)}}] ; (2*d-1)*6.643856189774724
{{.Call "su_power"}}
fmulp st1, st0
ret
{{- end}}
{{end}}
{{- if .HasOp "filter"}}
;-------------------------------------------------------------------------------

View File

@ -98,6 +98,26 @@
)
{{end}}
{{- if .HasOp "dbgain"}}
;;-------------------------------------------------------------------------------
;; DBGAIN opcode: apply gain on the signal, with gain given in decibels
;;-------------------------------------------------------------------------------
;; Mono: x -> x*g, where g = 2**((2*d-1)*6.643856189774724) i.e. -40dB to 40dB, d=[0..1]
;; Stereo: l r -> l*g r*g
;;-------------------------------------------------------------------------------
(func $su_op_dbgain (param $stereo i32)
{{- if .Stereo "dbgain"}}
(call $stereoHelper (local.get $stereo) (i32.const {{div (.GetOp "dbgain") 2}}))
{{- end}}
(call $input (i32.const {{.InputNumber "dbgain" "decibels"}}))
(f32.sub (f32.const 0.5))
(f32.mul (f32.const 13.287712379549449))
(call $pow2)
(f32.mul (call $pop))
(call $push)
)
{{end}}
{{- if .HasOp "filter"}}
;;-------------------------------------------------------------------------------

View File

@ -346,6 +346,12 @@ func (s *GoSynth) Render(buffer sointu.AudioBuffer, maxtime int) (samples int, t
stack[l-2] /= params[0]
}
stack[l-1] /= params[0]
case opDbgain:
gain := float32(math.Pow(2, float64(params[0]*2-1)*6.643856189774724))
if stereo {
stack[l-2] *= gain
}
stack[l-1] *= gain
case opClip:
if stereo {
stack[l-2] = clip(stack[l-2])

View File

@ -8,30 +8,31 @@ const (
opClip = 4
opCompressor = 5
opCrush = 6
opDelay = 7
opDistort = 8
opEnvelope = 9
opFilter = 10
opGain = 11
opHold = 12
opIn = 13
opInvgain = 14
opLoadnote = 15
opLoadval = 16
opMul = 17
opMulp = 18
opNoise = 19
opOscillator = 20
opOut = 21
opOutaux = 22
opPan = 23
opPop = 24
opPush = 25
opReceive = 26
opSend = 27
opSpeed = 28
opSync = 29
opXch = 30
opDbgain = 7
opDelay = 8
opDistort = 9
opEnvelope = 10
opFilter = 11
opGain = 12
opHold = 13
opIn = 14
opInvgain = 15
opLoadnote = 16
opLoadval = 17
opMul = 18
opMulp = 19
opNoise = 20
opOscillator = 21
opOut = 22
opOutaux = 23
opPan = 24
opPop = 25
opPush = 26
opReceive = 27
opSend = 28
opSpeed = 29
opSync = 30
opXch = 31
)
var transformCounts = [...]int{0, 0, 1, 0, 5, 1, 4, 1, 5, 2, 1, 1, 0, 1, 0, 1, 0, 0, 2, 6, 1, 2, 1, 0, 0, 0, 1, 0, 0, 0}
var transformCounts = [...]int{0, 0, 1, 0, 5, 1, 1, 4, 1, 5, 2, 1, 1, 0, 1, 0, 1, 0, 0, 2, 6, 1, 2, 1, 0, 0, 0, 1, 0, 0, 0}