Author Topic: Music Visualiser 2D  (Read 3320 times)

0 Members and 1 Guest are viewing this topic.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Music Visualiser 2D
« on: May 19, 2021, 01:59:39 am »
Hey everyone! When I learned about _MEMSOUND by watching Fellippe's video on QB64 Team channel.. I had an
idea to visualise the sound...

REPLACE the value of file_name$ to your sound file.

Have fun!!
Code: QB64: [Select]
  1. '#####################
  2. ' Music Visualiser 2D
  3. ' By Ashish
  4. ' 18 May, 2021
  5. '#####################
  6.  
  7. file_name$ = "rolling.mp3" '<<---- replace with your file name with a sound file you have
  8. $Let DEBUG = 1
  9.  
  10. $If DEBUG Then
  11.     $Console
  12.     _Echo "loading sound"
  13. Dim song&
  14.  
  15. song& = _SndOpen(file_name$)
  16. If song& = 0 Then Print "Load failed.": End
  17.  
  18. _Title "Music Visualiser 2D"
  19. Type vec3
  20.     As Single x, y, z
  21. Type vec2
  22.     As Single x, y
  23.     Sub gluLookAt (ByVal eyeX#, Byval eyeY#, Byval eyeZ#, Byval centerX#, Byval centerY#, Byval centerZ#, Byval upX#, Byval upY#, Byval upZ#)
  24. Screen _NewImage(800, 600, 32)
  25. Dim Shared leftChannel As _MEM, rightChannel As _MEM
  26. Dim Shared stereo As _Byte, glAllow As _Byte
  27. Dim Shared leftData(0 To 360) As Single, rightData(0 To 360) As Single, colorData(0 To 360) As vec3
  28. Dim tmp_color As _Unsigned Long
  29. For i = 0 To 360
  30.     tmp_color = hsb~&(i * (255 / 360), 255, 128, 255)
  31.     colorData(i).x = _Red(tmp_color) / 255
  32.     colorData(i).y = _Green(tmp_color) / 255
  33.     colorData(i).z = _Blue(tmp_color) / 255
  34.  
  35. leftChannel = _MemSound(song&, 1)
  36. rightChannel = _MemSound(song&, 2)
  37. If leftChannel.SIZE = 0 Then
  38.     Print "An error occurred."
  39.     End
  40. If rightChannel.SIZE = 0 Then
  41.     $If DEBUG Then
  42.         _Echo "sound is mono"
  43.     $End If
  44.     stereo = 0
  45.     $If DEBUG Then
  46.         _Echo "sound is stereo"
  47.     $End If
  48.     stereo = 1
  49.  
  50. i = 0
  51. glAllow = 1
  52. _FPS 60
  53. _SndPlay song&
  54.  
  55.  
  56.  
  57.     For n = 0 To 360
  58.         _MemGet leftChannel, leftChannel.OFFSET + i + j, a1% 'get sound data
  59.         leftData(n) = a1% * 0.00001
  60.         If stereo = 1 Then
  61.             _MemGet rightChannel, rightChannel.OFFSET + i + j, a2%
  62.             rightData(n) = a2% * 0.00001
  63.         End If
  64.         j = j + 2
  65.         If i + j + 2 > leftChannel.SIZE Then Exit Do
  66.         If stereo = 1 And i + j + 2 > rightChannel.SIZE Then Exit Do
  67.     Next
  68.     i = i + 2 * (44100 / 60)
  69.     j = 0
  70.     If i + 2 > leftChannel.SIZE Then Exit Do
  71.     If stereo = 1 And i + j + 2 > rightChannel.SIZE Then Exit Do
  72.     _Limit 60
  73.  
  74.  
  75. _SndClose song& 'closing the sound releases the mem blocks
  76.  
  77. Sub _GL ()
  78.     Static glInit
  79.     If glAllow = 0 Then
  80.         Exit Sub
  81.     End If
  82.     If glInit = 0 Then
  83.         glInit = 1
  84.         _glViewport 0, 0, _Width, _Height
  85.         $If DEBUG Then
  86.             _Echo "_GL() : initialization done. glInit set to 1"
  87.         $End If
  88.     End If
  89.  
  90.     'projection setup
  91.     _glMatrixMode _GL_PROJECTION
  92.     _gluPerspective 50, _Width / _Height, 0.1, 50
  93.  
  94.     _glMatrixMode _GL_MODELVIEW
  95.     gluLookAt 0, 0, 3, 0, 0, 0, 0, 1, 0
  96.  
  97.  
  98.  
  99.     _glBegin _GL_LINES
  100.     For i = 0 To 360
  101.         c = Int(map(Abs(leftData(i)), 0, 0.7, 0, 360))
  102.         _glColor3f colorData(c).x, colorData(c).y, colorData(c).z
  103.         _glVertex2f -1, 0
  104.         _glVertex2f -1 + (0.5 + leftData(i)) * Cos(_D2R(i)), (0.5 + leftData(i)) * Sin(_D2R(i))
  105.         _glVertex2f 1, 0
  106.         If stereo = 0 Then
  107.  
  108.             _glVertex2f 1 + (0.5 + leftData(i)) * Cos(_D2R(i)), (0.5 + leftData(i)) * Sin(_D2R(i))
  109.         Else
  110.             _glVertex2f 1 + (0.5 + rightData(i)) * Cos(_D2R(i)), (0.5 + rightData(i)) * Sin(_D2R(i))
  111.         End If
  112.     Next
  113.     _glEnd
  114.     _glFlush
  115.  
  116.  
  117. 'function from
  118. 'p5js.bas
  119. 'http://bit.ly/p5jsbas
  120. Function map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  121.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  122.  
  123.  
  124. 'method adapted form http://stackoverflow.com/questions/4106363/converting-rgb-to-hsb-colors
  125. Function hsb~& (__H As _Float, __S As _Float, __B As _Float, A As _Float)
  126.     Dim H As _Float, S As _Float, B As _Float
  127.  
  128.     H = map(__H, 0, 255, 0, 360)
  129.     S = map(__S, 0, 255, 0, 1)
  130.     B = map(__B, 0, 255, 0, 1)
  131.  
  132.     If S = 0 Then
  133.         hsb~& = _RGBA32(B * 255, B * 255, B * 255, A)
  134.         Exit Function
  135.     End If
  136.  
  137.     Dim fmx As _Float, fmn As _Float
  138.     Dim fmd As _Float, iSextant As Integer
  139.     Dim imx As Integer, imd As Integer, imn As Integer
  140.  
  141.     If B > .5 Then
  142.         fmx = B - (B * S) + S
  143.         fmn = B + (B * S) - S
  144.     Else
  145.         fmx = B + (B * S)
  146.         fmn = B - (B * S)
  147.     End If
  148.  
  149.     iSextant = Int(H / 60)
  150.  
  151.     If H >= 300 Then
  152.         H = H - 360
  153.     End If
  154.  
  155.     H = H / 60
  156.     H = H - (2 * Int(((iSextant + 1) Mod 6) / 2))
  157.  
  158.     If iSextant Mod 2 = 0 Then
  159.         fmd = (H * (fmx - fmn)) + fmn
  160.     Else
  161.         fmd = fmn - (H * (fmx - fmn))
  162.     End If
  163.  
  164.     imx = _Round(fmx * 255)
  165.     imd = _Round(fmd * 255)
  166.     imn = _Round(fmn * 255)
  167.  
  168.     Select Case Int(iSextant)
  169.         Case 1
  170.             hsb~& = _RGBA32(imd, imx, imn, A)
  171.         Case 2
  172.             hsb~& = _RGBA32(imn, imx, imd, A)
  173.         Case 3
  174.             hsb~& = _RGBA32(imn, imd, imx, A)
  175.         Case 4
  176.             hsb~& = _RGBA32(imd, imn, imx, A)
  177.         Case 5
  178.             hsb~& = _RGBA32(imx, imn, imd, A)
  179.         Case Else
  180.             hsb~& = _RGBA32(imx, imd, imn, A)
  181.     End Select
  182.  
  183.  


SCRST_1.png
* SCRST_1.png (Filesize: 71.03 KB, Dimensions: 802x629, Views: 253)
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Music Visualiser 2D
« Reply #1 on: May 19, 2021, 07:51:23 am »
That's pretty cool.... Look at the display 'cross-eyed' and focus on the center image...
Logic is the beginning of wisdom.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Music Visualiser 2D
« Reply #2 on: May 20, 2021, 01:21:38 am »
Thank you, @johnno56 !
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: Music Visualiser 2D
« Reply #3 on: May 20, 2021, 10:40:38 am »
That's cool, Ashish!  Inspires me to tinker around making a sound visualizer too.  Thanks for posting.

- Dav