Csound Csound-dev Csound-tekno Search About

[Csnd] Check if ftable is in use

Date2020-07-18 18:03
FromSyl Morrison
Subject[Csnd] Check if ftable is in use
Hi all, I'm using FTFree to deallocate ftables on an external event (dropdown callback in Unity for example), but when switching tables, obviously I get horrific glitches if its still being read from. Does anyone have any ideas when it comes to waiting until the end of a note to deallocate the ftable? (I'd rather not use temporary ones, as I've written a sampler based around the SFZ format, so ideally all the associated samples stay in ram until the caller requests a change in SFZ! 
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Date2020-07-18 19:06
FromJana Hübenthal
SubjectRe: [Csnd] Check if ftable is in use


Hi Syl,

there are two approaches which come into my mind spontaneously:

[1] Keep a global reference counter for any of your tables. Anytime an instrument using your table starts, increment it. Anytime an instrument using your table stops, decrement it. If the reference counter gets zeo, there are no more users and you can deallocate it. That's the usual way "garbage collection" works. Maybe a bit tricky to implement, especially to detect the "real" end of an instrument. There was a discussion about that just a few weeks ago. Can't remenber the exact topic, but is was how to get the very last k-cycle of an instrument.


[2] The way I would prefer, would be a timeout approach. Again, you need a global variable for each table to be supervised. So, define it in global instrument space, like 

gktable42used init 0

In any instrument using the table in question, set this variable to a value above zero, for example 1000: 

instr 123
  gktable42used = 1000   ; the running istrument keeps the global value sticking at 1000
  ...  ; your instrument code goes here
endin 


Then you need a supervisor instrument, continuously running:

instr 9999
  if gktable42used > 0 then   ; anyone who claims to use that table?
    gktable42used -= 1
    if gktable42used == 0 then  ; the counter hasn't been reset by any instrument for a longer time now, so we finally got down to zero
      FTfree ....   ; yeah, we don't need that anymore! Noone uses it...
    endif
  endif
endin

The nice thing with this: counting down that "in use"-counter leads to a delay before actually freeing the table. If any instrument decides to reuse the table during that period, it is still in memory an does not get deallocated, as the counting down is cancelled immediately.

Beware, you need to implement this for any table you use, which could be a pain!

This is not real, tested code! Just take my ideas an a pointer for further studies...

Best,
Jana


Am 18.07.20 um 19:03 schrieb Syl Morrison:
Hi all, I'm using FTFree to deallocate ftables on an external event (dropdown callback in Unity for example), but when switching tables, obviously I get horrific glitches if its still being read from. Does anyone have any ideas when it comes to waiting until the end of a note to deallocate the ftable? (I'd rather not use temporary ones, as I've written a sampler based around the SFZ format, so ideally all the associated samples stay in ram until the caller requests a change in SFZ! 
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Date2020-07-18 19:09
FromSyl Morrison
SubjectRe: [Csnd] Check if ftable is in use
The second approach sounds really handy, will try that, what I just tried was copying to a temp ftable on note start but still sounding pretty bad, thanks so much! Using the above approach but in opcode form could actually be quite useful as well (especially seeing as when a new ftable gets allocated I'm holding a handle for it in c++ already!

On Sat, Jul 18, 2020 at 7:06 PM Jana Hübenthal <jana@ebene11.de> wrote:


Hi Syl,

there are two approaches which come into my mind spontaneously:

[1] Keep a global reference counter for any of your tables. Anytime an instrument using your table starts, increment it. Anytime an instrument using your table stops, decrement it. If the reference counter gets zeo, there are no more users and you can deallocate it. That's the usual way "garbage collection" works. Maybe a bit tricky to implement, especially to detect the "real" end of an instrument. There was a discussion about that just a few weeks ago. Can't remenber the exact topic, but is was how to get the very last k-cycle of an instrument.


[2] The way I would prefer, would be a timeout approach. Again, you need a global variable for each table to be supervised. So, define it in global instrument space, like 

gktable42used init 0

In any instrument using the table in question, set this variable to a value above zero, for example 1000: 

instr 123
  gktable42used = 1000   ; the running istrument keeps the global value sticking at 1000
  ...  ; your instrument code goes here
endin 


Then you need a supervisor instrument, continuously running:

instr 9999
  if gktable42used > 0 then   ; anyone who claims to use that table?
    gktable42used -= 1
    if gktable42used == 0 then  ; the counter hasn't been reset by any instrument for a longer time now, so we finally got down to zero
      FTfree ....   ; yeah, we don't need that anymore! Noone uses it...
    endif
  endif
endin

The nice thing with this: counting down that "in use"-counter leads to a delay before actually freeing the table. If any instrument decides to reuse the table during that period, it is still in memory an does not get deallocated, as the counting down is cancelled immediately.

Beware, you need to implement this for any table you use, which could be a pain!

This is not real, tested code! Just take my ideas an a pointer for further studies...

Best,
Jana


Am 18.07.20 um 19:03 schrieb Syl Morrison:
Hi all, I'm using FTFree to deallocate ftables on an external event (dropdown callback in Unity for example), but when switching tables, obviously I get horrific glitches if its still being read from. Does anyone have any ideas when it comes to waiting until the end of a note to deallocate the ftable? (I'd rather not use temporary ones, as I've written a sampler based around the SFZ format, so ideally all the associated samples stay in ram until the caller requests a change in SFZ! 
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here