setFrames.lua 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. return {
  2. summary = 'Write frames to the Sound.',
  3. description = 'Writes frames to the Sound.',
  4. arguments = {
  5. t = {
  6. type = 'table',
  7. description = 'A table containing frames to write.'
  8. },
  9. blob = {
  10. type = 'Blob',
  11. description = 'A Blob containing frames to write.'
  12. },
  13. sound = {
  14. type = 'Sound',
  15. description = 'Another Sound to copy frames from.'
  16. },
  17. count = {
  18. type = 'number',
  19. default = 'nil',
  20. description = 'How many frames to write. If nil, writes as many as possible.'
  21. },
  22. dstOffset = {
  23. type = 'number',
  24. default = '0',
  25. description = 'A frame offset to apply when writing the frames.'
  26. },
  27. srcOffset = {
  28. type = 'number',
  29. default = '0',
  30. description = 'A frame, byte, or index offset to apply when reading frames from the source.'
  31. }
  32. },
  33. returns = {
  34. count = {
  35. type = 'number',
  36. description = 'The number of frames written.'
  37. }
  38. },
  39. variants = {
  40. {
  41. arguments = { 't', 'count', 'dstOffset', 'srcOffset' },
  42. returns = { 'count' }
  43. },
  44. {
  45. arguments = { 'blob', 'count', 'dstOffset', 'srcOffset' },
  46. returns = { 'count' }
  47. },
  48. {
  49. arguments = { 'sound', 'count', 'dstOffset', 'srcOffset' },
  50. returns = { 'count' }
  51. }
  52. },
  53. example = {
  54. description = 'Generate a sine wave.',
  55. code = [[
  56. function lovr.load()
  57. local length = 1
  58. local rate = 48000
  59. local frames = length * rate
  60. local frequency = 440
  61. local volume = 1.0
  62. sound = lovr.data.newSound(frames, 'f32', 'stereo', rate)
  63. local data = {}
  64. for i = 1, frames do
  65. local amplitude = math.sin((i - 1) * frequency / rate * (2 * math.pi)) * volume
  66. data[2 * i - 1] = amplitude
  67. data[2 * i - 0] = amplitude
  68. end
  69. sound:setFrames(data)
  70. source = lovr.audio.newSource(sound)
  71. source:setLooping(true)
  72. source:play()
  73. end
  74. ]]
  75. }
  76. }