Android Question Cannot clear canvas

Mark Read

Well-Known Member
Licensed User
Longtime User
I am using a modified version of the demo from steveIO5 to show the waveform of a playing mp3. I cannot get the canvas to clear, the waveform is always drawn on top of the old one. What am I doing wrong. Included is the full code without the mp3 file (added in line 37).
The code is for B4A 3.80 and using Media Player

I think it is probably something simple but its driving me crazy!

Thanks for any help
 

Attachments

  • visualizer.zip
    8.5 KB · Views: 215

stevel05

Expert
Licensed User
Longtime User
Basically the issue is that you are not drawing a filled rectangle, but the code is also in the wrong order.

Try this:

B4X:
Sub Wave_Capture(Data() As Byte,SamplingRate As Int)
    'Log("Wav " & Data.Length)
    'Log("Wav data: " & Data(1))
    'Activity.Color=Colors.Transparent      
    xinterval=Activity.Width/Data.Length*1dip
    yinterval=Activity.Height/128*1dip
    xoffset=(Activity.Width-(xinterval*Data.Length))/2
  
    'Erase canvas
    cv.DrawRect( recht1,Colors.black,True,5dip) 'True draws a filled rectangle
      
    For i=1 To Data.Length-1
        'Draw new data  
        'cv.DrawLine((xinterval*i)+xoffset,50%y, xinterval*i,50%y+(i*Data(i)), Colors.Blue,1dip)
        cv.DrawPoint((xinterval*i)+xoffset,50%y+Data(i),Colors.white)
      
    Next
  
      
    Activity.Invalidate
          
  
    'Log("Wave capture")
    'Log("Sampling Rate: " & SamplingRate) 44100
End Sub

I think there is still an error in the drawing routine, but this sorts out your first issue.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Steve, thanks very much. It is working now although I have two waves being drawn, top and bottom. I will continue working on it.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Okay SteveIO5, what is wrong with my drawing routine?
What does the Wave capture sub actually deliver? I see the data array with values +-127 roughly. Should I do something with them or is it okay just to draw the values?
Thanks
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Hi Mark,

I think you will have to use the line draw to get a proper wave representation, I have to go out for a few hours, but will have a look when I get back if you haven"t sorted it. Let me know if you do.

Steve
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
I tried the linedraw but made a mess of it. The code is in the zip file but commented out.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Ok, I will have another look now, but what you have to remember is that you have 1024 samples, if the sample rate is 44100, then that is roughly 1/44 th of a seconds worth of samples. So you need 44.1 buffers full to represent 1 second of audio.

If you zoom in on any audio program to that level, it looks different.

I found some code here: https://gitorious.org/0xdroid/devel...ndroid/apis/media/AudioFxDemo.java#Lundefined

that I've ported to B4a, it's output looks the similar to when I just used linedraw. I'll let you have it when I've done a little more work on it. Basically capture more samples to display.

Steve
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Hi Mark,

I've posted a new version of the Visualizer Class which does wave form drawing to a panel. You can try that, or rip the code out for your own use.

The biggest problem is that the visualizer (believe it or not) actually returns UNSIGNED bytes. Which need to be converted before anything in Java /B4a will work.

Hope it helps.

Steve
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
This is great Steve. I will try it out tonight.
A further question which also relates to this thread:

If I set a canvas on a panel and the panel has a gradient drawn on it, how do I clear the canvas? Drawing a rectangle with transparent fill destroys the gradient.
Option 1: delete the canvas and apply new
Option 2: Save the gradient as a bitmap
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
If you want to keep the gradient, the easiest way is to put another panel directly on top to hold the canvas. Then when you clear it, you still see the gradient.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Hi Steve, I tried your new version of the class with the demo program. I see no waveform at all on my samsung tablet and no error. All libs are present. Start and stop have no effect.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Can you hear the MP3?
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Can you pm me? Its a bit quicker.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Are you running in release mode?
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
not yet, debug rapid
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
aha, in release mode it works????
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
It includes a thread, which will fail if run in debug mode. If you want to debug a different part of the app, you can run the WavDraw sub directly, and remove the call to TH.Start(Me,DrawWav,Null), but the Gui will be pretty unresponsive. Or temporarily disable the wavdraw with the conditional compiling once you're happy it works OK.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Gain increaase the amplitude, but what does zoom do?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
The Zoom shows more (or less) time in the window, hence the slow down of the display updates.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…