audio.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. -- love.audio
  2. --------------------------------------------------------------------------------
  3. --------------------------------------------------------------------------------
  4. ------------------------------------OBJECTS-------------------------------------
  5. --------------------------------------------------------------------------------
  6. --------------------------------------------------------------------------------
  7. -- RecordingDevice (love.audio.getRecordingDevices)
  8. love.test.audio.RecordingDevice = function(test)
  9. -- check devices first
  10. local devices = love.audio.getRecordingDevices()
  11. if #devices == 0 then
  12. return test:skipTest('cant test this works: no recording devices found')
  13. end
  14. -- check object created and basics
  15. local device = devices[1]
  16. test:assertObject(device)
  17. test:assertMatch({1, 2}, device:getChannelCount(), 'check channel count is 1 or 2')
  18. test:assertNotEquals(nil, device:getName(), 'check has name')
  19. -- check initial data is empty as we haven't recorded anything yet
  20. test:assertNotNil(device:getBitDepth())
  21. test:assertEquals(nil, device:getData(), 'check initial data empty')
  22. test:assertEquals(0, device:getSampleCount(), 'check initial sample empty')
  23. test:assertNotNil(device:getSampleRate())
  24. test:assertEquals(false, device:isRecording(), 'check not recording')
  25. -- start recording for a short time
  26. -- @TODO needs delay for VMs
  27. local startrecording = device:start(32000, 4000, 16, 1)
  28. test:waitFrames(10)
  29. test:assertEquals(true, startrecording, 'check recording started')
  30. test:assertEquals(true, device:isRecording(), 'check now recording')
  31. test:assertEquals(4000, device:getSampleRate(), 'check sample rate set')
  32. test:assertEquals(16, device:getBitDepth(), 'check bit depth set')
  33. test:assertEquals(1, device:getChannelCount(), 'check channel count set')
  34. local recording = device:stop()
  35. test:waitFrames(10)
  36. -- after recording
  37. test:assertEquals(false, device:isRecording(), 'check not recording')
  38. test:assertEquals(nil, device:getData(), 'using stop should clear buffer')
  39. test:assertObject(recording)
  40. end
  41. -- Source (love.audio.newSource)
  42. love.test.audio.Source = function(test)
  43. -- create stereo source
  44. local stereo = love.audio.newSource('resources/click.ogg', 'static')
  45. test:assertObject(stereo)
  46. -- check stereo props
  47. test:assertEquals(2, stereo:getChannelCount(), 'check stereo src')
  48. test:assertEquals(66, math.floor(stereo:getDuration("seconds")*1000), 'check stereo seconds')
  49. test:assertNotNil(stereo:getFreeBufferCount())
  50. test:assertEquals('static', stereo:getType(), 'check stereo type')
  51. -- check cloning a stereo
  52. local clone = stereo:clone()
  53. test:assertEquals(2, clone:getChannelCount(), 'check clone stereo src')
  54. test:assertEquals(66, math.floor(clone:getDuration("seconds")*1000), 'check clone stereo seconds')
  55. test:assertNotNil(clone:getFreeBufferCount())
  56. test:assertEquals('static', clone:getType(), 'check cloned stereo type')
  57. -- mess with stereo playing
  58. test:assertEquals(false, stereo:isPlaying(), 'check not playing')
  59. stereo:setLooping(true)
  60. stereo:play()
  61. test:assertEquals(true, stereo:isPlaying(), 'check now playing')
  62. test:assertEquals(true, stereo:isLooping(), 'check now playing')
  63. stereo:pause()
  64. stereo:seek(0.01, 'seconds')
  65. test:assertEquals(0.01, stereo:tell('seconds'), 'check seek/tell')
  66. stereo:stop()
  67. test:assertEquals(false, stereo:isPlaying(), 'check stopped playing')
  68. -- check volume limits
  69. stereo:setVolumeLimits(0.1, 0.5)
  70. local min, max = stereo:getVolumeLimits()
  71. test:assertEquals(1, math.floor(min*10), 'check min limit')
  72. test:assertEquals(5, math.floor(max*10), 'check max limit')
  73. -- @NOTE the following works as setVolumeLimits is used with set volume
  74. -- as the BASE and then applying directional, rather than being a clamp
  75. stereo:setVolume(1)
  76. test:assertEquals(1, stereo:getVolume(), 'check set volume')
  77. stereo:setVolume(0)
  78. test:assertEquals(0, stereo:getVolume(), 'check set volume')
  79. -- change some get/set props that can apply to stereo
  80. stereo:setPitch(2)
  81. test:assertEquals(2, stereo:getPitch(), 'check pitch change')
  82. -- create mono source
  83. local mono = love.audio.newSource('resources/clickmono.ogg', 'stream')
  84. test:assertObject(mono)
  85. test:assertEquals(1, mono:getChannelCount(), 'check mono src')
  86. test:assertEquals(2927, mono:getDuration("samples"), 'check mono seconds')
  87. test:assertEquals('stream', mono:getType(), 'check mono type')
  88. -- check the basic get/set properties
  89. test:assertEquals(0, mono:getAirAbsorption(), 'get air absorption')
  90. mono:setAirAbsorption(1)
  91. test:assertEquals(1, mono:getAirAbsorption(), 'set air absorption')
  92. mono:setCone(0, 90*(math.pi/180), 1)
  93. local ia, oa, ov = mono:getCone()
  94. test:assertEquals(0, ia, 'check cone ia')
  95. test:assertEquals(math.floor(9000*(math.pi/180)), math.floor(oa*100), 'check cone oa')
  96. test:assertEquals(1, ov, 'check cone ov')
  97. mono:setDirection(3, 1, -1)
  98. local x, y, z = mono:getDirection()
  99. test:assertEquals(3, x, 'check direction x')
  100. test:assertEquals(1, y, 'check direction y')
  101. test:assertEquals(-1, z, 'check direction z')
  102. mono:setRelative(true)
  103. test:assertEquals(true, mono:isRelative(), 'check set relative')
  104. mono:setPosition(1, 2, 3)
  105. x, y, z = mono:getPosition()
  106. test:assertEquals(x, 1, 'check pos x')
  107. test:assertEquals(y, 2, 'check pos y')
  108. test:assertEquals(z, 3, 'check pos z')
  109. mono:setVelocity(1, 3, 4)
  110. x, y, z = mono:getVelocity()
  111. test:assertEquals(x, 1, 'check velocity x')
  112. test:assertEquals(y, 3, 'check velocity x')
  113. test:assertEquals(z, 4, 'check velocity x')
  114. mono:setRolloff(1)
  115. test:assertEquals(1, mono:getRolloff(), 'check rolloff set')
  116. -- create queue source
  117. local queue = love.audio.newQueueableSource(44100, 16, 1, 3)
  118. local sdata = love.sound.newSoundData(1024, 44100, 16, 1)
  119. test:assertObject(queue)
  120. local run = queue:queue(sdata)
  121. test:assertEquals(true, run, 'check queued sound')
  122. queue:stop()
  123. -- check making a filer
  124. local setfilter = stereo:setFilter({
  125. type = 'lowpass',
  126. volume = 0.5,
  127. highgain = 0.3
  128. })
  129. test:assertEquals(true, setfilter, 'check filter applied')
  130. local filter = stereo:getFilter()
  131. test:assertEquals('lowpass', filter.type, 'check filter type')
  132. test:assertEquals(0.5, filter.volume, 'check filter volume')
  133. test:assertEquals(3, math.floor(filter.highgain*10), 'check filter highgain')
  134. test:assertEquals(nil, filter.lowgain, 'check filter lowgain')
  135. -- add an effect
  136. local effsource = love.audio.newSource('resources/click.ogg', 'static')
  137. love.audio.setEffect('testeffect', {
  138. type = 'flanger',
  139. volume = 10
  140. })
  141. local seteffect, err = effsource:setEffect('flanger', {
  142. type = 'highpass',
  143. volume = 0.3,
  144. lowgain = 0.1
  145. })
  146. -- both these fail on 12 using stereo or mono, no err
  147. test:assertEquals(true, seteffect, 'check effect was applied')
  148. local filtersettings = effsource:getEffect('chorus', {})
  149. test:assertNotNil(filtersettings)
  150. end
  151. --------------------------------------------------------------------------------
  152. --------------------------------------------------------------------------------
  153. ------------------------------------METHODS-------------------------------------
  154. --------------------------------------------------------------------------------
  155. --------------------------------------------------------------------------------
  156. -- love.audio.getActiveEffects
  157. love.test.audio.getActiveEffects = function(test)
  158. -- check we get a value
  159. test:assertNotNil(love.audio.getActiveEffects())
  160. -- check setting an effect active
  161. love.audio.setEffect('testeffect', {
  162. type = 'chorus',
  163. volume = 10
  164. })
  165. test:assertEquals(1, #love.audio.getActiveEffects(), 'check 1 effect running')
  166. test:assertEquals('testeffect', love.audio.getActiveEffects()[1], 'check effect details')
  167. end
  168. -- love.audio.getActiveSourceCount
  169. love.test.audio.getActiveSourceCount = function(test)
  170. -- check we get a value
  171. test:assertNotNil(love.audio.getActiveSourceCount())
  172. -- check source isn't active by default
  173. local testsource = love.audio.newSource('resources/click.ogg', 'static')
  174. test:assertEquals(0, love.audio.getActiveSourceCount(), 'check not active')
  175. -- check playing a source marks it as active
  176. love.audio.play(testsource)
  177. test:assertEquals(1, love.audio.getActiveSourceCount(), 'check now active')
  178. love.audio.pause()
  179. end
  180. -- love.audio.getDistanceModel
  181. love.test.audio.getDistanceModel = function(test)
  182. -- check we get a value
  183. test:assertNotNil(love.audio.getDistanceModel())
  184. -- check default value from documentation
  185. test:assertEquals('inverseclamped', love.audio.getDistanceModel(), 'check default value')
  186. -- check get correct value after setting
  187. love.audio.setDistanceModel('inverse')
  188. test:assertEquals('inverse', love.audio.getDistanceModel(), 'check setting model')
  189. end
  190. -- love.audio.getDopplerScale
  191. love.test.audio.getDopplerScale = function(test)
  192. -- check default value
  193. test:assertEquals(1, love.audio.getDopplerScale(), 'check default 1')
  194. -- check correct value after setting to 0
  195. love.audio.setDopplerScale(0)
  196. test:assertEquals(0, love.audio.getDopplerScale(), 'check setting to 0')
  197. love.audio.setDopplerScale(1)
  198. end
  199. -- love.audio.getEffect
  200. love.test.audio.getEffect = function(test)
  201. -- check getting a non-existent effect
  202. test:assertEquals(nil, love.audio.getEffect('madeupname'), 'check wrong name')
  203. -- check getting a valid effect
  204. love.audio.setEffect('testeffect', {
  205. type = 'chorus',
  206. volume = 10
  207. })
  208. test:assertNotNil(love.audio.getEffect('testeffect'))
  209. -- check effect values match creation values
  210. test:assertEquals('chorus', love.audio.getEffect('testeffect').type, 'check effect type')
  211. test:assertEquals(10, love.audio.getEffect('testeffect').volume, 'check effect volume')
  212. end
  213. -- love.audio.getMaxSceneEffects
  214. -- @NOTE feel like this is platform specific number so best we can do is a nil?
  215. love.test.audio.getMaxSceneEffects = function(test)
  216. test:assertNotNil(love.audio.getMaxSceneEffects())
  217. end
  218. -- love.audio.getMaxSourceEffects
  219. -- @NOTE feel like this is platform specific number so best we can do is a nil?
  220. love.test.audio.getMaxSourceEffects = function(test)
  221. test:assertNotNil(love.audio.getMaxSourceEffects())
  222. end
  223. -- love.audio.getOrientation
  224. -- @NOTE is there an expected default listener pos?
  225. love.test.audio.getOrientation = function(test)
  226. -- checking getting values matches what was set
  227. love.audio.setOrientation(1, 2, 3, 4, 5, 6)
  228. local fx, fy, fz, ux, uy, uz = love.audio.getOrientation()
  229. test:assertEquals(1, fx, 'check fx orientation')
  230. test:assertEquals(2, fy, 'check fy orientation')
  231. test:assertEquals(3, fz, 'check fz orientation')
  232. test:assertEquals(4, ux, 'check ux orientation')
  233. test:assertEquals(5, uy, 'check uy orientation')
  234. test:assertEquals(6, uz, 'check uz orientation')
  235. end
  236. -- love.audio.getPosition
  237. -- @NOTE is there an expected default listener pos?
  238. love.test.audio.getPosition = function(test)
  239. -- check getting values matches what was set
  240. love.audio.setPosition(1, 2, 3)
  241. local x, y, z = love.audio.getPosition()
  242. test:assertEquals(1, x, 'check x position')
  243. test:assertEquals(2, y, 'check y position')
  244. test:assertEquals(3, z, 'check z position')
  245. end
  246. -- love.audio.getRecordingDevices
  247. -- @NOTE hardware dependent so best can do is not nil check
  248. love.test.audio.getRecordingDevices = function(test)
  249. test:assertNotNil(love.audio.getRecordingDevices())
  250. end
  251. -- love.audio.getVelocity
  252. love.test.audio.getVelocity = function(test)
  253. -- check getting values matches what was set
  254. love.audio.setVelocity(1, 2, 3)
  255. local x, y, z = love.audio.getVelocity()
  256. test:assertEquals(1, x, 'check x velocity')
  257. test:assertEquals(2, y, 'check y velocity')
  258. test:assertEquals(3, z, 'check z velocity')
  259. end
  260. -- love.audio.getVolume
  261. love.test.audio.getVolume = function(test)
  262. -- check getting values matches what was set
  263. love.audio.setVolume(0.5)
  264. test:assertEquals(0.5, love.audio.getVolume(), 'check matches set')
  265. end
  266. -- love.audio.isEffectsSupported
  267. love.test.audio.isEffectsSupported = function(test)
  268. test:assertNotNil(love.audio.isEffectsSupported())
  269. end
  270. -- love.audio.newQueueableSource
  271. -- @NOTE this is just basic nil checking, objs have their own test method
  272. love.test.audio.newQueueableSource = function(test)
  273. test:assertObject(love.audio.newQueueableSource(32, 8, 1, 8))
  274. end
  275. -- love.audio.newSource
  276. -- @NOTE this is just basic nil checking, objs have their own test method
  277. love.test.audio.newSource = function(test)
  278. test:assertObject(love.audio.newSource('resources/click.ogg', 'static'))
  279. test:assertObject(love.audio.newSource('resources/click.ogg', 'stream'))
  280. end
  281. -- love.audio.pause
  282. love.test.audio.pause = function(test)
  283. -- check nothing paused (as should be nothing playing)
  284. local nopauses = love.audio.pause()
  285. test:assertEquals(0, #nopauses, 'check nothing paused')
  286. -- check 1 source paused after playing/pausing 1
  287. local source = love.audio.newSource('resources/click.ogg', 'static')
  288. love.audio.play(source)
  289. local onepause = love.audio.pause()
  290. test:assertEquals(1, #onepause, 'check 1 paused')
  291. end
  292. -- love.audio.play
  293. love.test.audio.play = function(test)
  294. -- check playing source is detected
  295. local source = love.audio.newSource('resources/click.ogg', 'static')
  296. love.audio.play(source)
  297. test:assertEquals(true, source:isPlaying(), 'check something playing')
  298. love.audio.pause()
  299. end
  300. -- love.audio.setDistanceModel
  301. love.test.audio.setDistanceModel = function(test)
  302. -- check setting each of the distance models is accepted and val returned
  303. local distancemodel = {
  304. 'none', 'inverse', 'inverseclamped', 'linear', 'linearclamped',
  305. 'exponent', 'exponentclamped'
  306. }
  307. for d=1,#distancemodel do
  308. love.audio.setDistanceModel(distancemodel[d])
  309. test:assertEquals(distancemodel[d], love.audio.getDistanceModel(),
  310. 'check model set to ' .. distancemodel[d])
  311. end
  312. end
  313. -- love.audio.setDopplerScale
  314. love.test.audio.setDopplerScale = function(test)
  315. -- check setting value is returned properly
  316. love.audio.setDopplerScale(0)
  317. test:assertEquals(0, love.audio.getDopplerScale(), 'check set to 0')
  318. love.audio.setDopplerScale(1)
  319. test:assertEquals(1, love.audio.getDopplerScale(), 'check set to 1')
  320. end
  321. -- love.audio.setEffect
  322. love.test.audio.setEffect = function(test)
  323. -- check effect is set correctly
  324. local effect = love.audio.setEffect('testeffect', {
  325. type = 'chorus',
  326. volume = 10
  327. })
  328. test:assertEquals(true, effect, 'check effect created')
  329. -- check values set match
  330. local settings = love.audio.getEffect('testeffect')
  331. test:assertEquals('chorus', settings.type, 'check effect type')
  332. test:assertEquals(10, settings.volume, 'check effect volume')
  333. end
  334. -- love.audio.setMixWithSystem
  335. love.test.audio.setMixWithSystem = function(test)
  336. test:assertNotNil(love.audio.setMixWithSystem(true))
  337. end
  338. -- love.audio.setOrientation
  339. love.test.audio.setOrientation = function(test)
  340. -- check setting orientation vals are returned
  341. love.audio.setOrientation(1, 2, 3, 4, 5, 6)
  342. local fx, fy, fz, ux, uy, uz = love.audio.getOrientation()
  343. test:assertEquals(1, fx, 'check fx orientation')
  344. test:assertEquals(2, fy, 'check fy orientation')
  345. test:assertEquals(3, fz, 'check fz orientation')
  346. test:assertEquals(4, ux, 'check ux orientation')
  347. test:assertEquals(5, uy, 'check uy orientation')
  348. test:assertEquals(6, uz, 'check uz orientation')
  349. end
  350. -- love.audio.setPosition
  351. love.test.audio.setPosition = function(test)
  352. -- check setting position vals are returned
  353. love.audio.setPosition(1, 2, 3)
  354. local x, y, z = love.audio.getPosition()
  355. test:assertEquals(1, x, 'check x position')
  356. test:assertEquals(2, y, 'check y position')
  357. test:assertEquals(3, z, 'check z position')
  358. end
  359. -- love.audio.setVelocity
  360. love.test.audio.setVelocity = function(test)
  361. -- check setting velocity vals are returned
  362. love.audio.setVelocity(1, 2, 3)
  363. local x, y, z = love.audio.getVelocity()
  364. test:assertEquals(1, x, 'check x velocity')
  365. test:assertEquals(2, y, 'check y velocity')
  366. test:assertEquals(3, z, 'check z velocity')
  367. end
  368. -- love.audio.setVolume
  369. love.test.audio.setVolume = function(test)
  370. -- check setting volume works
  371. love.audio.setVolume(0.5)
  372. test:assertEquals(0.5, love.audio.getVolume(), 'check set to 0.5')
  373. end
  374. -- love.audio.stop
  375. love.test.audio.stop = function(test)
  376. -- check source is playing first
  377. local source = love.audio.newSource('resources/click.ogg', 'static')
  378. love.audio.play(source)
  379. test:assertEquals(true, source:isPlaying(), 'check is playing')
  380. -- check source is then stopped
  381. love.audio.stop()
  382. test:assertEquals(false, source:isPlaying(), 'check stopped playing')
  383. end