09-05-2022, 02:02 PM
I made a test with the code of SierraKen below which reads an mp3 audio file with animation. the quality of the sound is better with the version 3.0.0 of qb64. to note that the animation disappears with the version 3.1.0...
Code: (Select All)
'Tesla Coil by SierraKen - September 1, 2022.
'Tesla Coil will shoot lightning to your music like a real one.
'Make sure and put a song file in the same folder as this program.
'Thank you for the frequency example at the QB64 Wiki Help Page https://qb64phoenix.com/qb64wiki/index.php/MEMSOUND
_Title "Tesla Coil by SierraKen"
Screen _NewImage(800, 600, 32)
start:
Clear
Cls
Dim file$
Input "Song Filename Here: ", file$
DefLng A-Z
Option _Explicit
Option _ExplicitArray
Dim bolts As Single, bolts2 As Single, t As Single
Dim a$
Print "Loading...";
Dim Song As Long
Song = _SndOpen(file$) ' Replace this with your (rad, mid, it, xm, s3m, mod, mp3, flac, ogg, wav) sound file
If Song < 1 Then
Print "Failed to load song!"
End
End If
Print "Done!"
_Display
_SndPlay Song
Dim SampleData As _MEM
SampleData = _MemSound(Song, 1) ' This can now be 0 or 1
If SampleData.SIZE = 0 Then
Print "Failed to access sound sample data."
End
End If
Dim y As Long, i As _Unsigned _Integer64, sf As Single, si As Integer
Dim sz As _Unsigned _Integer64
sz = _CV(_Unsigned _Integer64, _MK$(_Offset, SampleData.ELEMENTSIZE)) ' sz is the total size of the sound in bytes
Do Until Not _SndPlaying(Song) Or i + (_Width * sz) > SampleData.SIZE
a$ = InKey$
If a$ = Chr$(27) Then _SndClose Song: End
Cls
Locate 1, 1: Print i; "/"; SampleData.SIZE, "Frame Size ="; sz, "Data Type ="; SampleData.TYPE
Line (0, 500)-(800, 500), _RGB32(127, 255, 127)
Line (400, 350)-(400, 500), _RGB32(255, 0, 0)
$Checking:Off
If (sz = 4 Or sz = 2) And SampleData.TYPE = 1 Then ' integer stereo or mono
For y = 0 To _Width - 1
si = _MemGet(SampleData, SampleData.OFFSET + i + y * sz, Integer) 'get sound data
If 300 * si / 32768 = 0 Then GoTo skip:
For bolts2 = 1 To 3
For bolts = 1 To t
Line (400, 350)-(400 + 300 * si / 32768, (y / 2) + 200), _RGB32(255, 255, 255)
Next bolts
t = t + 2
Next bolts2
t = 0
Next
ElseIf (sz = 8 Or sz = 4) And SampleData.TYPE = 4 Then ' floating point stereo or mono
For y = 0 To _Width - 1
sf = _MemGet(SampleData, SampleData.OFFSET + i + y * sz, Single) 'get sound data
If sf * 300 = 0 Then GoTo skip:
For bolts2 = 1 To 3
For bolts = 1 To t
Line (400, 350)-(400 + sf * 300, (y / 2) + 200), _RGB32(255, 0, 0)
Next bolts
t = t + 2
Next bolts2
t = 0
Next
ElseIf sz = 2 And SampleData.TYPE = 0 Then ' integer mono (QB64 OpenAL stuff)
For y = 0 To _Width - 1
si = _MemGet(SampleData, SampleData.OFFSET + i + y * sz, Integer) 'get sound data
If 300 * si / 32768 = 0 Then GoTo skip:
For bolts2 = 1 To 3
For bolts = 1 To t
Line (400, 350)-(400 + 300 * si / 32768, (y / 2) + 200), _RGB32(255, 255, 255)
Next bolts
t = t + 2
Next bolts2
t = 0
Next
End If
skip:
$Checking:On
_Display
_Limit 60
i = Fix(_SndGetPos(Song) * _SndRate) * sz ' Calculate the new sample frame position
Loop
_SndClose Song 'closing the sound releases the mem blocks
_AutoDisplay
GoTo start: