Ruby sort method on [x1,y1] [x2,y2] ... [xn,yn] data
Posted: Sun Feb 02, 2020 8:12 pm
How to take advantage of Ruby sort methods applied on simple data (actually non-hash data) consisting of a value table organized this way : [x1,y1] [x2,y2] ... [xn,yn] ?
In the application, the [x,y] table gets maintained, storing the various frequencies (Hz) and attenuation (dB) the user asked to play. Each time the user requests a new frequency/attenuation couple, such couple gets played as audio (blue schematic) by Flowstone, such table gets updated by a << [x,y] Ruby append instruction, and then, comes the difficulty I am facing : a derivative of such table needs to get generated, this time sorted by frequency (lowest frequency first). The sorting algorithm is not allowed to consume a lot of CPU%, as I don't want each frequency/attenuation change, to cause some audio glitch or DS or ASIO buffer malfunction.
I am sorry, I miserably failed in converting the [x,y] array into a hash, then sorting the hash, then converting the hash back into a [x,y] array. As consequence, I am currently relying on the following awful "hand-made" code :
Your kind help will allow people like me to quickly take advantage of the elegant built-in Ruby arrays and hash manipulating instructions. It will be funny, comparing my "by hand-made" code, with the few elegant Ruby instructions that are actually required. I am curious to discover the CPU% advantage that the native Ruby sorting instructions can bring.
In the application, the [x,y] table gets maintained, storing the various frequencies (Hz) and attenuation (dB) the user asked to play. Each time the user requests a new frequency/attenuation couple, such couple gets played as audio (blue schematic) by Flowstone, such table gets updated by a << [x,y] Ruby append instruction, and then, comes the difficulty I am facing : a derivative of such table needs to get generated, this time sorted by frequency (lowest frequency first). The sorting algorithm is not allowed to consume a lot of CPU%, as I don't want each frequency/attenuation change, to cause some audio glitch or DS or ASIO buffer malfunction.
I am sorry, I miserably failed in converting the [x,y] array into a hash, then sorting the hash, then converting the hash back into a [x,y] array. As consequence, I am currently relying on the following awful "hand-made" code :
Code: Select all
# ############### generate @sortFLarray, basing on @FLarray data
# generate @tmparray, for it to contain rounded @F values along with untouched @dB values
# the rounding is required by the stupid sorting loop (NEED TO IMPROVE THIS)
@FL_len = @FLarray.length
@tmparray = []
n=0 # FLarray index
while n <= @FL_len-1
@F = (@FLarray[n][0]).round
@dB = (@FLarray[n][1])
@tmparray << [@F,@dB]
n=n+1
end # while n
@sortFLarray=[]
@sortFLarray=@tmparray # @sortFLarray now contains integer frequencies
#generate @tmparray, for it to contain data, sorted by @F
@tmparray=[]
@FL_len = @sortFLarray.length
n=0 # @sortFLarray index
val = 0
while (val <=24000) # this is a stupid load for the computer
while n <= @FL_len-1
@F = @sortFLarray[n][0]
if @F == val then
@dB = @sortFLarray[n][1]
@tmparray << [@F,@dB] # append data to @tmparray
end #if
n=n+1
end # while n
val=val+1
n=0
end # while val
@sortFLarray=[]
@sortFLarray=@tmparray
# ############### Your kind help will allow people like me to quickly take advantage of the elegant built-in Ruby arrays and hash manipulating instructions. It will be funny, comparing my "by hand-made" code, with the few elegant Ruby instructions that are actually required. I am curious to discover the CPU% advantage that the native Ruby sorting instructions can bring.