Author Topic: Music Routing Problem  (Read 114 times)

0 Members and 1 Guest are viewing this topic.

Offline inc.

  • Contact me @ skype: a5recordings
  • Amiga 1200
  • ****
  • Posts: 261
  • Karma: 19
  • I SPEAK ENGLISH & GERMAN as good as i can :D
    • View Profile
Music Routing Problem
« on: August 06, 2019 »
Hi folks,

after a long time i'm back and have a question.
i wrote a demo code that show up a glsl shader and playing a v2m sound file.

how does it looks like (plain and simple)


after some fails i wanna explain the problem that i have.

If i'm sending the V2M Music Main Out to the Shader, that one of the white SinWave react to the Music nice and smoove does not work properly.


I tried a lot but always unnatural values will be sent to a SinWave and it wobble like this:


this is not what i want :D

define the value as following in Line 96:
Code: [Select]
Change = MP_GetChannelVUV2M(channel, @left.f, @right.f)
and adding the "change" into the shader code in Line 128 as an example:
Code: [Select]
Fragment$ + " float angle = iTime * speed * frequency *" + Change +"+(shift + uv.x) * 2.0;   " + Chr(10)
i know there is anything missing in my thoughts.
maybe some one can help?


if you try to run the code properly, you need a PureBasic Version that support MP3D Lib because i used the v2m Lib that is included. Not to forget in Compiler Settings the LibSubsystem set to : dx9


Code: [Select]
;:
;:  PureBasic Version 5.43 LTS x86
;:
;:
;:  get the MP3D Lib from here:
;:  inc-games-design.de/Tools/Mp3dLib_installer.rar
;:
;:



Enumeration
  #Main
EndEnumeration
Enumeration
  #leftbar
  #rightbar
  #Gad_OpenGL
  #Exit_btn
  #Gadget_exit
  #textgadget
  #Infobar_Font
EndEnumeration
#GL_VERTEX_SHADER = $8B31
#GL_FRAGMENT_SHADER = $8B30
#GL_BGR = $80E0
#GL_BGRA = $80E1
DataSection
  Song:
  IncludeBinary "Sound\mpl.v2m"
  SongEnd:
EndDataSection

;:handle the GLSL needs
XIncludeFile "include\cm_opengl_uni.pbi" : UseModule cm_opengl

Define IsPlaying, EndOfTrackReached, EventGadget
Define.i Event

;:V2M Music File Main Out - left / right
Define MainVULeft.f,MainVURight.f

;:Channels V2M Music File - no need yet
Define Ch0VULeft.f, Ch0VURight.f
Define Ch1VULeft.f, Ch1VURight.f
Define Ch2VULeft.f, Ch2VURight.f
Define Ch3VULeft.f, Ch3VURight.f
Define Ch4VULeft.f, Ch4VURight.f
Define Ch5VULeft.f, Ch5VURight.f
Define Ch6VULeft.f, Ch6VURight.f
Define Ch7VULeft.f, Ch7VURight.f
Define Ch8VULeft.f, Ch8VURight.f
Define Ch9VULeft.f, Ch9VURight.f
Define Ch10VULeft.f, Ch10VURight.f
Define Ch11VULeft.f, Ch11VURight.f
Define Ch12VULeft.f, Ch12VURight.f
Define Ch13VULeft.f, Ch13VURight.f
Define Ch14VULeft.f, Ch14VURight.f
Define Ch15VULeft.f, Ch15VURight.f


Procedure Open_MainScreen()
 
  CreateImage(#Exit_btn,16,16)
  StartDrawing(ImageOutput(#Exit_btn))
  FillArea(1,1,-1,RGB(137,187,229))
  Line(0,16,16,-17,RGB(45, 45, 45))
  Line(0,0,16,16,RGB(45, 45, 45))
  StopDrawing()
  OpenWindow(#Main, 0 , 0, 640, 480,"",#PB_Window_ScreenCentered|#PB_Window_BorderLess|#PB_Window_Invisible)
  SetWindowColor(#Main, RGB(137,187,229))
  SetClassLongPtr_(WindowID(#Main), #GCL_STYLE, #CS_DROPSHADOW)
  ;:exit button
  ImageGadget(#Gadget_exit,WindowWidth(0)-22,2,16,16,ImageID(#Exit_btn))
  TextGadget(#textgadget,5, 1, 567, 20, "GLSL Shader Test: inc 2019",#SS_CENTERIMAGE)
  SetGadgetFont(#textgadget, LoadFont(#Infobar_Font, "Microsoft Sans Serif", 8, #PB_Font_HighQuality))
  SetGadgetColor(#textgadget , #PB_Gadget_BackColor, RGB(137,187,229))
  SetGadgetColor(#textgadget , #PB_Gadget_FrontColor, RGB(15, 15, 15))

 
  OpenGLGadget(#Gad_OpenGL, 0, 20, 640, 460, #PB_OpenGL_Keyboard)
  Init_OpenGL(#Gad_OpenGL, 640, 420)
 
  HideWindow(#Main, #False)

EndProcedure

Open_MainScreen()

  MP_CatchV2M(?Song)  ;LOAD SONG
  MP_PlayV2M(0)       ;0 START SOUND FROM THE BEGINNING           
  MP_SetVolumeV2M(1)
  MP_GetMainVUV2M(@MainVULeft.f,@MainVURight.f)
 
  ;channel = -1
  ;Change = MP_GetChannelVUV2M(channel, @left.f, @right.f)

   
  ;Change = MP_GetChannelVUV2M(channel, @MainVULeft.f, @MainVURight.f)
  ;SetGadgetState(#ProgressBar_6,(Ch1VULeft + Ch1VURight)*20)
;:----------------------------------------------------------------------------------------
  ;:The plain Shader Code
  ;{
Vertex$ = "attribute vec3 position;"
Vertex$ + "attribute vec2 surfacePosAttrib;"
Vertex$ + "varying vec2 surfacePosition;"
Vertex$ + " void main() {"
Vertex$ + " surfacePosition = surfacePosAttrib;"
Vertex$ + " gl_Position = vec4( position, 1.0 );"
Vertex$ + " }"

Fragment$ = " #ifdef GL_ES                                                                       " + Chr(10)
Fragment$ + " precision mediump float;                                                           " + Chr(10)
Fragment$ + " #endif                                                                             " + Chr(10)
Fragment$ + " #extension GL_OES_standard_derivatives : enable                                   " + Chr(10)
Fragment$ + " uniform float time;                                                               " + Chr(10)
Fragment$ + " uniform vec2 mouse;                                                               " + Chr(10)
Fragment$ + " uniform vec2 resolution;                                                           " + Chr(10)
Fragment$ + " const vec3 top = vec3(0.318, 0.831, 1.0);                                         " + Chr(10)
Fragment$ + " const vec3 bottom = vec3(0.094, 0.141, 0.424);                                     " + Chr(10)
Fragment$ + " const float widthFactor = 1.5;                                                     " + Chr(10)
Fragment$ + " #define iTime time                                                                 " + Chr(10)
Fragment$ + " #define iResolution resolution                                                     " + Chr(10)
Fragment$ + " vec3 calcSine(vec2 uv, float speed,                                               " + Chr(10)
Fragment$ + " float frequency, float amplitude, float shift, float offset,                       " + Chr(10)
Fragment$ + " vec3 color, float width, float exponent, bool dir)                                 " + Chr(10)
Fragment$ + " {                                                                                 " + Chr(10)
Fragment$ + " float angle = iTime * speed * frequency *" + Change +"+(shift + uv.x) * 2.0;   " + Chr(10)
Fragment$ + " float y = sin(angle) * amplitude + offset;                                         " + Chr(10)
Fragment$ + " float clampY = clamp(0.0, y, y);                                                   " + Chr(10)
Fragment$ + " float diffY = y - uv.y;                                                           " + Chr(10)
Fragment$ + " float dsqr = distance(y, uv.y);                                                   " + Chr(10)
Fragment$ + " float scale = 1.0;                                                                 " + Chr(10)
Fragment$ + " if(dir && diffY > 0.0)                                                             " + Chr(10)
Fragment$ + " {                                                                                 " + Chr(10)
Fragment$ + " dsqr = dsqr * 4.0;                                                                 " + Chr(10)
Fragment$ + " }                                                                                 " + Chr(10)
Fragment$ + " else if(!dir && diffY < 0.0)                                                       " + Chr(10)
Fragment$ + " {                                                                                 " + Chr(10)
Fragment$ + " dsqr = dsqr * 4.0;                                                                 " + Chr(10)
Fragment$ + " }                                                                                 " + Chr(10)
Fragment$ + " scale = pow(smoothstep(width * widthFactor, 0.0, dsqr), exponent);                 " + Chr(10)
Fragment$ + " return min(color * scale, color);                                                 " + Chr(10)
Fragment$ + " }                                                                                 " + Chr(10)
Fragment$ + " void mainImage( out vec4 fragColor, in vec2 fragCoord )                           " + Chr(10)
Fragment$ + " {                                                                                 " + Chr(10)
Fragment$ + " vec2 uv = fragCoord.xy / iResolution.xy;                                           " + Chr(10)
Fragment$ + " vec3 color = vec3(mix(bottom, top, uv.y));                                         " + Chr(10)
Fragment$ + " color += calcSine(uv, 0.2, 0.20, 0.2, 0.0, 0.5,  vec3(0.3, 0.3, 0.3), 0.1, 15.0,false);    " + Chr(10)
Fragment$ + " color += calcSine(uv, 0.4, 0.40, 0.15, 0.0, 0.5, vec3(0.3, 0.3, 0.3), 0.1, 17.0,false); " + Chr(10)
Fragment$ + " color += calcSine(uv, 0.3, 0.60, 0.15, 0.0, 0.5, vec3(0.3, 0.3, 0.3), 0.05, 23.0,false); " + Chr(10)
Fragment$ + " color += calcSine(uv, 0.1, 0.26, 0.07, 0.0, 0.3, vec3(0.3, 0.3, 0.3), 0.1, 17.0,true); " + Chr(10)
Fragment$ + " color += calcSine(uv, 0.3, 0.36, 0.07, 0.0, 0.3, vec3(0.3, 0.3, 0.3), 0.1, 17.0,true); " + Chr(10)
Fragment$ + " color += calcSine(uv, 0.5, 0.46, 0.07, 0.0, 0.3, vec3(0.3, 0.3, 0.3), 0.05, 23.0,true); " + Chr(10)
Fragment$ + " color += calcSine(uv, 0.2, 0.58, 0.05, 0.0, 0.3, vec3(0.3, 0.3, 0.3), 0.2, 15.0,true); " + Chr(10)
Fragment$ + " fragColor = vec4(color,1.0);                                                       " + Chr(10)
Fragment$ + " }                                                                                 " + Chr(10)
Fragment$ + " void main( void ) {                                                               " + Chr(10)
Fragment$ + " mainImage(gl_FragColor, gl_FragCoord.xy);                                         " + Chr(10)
Fragment$ + " }                                                                                 " + Chr(10)
;}

CreateShader(1, Vertex$, Fragment$)
UseShader(1)

Repeat
  Event = WindowEvent()
  EventGadget = EventGadget()
 
  Render_OpenGL()
  ;: LOOP FUNCTION V2M SOUND 
 
  If MP_IsPlayingV2M()
      IsPlaying=1           ;CHECK IF SONG IS PLAYING
    Else
      EndOfTrackReached=1   ;IF SONG REACHED ITS END
      MP_PlayV2M(0)         ;0 STARTs SONG At THE BEGINNING
    EndIf

 
  Select Event
    Case #WM_LBUTTONDOWN
      If WindowMouseX(#Main) >= 0 And WindowMouseX(#Main) <= 640 And WindowMouseY(#Main) >= 0 And WindowMouseY(#Main) <= 61
        SendMessage_(WindowID(#Main), #WM_NCLBUTTONDOWN, #HTCAPTION, 0)
      EndIf
    Case #PB_Event_Gadget
      Select EventGadget() 
        Case #Gadget_exit
          End
      EndSelect
  EndSelect
  ;ESC END PROGRAM
  If GetAsyncKeyState_(#VK_ESCAPE)
    End
  EndIf
Until Event = #PB_Event_CloseWindow
End

the complete package is attached to this posting.
« Last Edit: August 09, 2019 by inc. »
currently coding in PureBasic: GLSL Shader Maker & Editor Tool for further Demo coding usage

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2721
  • Karma: 485
    • View Profile
    • http://www.rbraz.com/
Re: Music Routing Problem
« Reply #1 on: August 18, 2019 »
Seems to me that what you would need is to interpolate through v2m output data and send it to the shader, just a guess.
Challenge Trophies Won: