Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tesla Coil That Dances With Your Music
#1
Just like the real thing. Smile 

Put one of your sound files, like a .mp3 file, in the same folder as this. When it starts it asks for the file name, and you will also need the ending (like .mp3). Then it will play the song
and shoot lightning in the frequencies of the music. After it ends it will go back to asking for a song name again. 

Tell me what you think. I got almost all the code from the Wiki Help page with an example of sound frequency display here: https://qb64phoenix.com/qb64wiki/index.php/MEMSOUND 
I just turned the frequency waves into lightning bolts pretty much on a rod. 

(Code deleted, scroll down to my next code I posted.)
Reply
#2
(09-01-2022, 11:19 PM)SierraKen Wrote: Just like the real thing. Smile 

Put one of your sound files, like a .mp3 file, in the same folder as this. When it starts it asks for the file name, and you will also need the ending (like .mp3). Then it will play the song
and shoot lightning in the frequencies of the music. After it ends it will go back to asking for a song name again. 

Tell me what you think. I got almost all the code from the Wiki Help page with an example of sound frequency display here: https://qb64phoenix.com/qb64wiki/index.php/MEMSOUND 
I just turned the frequency waves into lightning bolts pretty much on a rod. 

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:

Cool effect.
(Of course I had to test it on Billy Thorpe's "Children of the Sun"!)
Reply
#3
Smile Thanks, glad you like it.
Reply
#4
Kool!
I ran Beethoven's Fifth on it, and it seemed to handle the size ok. Is there a limit set in there somewhere?
The display is a bit low on the screen on my computer, so I lose part of the lower display - maybe raise it 100px or so?
It would be great if colours could be added too, but it's great as it is.
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#5
Hmm I'll try to raise it a little bit. I know a tiny bit goes off the screen. I'll add some colors too but a Tesla Coil doesn't really have a lot of colors.
Reply
#6
OK here we go! Thank you for telling me those things because it looks a lot better now. 

(Code deleted, below is added filenames support on my next post.)
Reply
#7
(09-02-2022, 02:33 AM)SierraKen Wrote: Hmm I'll try to raise it a little bit. I know a tiny bit goes off the screen. I'll add some colors too but a Tesla Coil doesn't really have a lot of colors.
Great!
I think the colours help a lot. Look on it as poetic license (or Coder's license) !!
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#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
#9
good job. i tested the first code because the other one depends on windows.
Reply
#10
Impressive job. It handled Morricone's "Ecstacy of Gold" just fine.
DO: LOOP: DO: LOOP
sha_na_na_na_na_na_na_na_na_na:
Reply




Users browsing this thread: 2 Guest(s)