Tesla Coil That Dances With Your Music
#8
I also just added a way to see all of the .mp3 files on the current directory by just pressing Enter without typing anything at the start. It brings up a Notepad list of your mp3 files in that directory. When you are finished you will have to close the Notepad as well. It saves it as a temp.dir file in that directory which you can delete anytime if you wish. 
I tried to run .wav, .mid, and .flac but for some reason none of those would play on this. So I just kept with mp3. 

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$
Print "Just press Enter to bring up list of your mp3 files in Notepad."
Print
Input "Song Filename Here: ", file$
If file$ = "" Then
    Shell _Hide Chr$(34) + "dir *.MP3" + Chr$(34) + "/b > temp.dir"
    Shell "start Notepad temp.dir" ' display temp file contents in Notepad window
    GoTo start:
End If

DefLng A-Z
Option _Explicit
Option _ExplicitArray
Dim bolts As Single, bolts2 As Single, t As Single
Dim c1 As Single, c2 As Single, c3 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:
            c1 = 255 * Rnd
            c2 = 255 * Rnd
            c3 = 255 * Rnd
            For bolts2 = 1 To 3
                For bolts = 1 To t
                    Line (400, 350)-(400 + 300 * si / 32768, (y / 2) + 100), _RGB32(c1, c2, c3)
                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:
            c1 = 255 * Rnd
            c2 = 255 * Rnd
            c3 = 255 * Rnd
            For bolts2 = 1 To 3
                For bolts = 1 To t
                    Line (400, 350)-(400 + sf * 300, (y / 2) + 100), _RGB32(c1, c2, c3)
                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:
            c1 = 255 * Rnd
            c2 = 255 * Rnd
            c3 = 255 * Rnd
            For bolts2 = 1 To 3
                For bolts = 1 To t
                    Line (400, 350)-(400 + 300 * si / 32768, (y / 2) + 100), _RGB32(c1, c2, c3)
                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:
Reply


Messages In This Thread
RE: Tesla Coil That Dances With Your Music - by SierraKen - 09-02-2022, 04:04 AM



Users browsing this thread: 6 Guest(s)