Page 1 of 1

Sounds don't play sometimes.

Posted: Tue Jul 15, 2014 6:06 am
by columna1
Im working on a small music player and as of right now It is a proof of concept. I want to play the sounds according to what notes should be played. I made the program load all the sounds into a table and play them when I need them but it only plays the sounds sometimes. When I load the sound and play it when I need it It works but eats up more and more memory the more notes are played which sucks a lot.
Here is some of the code I used:

Code: Select all

notes = loadMidi("Tower of Heaven Medley.mid","time",true)


piano = {  [1] = "a1.wav",  [2] = "b1.wav",  [3] = "a2.wav",  [4] = "b2.wav",  [5] = "a3.wav",  [6] = "a4.wav",  [7] = "b3.wav",  [8] = "a5.wav",  [9] = "b4.wav",  [10] = "a6.wav",  [11] = "b5.wav",  [12] = "a7.wav",  [13] = "a8.wav",  [14] = "b6.wav",  [15] = "a9.wav",  [16] = "b7.wav",  [17] = "a10.wav",  [18] = "a11.wav",  [19] = "b8.wav",  [20] = "a12.wav",  [21] = "b9.wav",  [22] = "a13.wav",  [23] = "b10.wav",  [24] = "a14.wav",  [25] = "a15.wav",  [26] = "b11.wav",  [27] = "a16.wav",  [28] = "b12.wav",  [29] = "a17.wav",  [30] = "a18.wav",  [31] = "b13.wav",  [32] = "a19.wav",  [33] = "b14.wav",  [34] = "a20.wav",  [35] = "b15.wav",  [36] = "a21.wav",  [37] = "a22.wav",  [38] = "b16.wav",  [39] = "a23.wav",  [40] = "b17.wav",  [41] = "a24.wav",  [42] = "a25.wav",  [43] = "b18.wav",  [44] = "a26.wav",  [45] = "b19.wav",  [46] = "a27.wav",  [47] = "b20.wav",  [48] = "a28.wav",  [49] = "a29.wav",  [50] = "b21.wav",  [51] = "a30.wav",  [52] = "b22.wav",  [53] = "a31.wav",  [54] = "a32.wav",  [55] = "b23.wav",  [56] = "a33.wav",  [57] = "b24.wav",  [58] = "a34.wav",  [59] = "b25.wav",  [60] = "a35.wav",  [61] = "a36.wav"}

function playNote(num,vel)
  love.audio.play(sounds[num])
  dis[#dis+1] = {num,nt}
end

function love.load()
  dis = {}
  sounds = {}
  for i = 1,#piano do
    sounds[i] = love.audio.newSource("piano/"..piano[i])
  end
  lt,nt = 0,0
end 

function love.draw()
  love.graphics.print(nt,10,10)
  for i = 1,#dis do
    if dis[i][2]+1 > nt then
      love.graphics.rectangle("fill",(600/61)*dis[i][1],10,5,100)
    end
  end
end 
--notes are stored like {delta time it started, when it ended, how long it lasted, note, velocity, track,instrument}
function love.update(dt)
  nt = nt + dt
  for i = 1,#notes do
    --if the note is between the last and the current then play it
    if notes[i][1] >= lt and notes[i][1] < nt then
      --play the note
      if notes[i][4] > 35 and notes[i][4] < 97 then
        --str = str.."\rSend, "..sendTab[notes[i][4]-35]
        playNote(notes[i][4]-35,1)
      end
    end
  end
  lt = nt
end 
EDIT: it seems that the problem is that you can't play the same audio source if it is currently being played, Working on a workaround

EDIT2: Fixed it here is what I changed:

Code: Select all

function playNote(num,vel)
  --source = love.audio.newSource("piano/"..piano[num])
  if sounds[num]:isPlaying() then sounds[num]:stop() end
  sounds[num]:play()
  dis[#dis+1] = {num,nt}
end