B4A Class Audio Visualizer

Discussion in 'Additional libraries, classes and official updates' started by stevel05, Jun 9, 2014.

  1. stevel05

    stevel05 Expert Licensed User

    As a response to a request on the forums, and to try out the new JavaObject functionality, I decided to take a diversion and write a wrapper for the http://developer.android.com/reference/android/media/audiofx/Visualizer.html.

    It's written as a B4a class using JavaObject, and provides the DataCaptureListener and callbacks for wave and fft data.

    Just add an audio file, and some processing code.

    The Class depends on JavaObject and the Demo App depends on Reflection and JavaObject.

    You need to add permission: RECORD_AUDIO to the Manifest.

    Code:
    AddPermission("android.permission.RECORD_AUDIO")
    If you want to apply the visualizer to the main outputs (AudioSession = 0) you also need permission: MODIFY_AUDIO_SETTINGS

    Code:
    AddPermission("android.permission.MODIFY_AUDIO_SETTINGS")
    Requires B4A 3.80+

    The Class depends on JavaObject and the Demo App depends on Reflection and JavaObject.

    Enjoy

    Update: Added a new version with WavForm drawing to a user provided panel. While I have tested it on various configurations, it needs to be tested further before production use. Please let me know if you come across any issues.

    This version has additional dependencies:

    SLByteArrayBuffer and Threading Libraries.

    Edit: I forgot to mention that as the Wav drawing is done in a separate thread, it won't run in Debug mode. You can disable it by temporarily removing the TH.Start call to the draw routine, better still use conditional compilation.

    If you want to debug the rest of the app, add

    Code:
    #ExcludeFromDebugger: True
    to the top of the WavDraw class, then you can run the app in debug mode, but you can't debug the class. In legacy debug mode the wav will be drawn, in Rapid debug mode it won't, but the app will still run.

    As an aside, I know nothing about using fft, if someone would care to provide an example using that, it would be most appreciated.

    Edit: 20/1/16 Uploaded new Vizualizer.bas Class which fixes the bug from post #29
    26/3/18 Update to fix the bug in post #29
     

    Attached Files:

    Last edited: Mar 26, 2018
    Levit, noeleon, moster67 and 9 others like this.
  2. stevel05

    stevel05 Expert Licensed User

    Updated to add waveform drawing to a panel.
     
    Erel likes this.
  3. fanfalveto

    fanfalveto Active Member Licensed User

    Hi,i have a problem with samsung galaxy s4 device.
    i do in Main declare and initialize a Mediaplayer. (so1)
    then
    and into other activity your visualizer-wavform.
    i load a song and mediaplayer play rigth from File.DirRootExternal&"/soundfx/ritm/" but i can´t see the graphics.
    but from File.DirAssets i can see.
    any idea why? thank you
     
  4. stevel05

    stevel05 Expert Licensed User

    Can you zip and post your project, I can't see where you've initialized the visualizer , or the wave form draw in the code you've posted.

    The visualizer modules should be in the same activity as the mediaplayer, if you're not sure, it's better to start with the demo project and change that to suit your needs.
     
  5. fanfalveto

    fanfalveto Active Member Licensed User

    i solved the problem with library file explorer by Informatix (thank you for this :))
    thank you.
     
  6. fanfalveto

    fanfalveto Active Member Licensed User

    please,how i can visualizer 2 songs at same time,one in green and the other in white for example.
    i try to create another viz object but crash.
    any idea?
    thank you.
     
  7. stevel05

    stevel05 Expert Licensed User

    How are you playing your two songs? Two Mediaplayer objects?
     
  8. fanfalveto

    fanfalveto Active Member Licensed User

    yes,with two diferents Mediaplayer objects.
     
  9. stevel05

    stevel05 Expert Licensed User

    You need to create two of everything (including panels). And delay the start of subsequent drawing routines.

    Try this one.
     

    Attached Files:

  10. fanfalveto

    fanfalveto Active Member Licensed User

    don´t run fine.upload a zip with some changes.
    please can you see?
    thank you
     

    Attached Files:

  11. stevel05

    stevel05 Expert Licensed User

    You had initialiszed so1 twice and not so2, and you had disabled the timer that starts the second wave draw.
     

    Attached Files:

  12. fanfalveto

    fanfalveto Active Member Licensed User

    :( thank you very much
     
  13. stevel05

    stevel05 Expert Licensed User

    If you want to debug the rest of your app, add
    Code:
    #ExcludeFromDebugger: True
    to the top of the WavDraw class, see post 1.
     
  14. fanfalveto

    fanfalveto Active Member Licensed User

    Thank you very much.;)
     
  15. BarryW

    BarryW Active Member Licensed User

    Can i used this class while recording an audio. Is short visualize while recording. Thx Masters.
     
  16. stevel05

    stevel05 Expert Licensed User

    The Vizualizer is driven from the audio output, if you play the audio at the same time you are recording it, it should work, you can try setting the output volume to 0 if you don't want to hear it.
     
  17. BarryW

    BarryW Active Member Licensed User

    Is it possible to play audio recording while recording it? How?
     
  18. stevel05

    stevel05 Expert Licensed User

    What library are you using to record?
     
  19. fanfalveto

    fanfalveto Active Member Licensed User

    Hi, i try
    Code:
    R.Target = tem.mp
    where "tem" is a service.
    The program runs but the visualizer not
    Code:
    ** Activity (main) Create, isFirst = true **

    java.lang.NoSuchFieldException: tem.mp

        at java.lang.Class.getDeclaredField(Class.java:
    596)
        at anywheresoftware.b4a.agraham.reflection.Reflection.GetField(Reflection.java:
    316)
        at alanhm.vngsoft.music.main._activity_create(main.java:
    359)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:
    515)
        at anywheresoftware.b4a.BA.raiseEvent2(BA.java:
    175)
        at alanhm.vngsoft.music.main.afterFirstLayout(main.java:
    98)
        at alanhm.vngsoft.music.main.access$
    100(main.java:16)
        at alanhm.vngsoft.music.main$WaitForLayout.run(main.java:
    76)
        at android.os.Handler.handleCallback(Handler.java:
    733)
        at android.os.Handler.dispatchMessage(Handler.java:
    95)
        at android.os.Looper.loop(Looper.java:
    157)
        at android.app.ActivityThread.main(ActivityThread.java:
    5356)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:
    515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:
    1265)


        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:
    1081)
        at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:
    132)
        at dalvik.system.NativeStart.main(Native Method)
    java.lang.NoSuchFieldException: tem.mp
    Any idea? Thanks
     
  20. Agnetha

    Agnetha Member Licensed User

    Hello!
    I want to use the sub "getMeasurementPeakRms" , but then i get the error
    Class "android.media.audiofx.Visualizer.MeasurementPeakRms" not found.
    Can you help me? I just want to visualize the audio peaks.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice