You are awesome guys! Really, I've learnt a lot from this topc! Thanks, many thanks!!!!
Yeah, your code looks brilliant Nubeat7.
It will suffer only one problems: the number of channels. When I make the path that will be draw, I need to decide if the wave is mono or stereo (because the draw will be different). WaveFile prim will first trigger the mem array prim, than the number of channels. So you have to create the drawing path when you get the number of channels, not the wave. Thus, the "trigger" to draw the new path must go to the Number of Channels connector. But when you re-load the schematic, it triggers only Mem connector (since M2F trigger once loaded), not the number of channels.
i.e. I cannot set two different triggers for the same task (one when I just reload schematic; one when I change to a new sample). Or at least: I don't know how to manage this situation.
This is my actual code (I prefer to stay inside Ruby; as you can see, Mono or Stereo will draw two different kind of Waveform, so I need to know this information before draw it. And this information come to me after Mem, due to how WaveFile output triggers once loaded a wavefile):
Code: Select all
def setParameters
@width,@height = getViewSize 0
@rescaling = 0.9
@factor_s = @n_channels == 2 ? 0.5 : 1
@sample_block_size = (@samples.length*@factor_s)/(@width*8)
@s = @width/(@samples.length*@factor_s)
@h = @height*0.5*@factor_s
@penPeaks = Pen.new Color.new(255,128,255,255),0.1
@penPeaks.setLineJoin "round"
@penSeparator = Pen.new Color.new(50,255,255,255),0.1
@brushBackground = Brush.new @color_background
@brushPeaks = Brush.new Color.new(255,128,255,255)
end
def getCoordinates
@path = GraphicsPath.new
# Stereo
if(@n_channels == 2)
# Left
peaksMax = []
peaksMin = []
step = 0
max = -1
min = 1
0.step(@samples.length-1, 2) do |i|
step += 1
max = @samples[i] > max ? @samples[i] : max
min = @samples[i] < min ? @samples[i] : min
if step >= @sample_block_size
peaksMax << [i*@factor_s*@s,(@h-max*@h*@rescaling)]
peaksMin << [i*@factor_s*@s,(@h-min*@h*@rescaling)]
step = 0
max = -1
min = 1
end
end
@path.addLines peaksMax
@path.addLines peaksMin.reverse!
@path.closeFigure
# Right
peaksMax = []
peaksMin = []
step = 0
max = -1
min = 1
1.step(@samples.length-1, 2) do |i|
step += 1
max = @samples[i] > max ? @samples[i] : max
min = @samples[i] < min ? @samples[i] : min
if step >= @sample_block_size
peaksMax << [(i-1)*@factor_s*@s,(@h-max*@h*@rescaling)+@h*2]
peaksMin << [(i-1)*@factor_s*@s,(@h-min*@h*@rescaling)+@h*2]
step = 0
max = -1
min = 1
end
end
@path.addLines peaksMax
@path.addLines peaksMin.reverse!
@path.closeFigure
# Mono
elsif(@n_channels == 1)
peaksMax = []
peaksMin = []
step = 0
max = -1
min = 1
0.step(@samples.length-1) do |i|
step += 1
max = @samples[i] > max ? @samples[i] : max
min = @samples[i] < min ? @samples[i] : min
if step >= @sample_block_size
peaksMax << [i*@s,(@h-max*@h*@rescaling)]
peaksMin << [i*@s,(@h-min*@h*@rescaling)]
step = 0
max = -1
min = 1
end
end
@path.addLines peaksMax
@path.addLines peaksMin.reverse!
@path.closeFigure
end
end
def draw i,v
# Background
v.drawRectangle @brushBackground,[0,0,@width,@height]
# Waveform
v.drawPath @path,@penPeaks
v.drawPath @path,@brushPeaks
# Separator
if(@n_channels == 2 || @n_channels == 0)
v.drawLine @penSeparator,[0,@height/2],[@width,@height/2]
end
end
def event i,v
if(i=="init")
setParameters
elsif(i=="n_channels")
setParameters
getCoordinates
redraw 0
end
end
Notice the trigger to draw is on "n_channels", which is not called once I load the schematic.
This will also introduce another problem: when you change the sample as VST.
Let say you load SampleA and save the schematic, than export as VST.
When you load, it first will load the wave inside the schematic, and that's ok. If now I change the sample (SampleB) and I store the path to the preset (for reload it next loading), when I reload it will load first the wave inside the schematic, than the sample stored in preset:

- Immagine.png (12.09 KiB) Viewed 18198 times
If you look at Trigger counter when you load the VST first time (no data in preset), you will see count 1 (the wave stored on schematic when you export as VST).
If you now change the sample (which will store the path to Preset String), save the preset and reload, count is 2 (it first load schematic one, than from VST).
Two times! And this makes trouble, since there are two scenario, where in one you don't have informations about channels, and one with trigger twice and send wave + n of channels.
Is it clear what I mean?