QB64 Phoenix Edition
Tesla Coil That Dances With Your Music - Printable Version

+- QB64 Phoenix Edition (https://staging.qb64phoenix.com)
+-- Forum: QB64 Rising (https://staging.qb64phoenix.com/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://staging.qb64phoenix.com/forumdisplay.php?fid=3)
+---- Forum: Programs (https://staging.qb64phoenix.com/forumdisplay.php?fid=7)
+---- Thread: Tesla Coil That Dances With Your Music (/showthread.php?tid=838)

Pages: 1 2


RE: Tesla Coil That Dances With Your Music - SierraKen - 09-02-2022

Thanks guys!

To run it in other systems other than windows, try removing the following code up on top, and also where it says you can see the file list in Notepad if you just press Enter. You will have to know the song filename.


Code: (Select All)
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



RE: Tesla Coil That Dances With Your Music - SierraKen - 09-02-2022

Here is a picture of the Tesla Coil in action. I'm going to make a YouTube of it in action because photos don't do it justice. I'll use one of the computer songs I made many years ago so I won't have copyright problems on YouTube.


[Image: Tesla-Coil-by-Sierra-Ken.jpg]


RE: Tesla Coil That Dances With Your Music - SierraKen - 09-02-2022

Here is a 25 minute video I just made and uploaded using music I made around 15 years ago on this Tesla Coil simulation. You don't have to watch it all of course but I thought I would put the whole thing on there because it goes together well. After you click the video, wait about 1 or 2 minutes to let the program load the large song file first.

https://www.youtube.com/watch?v=9FACSukS694


RE: Tesla Coil That Dances With Your Music - SMcNeill - 09-05-2022

A couple of people have reported that this isn't working in the new version 3.1 in QB64-PE.  As I explained there, the glitch is two-fold:

First, the old version of QB64-PE was glitched.  The OpenAL backend *always* converted things down to mono-sound, so this program was always running under the 'integer mono TYPE 0 block of the IF statement in it.  The new audio backend doesn't have that glitch (we fixed it), and now you get your stereo sound and such to play, whereas it never did before!  YAY!!

Second, this program has a slight glitch in how it checks against the mem.TYPE values.

Here, it checks to see if the mem.TYPE = 1  or if the mem.TYPE = 4.

Code: (Select All)
    If (sz = 4 Or sz = 2) And SampleData.TYPE = 1 Then ' integer stereo or mono
         ...stuff
    ElseIf (sz = 8 Or sz = 4) And SampleData.TYPE = 4 Then ' floating point stereo or mono

This isn't what the values actually are.  MEM values are built upon the various parts of the flags which make up the type which the mem block contains.

For example, if the data length is 4 bytes, then it sets the 3rd bit to TRUE.  (which &B100 has a value of 4)
Now, if this data is an INTEGER TYPE, then it sets bit 7 to TRUE.  (which &B1000000 has a value of 128)
If the data is a FLOATING POINT TYPE, then it sets bit 8 to TRUE.  (and &B10000000 has a value of 256)

So, if our data type is a LONG, it'd have a value of 132.  (bit 3 set and bit 7 set, making it an integer type which takes uses 4 bytes of memory)
If the data type was a SINLGE, it'd have a value of 260.  (bit 3 set and bit 8 set, making it a floating point type which uses 4 bytes of memory

So the values you're looking for here isn't EQUAL 1 or EQUAL 4 -- they're AND 128 (integer type) or AND 256 (floating point type)...

Code: (Select All)
    If (sz = 4 Or sz = 2) And (SampleData.TYPE AND 128) Then ' integer stereo or mono
        ...stuff
    ElseIf (sz = 8 Or sz = 4) And (SampleData.TYPE AND 256) Then ' floating point stereo or mono


It's a minor glitch in the code which was never triggered or found because the old audio backend was glitched itself and only ever produced a TYPE 0 response.  To work properly with the new backend, and to have stereo sound playing and all, these checks need to be ANDed instead of EQUALed.  Wink


RE: Tesla Coil That Dances With Your Music - SierraKen - 09-05-2022

Thanks Steve. I changed the lines you talked about. I still don't know hardly anything about this, but if there is anything else I should change, please tell me. You also might want to update the Wiki page where I found this example code at, which is here: https://qb64phoenix.com/qb64wiki/index.php/MEMSOUND

Here is the update: 

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
    If (sz = 4 Or sz = 2) And (SampleData.TYPE And 128) Then
        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
    ElseIf (sz = 8 Or sz = 4) And (SampleData.TYPE And 256) Then
        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: