These are chat archives for opal/opal

30th
Dec 2018
Mitch VanDuyn
@catmando
Dec 30 2018 02:19
You can use eval (or compile) to change text into code. But why do you want to change code back into text?
Jose AƱasco
@merongivian
Dec 30 2018 09:42

So i'm building this code editor that plays music with code: http://negasonic.org, (similar to sonic pi, tidal cycles, etc) but it works with javascript (compiled with opal). I actually borrowed some of the code from http://opalrb.com/try/ for the text editor.

The idea is that every time i run the code it will reproduce sounds through the web audio api:

# code that generates music

# defining the the synth, effects
with_instrument(synth: :piano, volume: 3) do
   # the cycle is the one in charged of defining the notes
   # being played
   cycle do 
        # notes as numbers
        play 30, 40
   end
end

This is a custom DSL, so under the hood i use instance_eval to evaluate the block in with_instrument. Every time i execute the code for the instrument it will generate a bunch of web audio api objects called audio nodes that are really heavy for the browser, and can not be to destroyed completely after created, so i want to avoid creating them again if the code didn't changed.

Also, i think using instance_eval might be causing me performance issues (i'm using eval for the all compiling all the code, and then eval is used again in instance_eval).

My idea is to just store the code as text inside the instrument object used by with_instrument, and then somehow execute the code inside instance_evalonly if the code is different.

Was looking at BasicObject and seems that it retrieves the code inside the block as text as well, so i think i can create my own version of instance_eval.

Anyway if someone is interested in helping me, or has a better idea let me know :), this stuff is pretty difficult he