Dimencia

Untitled

Jan 10th, 2023
1,278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.68 KB | None | 0 0
  1. function addNote(instrument, index, data, pitch, velocity)
  2.     local numNotes = 48
  3.    
  4.     local originalNumData = #data
  5.     -- Only runs if index is bigger than #data, and populates 0's until index
  6.     -- note that index is 0-based and inclusive
  7.     for i=originalNumData+1,index-1,1 do
  8.         data[i] = 0
  9.     end
  10.    
  11.     local instrumentData = pcmSets[instrument]
  12.     local numInstrumentData = #instrumentData
  13.    
  14.     -- Early pitching attempt, hopefully becomes FFT later
  15.    
  16.     local pitchMult = math.pow(2,((numNotes/2)-pitch)/12)
  17.     -- Gives us 0.25 to 4 for 48 notes, or 1/4 to 4x freq
  18.    
  19.     local curDataIndex = 1
  20.     for i=1, numInstrumentData, 1 do
  21.     -- This handles both duplicating notes and interpolating
  22.         while curDataIndex <= i*pitchMult do
  23.             local curData = data[index+curDataIndex] or 0
  24.            
  25.             local newData
  26.             if pitchMult < 1 then
  27.                 newData = InterpolateHermite(instrumentData[i-1] or 0, instrumentData[i] or 0, instrumentData[i+1] or 0, instrumentData[i+2] or 0, (i*pitchMult)-curDataIndex) * (velocity/128)
  28.             else -- No interpolation when duplicating?
  29.                 newData = instrumentData[i]*(velocity/128)
  30.             end
  31.                        
  32.             -- Next we try a thing to mix them well
  33.             local m
  34.             -- Make them unsigned
  35.             local newDataU = newData + 128
  36.             local curDataU = curData + 128
  37.             if newDataU < 128 or curDataU < 128 then
  38.                 m = newDataU * curDataU / 128
  39.             else
  40.                 m = 2 * (newDataU + curDataU) - (newDataU * curDataU) / 128 - 256
  41.             end
  42.             -- Sign it...
  43.             if m == 256 then m = 255 end
  44.            
  45.             m = (m - 128)*0.9 -- Keep volume from oversaturating...
  46.            
  47.             data[index+curDataIndex] = m
  48.            
  49.             curDataIndex = curDataIndex + 1
  50.         end
  51.         -- then keep duplicating it if it's still positive
  52.        
  53.     end
  54.     return data
  55. end
Advertisement
Add Comment
Please, Sign In to add comment