Guest User

Untitled

a guest
Jun 21st, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. # This takes about 2s for a 53Mb file.
  2. bytes = audio_file.read_into(audio_buffer)
  3.  
  4. # This takes less than a second, BUT the data is interleaved
  5. # (left, right, left, right, left, right, etc)
  6. self.audio_data = audio_buffer.unpack('s*')
  7.  
  8. # Less than a second.
  9. self.left_channel = NArray.float(2, audio_data.size)
  10. self.right_channel = NArray.float(2, audio_data.size)
  11.  
  12. # This loop takes about 40-50 seconds. Rewriting it to eliminate
  13. # all division and multiplication shaves about 10s off that time.
  14. 0.upto(audio_data.size/2-1) do |i|
  15. time = i.to_f / audio_file.rate
  16. # First array stores time offset of sample
  17. left_channel[0, i] = time
  18. right_channel[0, i] = time
  19.  
  20. # Second array stores sampled data
  21. left_channel[1, i] = audio_data[i*2]
  22. right_channel[1, i] = audio_data[i*2+1]
  23. end
  24.  
  25. # BUT!
  26. #
  27. # If I could avoid the Ruby loop altogether, and let something
  28. # like NArray's constructors handle it, it's a 10x speedup. For
  29. # example:
  30. #
  31. # interleaved_data = NArray[self.audio_data]
  32. #
  33. # Takes less than 4 seconds! This is because we're not looping in
  34. # Ruby, but letting C handle the grindy stuff. However, the data
  35. # is interleaved, making it useless for processing rationally. :-/
  36. # I can separate it back out with another loop, but guess how long
  37. # that takes....
Add Comment
Please, Sign In to add comment