1. Do not share user accounts! Any account that is shared by another person will be blocked and closed. This means: we will close not only the account that is shared, but also the main account of the user who uses another person's account. We have the ability to detect account sharing, so please do not try to cheat the system. This action will take place on 04/18/2023. Read all forum rules.
    Dismiss Notice
  2. For downloading SimTools plugins you need a Download Package. Get it with virtual coins that you receive for forum activity or Buy Download Package - We have a zero Spam tolerance so read our forum rules first.

    Buy Now a Download Plan!
  3. Do not try to cheat our system and do not post an unnecessary amount of useless posts only to earn credits here. We have a zero spam tolerance policy and this will cause a ban of your user account. Otherwise we wish you a pleasant stay here! Read the forum rules
  4. We have a few rules which you need to read and accept before posting anything here! Following these rules will keep the forum clean and your stay pleasant. Do not follow these rules can lead to permanent exclusion from this website: Read the forum rules.
    Are you a company? Read our company rules

VB 2010 Tone Generator library for buttkicker support?

Discussion in 'Miscellaneous' started by RaceRay, Oct 7, 2013.

  1. RaceRay

    RaceRay Administrator Staff Member SimAxe Beta Tester

    Joined:
    Nov 8, 2006
    Messages:
    4,656
    Occupation:
    Self-employed | Web and application development
    Location:
    Hamburg, Germany
    Balance:
    23,842Coins
    Ratings:
    +1,969 / 13 / -0
    My Motion Simulator:
    2DOF, DC motor, SimAxe, SimforceGT
    Hey guys,

    is someone here with experience in sound generating via VB?
    If we find some working sample code it should be easy to get buttkicker support.

    i am helpful for any recommend resource.

    Cheers, René
    • Like Like x 3
  2. RacingMat

    RacingMat Well-Known Member Gold Contributor

    Joined:
    Feb 22, 2013
    Messages:
    2,234
    Location:
    Marseille - FRANCE
    Balance:
    20,896Coins
    Ratings:
    +2,084 / 21 / -2
    My Motion Simulator:
    2DOF, DC motor, Arduino
    I'm willing to help!!
    - Is it VB or VB.net?

    Here is the beginning of a specification list:
    - in real time,
    - converts telemetry data into a tone
    - volume control
    - frequency control
    - the code should generate "wave sound" and not play ".wav" files
    - to external sound speakers

    For example, simple "Beep" function do not fit: it has frequency control but doesn't have control volume and uses only internal speakers
    ;-)
    • Like Like x 1
  3. RaceRay

    RaceRay Administrator Staff Member SimAxe Beta Tester

    Joined:
    Nov 8, 2006
    Messages:
    4,656
    Occupation:
    Self-employed | Web and application development
    Location:
    Hamburg, Germany
    Balance:
    23,842Coins
    Ratings:
    +1,969 / 13 / -0
    My Motion Simulator:
    2DOF, DC motor, SimAxe, SimforceGT
    Hi Mat,

    i had hope that you are able to help:)
    dustin already found out that beep is not working, not possible to use peep more than one concurrent at same time...it sucks:)
    So i see that you already paid some attention to it.

    We are talking about Visual Basic 2010.
  4. RaceRay

    RaceRay Administrator Staff Member SimAxe Beta Tester

    Joined:
    Nov 8, 2006
    Messages:
    4,656
    Occupation:
    Self-employed | Web and application development
    Location:
    Hamburg, Germany
    Balance:
    23,842Coins
    Ratings:
    +1,969 / 13 / -0
    My Motion Simulator:
    2DOF, DC motor, SimAxe, SimforceGT
    - we basicly need a app with 5 hor scroll bars on it
    - each one turns up the volume of its note
    - and each one can have a starting frequency and a ending frequency
  5. yobuddy

    yobuddy Well-Known Member Staff Member Moderator SimAxe Beta Tester SimTools Developer Gold Contributor

    Joined:
    Feb 9, 2007
    Messages:
    5,168
    Occupation:
    Computer Technician
    Location:
    Portland, Oregon - USA
    Balance:
    48,262Coins
    Ratings:
    +5,038 / 16 / -0
    VB.net would be the preferred language.

    -theory
    The way I see it, is when the app is started, any tones that are need will startup with a zero for volume, and then they get altered on the fly. But I’m sure there are other ways.

    -my ideas
    I see an app that had 5 horizontal sliders representing the volume of each ‘tone’.
    Then each slider has a start and stop frequency so it scales with the volume.

    For RPM,
    Start frequency of 50
    End frequency of 150
    As the volume get louder the frequency also increases causing that rev feeling. :thumbs

    For Bumps,
    I sample the heave at 10ms and compare the old with the new value, if the value is over a limit value a bump has occurred!
    Start frequency of 250
    End frequency of 100
    As the volume get louder the frequency decreases causing the small bumps to sound like pebbles bouncing off the car, but bug bumps a deeper thud!

    -help? :cheers
    I just need a way to generate a stable tone that does not flicker every time I change the frequency or volume.
    Can someone make a simple app that selects a sound card and plays 5 tones with sliders?
    I could take it from there I think.
    yobuddy
  6. Historiker

    Historiker Dramamine Adict Gold Contributor

    Joined:
    Dec 16, 2010
    Messages:
    2,158
    Occupation:
    Retired
    Location:
    Michigan USA
    Balance:
    9,176Coins
    Ratings:
    +2,159 / 19 / -1
    My Motion Simulator:
    3DOF, DC motor, Arduino, Motion platform, 6DOF
    Wow, thanks guys for working on this!

    I have been trying to come up with an alternative to just connecting my bass shakers to the same stereo outputs as my speakers. The obvious issue with that is all bass sounds are reproduced regardless of the source (so no music playing while I drive my big rig, lol).

    Not sure if this helps at all but I did find a way to split a sound source (in this case the game) to output to two different sound devices. I was going to create a post about this later today and I think I still will for those needing to do so.

    I used VB-Audio Cable (virtual device driver) that can take up to two inputs, split them to several outputs if you have the right software.

    http://vb-audio.pagesperso-orange.fr/Cable/index.htm

    The right software is called Voicemeeter (also by VB-Audio) which takes the input and splits it between two different outputs (physical devices). So now I can adjust each output individually so my speakers have just enough bass to sound good and my other sound card can output only to my bass shakers and is adjustable.

    http://vb-audio.pagesperso-orange.fr/Voicemeeter/index.htm

    Again, I do not know if this information helps at all with this discussion but I thought I would share.

    Again, keep up the great work!

    EDIT: Forgot to mention that the software is Donationware. If this works for me, even in the short term, I plan on contributing.
  7. RacingMat

    RacingMat Well-Known Member Gold Contributor

    Joined:
    Feb 22, 2013
    Messages:
    2,234
    Location:
    Marseille - FRANCE
    Balance:
    20,896Coins
    Ratings:
    +2,084 / 21 / -2
    My Motion Simulator:
    2DOF, DC motor, Arduino
    Hello!

    Some data : the frequency range of buttkickers(r) is 5Hz to 200 Hz

    frequency range.GIF

    May I ask why 5 "channels"?
    is it 1 central for engine ? and 4 for 4 wheels?

    I have not understood everything in the approach :eek::
    - for the engine:
    The vibration frequency is given by the telemetry: it's the round per minute RPM!

    The vibration amplitude (vibration force) should be stronger for low rpm and weaker for high rpm.
    As the vibration amplitude is driven by the volume, the volume has to vary opppositely to frequence, no?

    - for bumps:
    I would suggest that road bumps would be acquired like this: the variations around the mean value are the signal to be transducted by buttkicker.

    bumps signal for buttkicker.png

    In both cases, the cuts (start or end frequency) would distort the reality...

    PS: I really love posting images on this forum :D
    • Agree Agree x 1
  8. Historiker

    Historiker Dramamine Adict Gold Contributor

    Joined:
    Dec 16, 2010
    Messages:
    2,158
    Occupation:
    Retired
    Location:
    Michigan USA
    Balance:
    9,176Coins
    Ratings:
    +2,159 / 19 / -1
    My Motion Simulator:
    3DOF, DC motor, Arduino, Motion platform, 6DOF




    I realize that I have no experience with programming (remember I recently did my first "Hello World"? ;) ) but I have been searching for help on this issue.

    A very old post (2005) has a code created by a user in the VB Forums which generates a wave file from an input but the author also said that "You can have it make a wave file, and play the wave file, or if you use DirectSound, you can play directly without saving to wav...[to a device]"

    Does this help at all or should I just go bugger off now? lol :D

    I really want this project to work.

    EDIT: Forgot to include original references:

    Found first mention on post #7 here:

    http://www.vbforums.com/showthread....y-Generation-HOW&highlight=VB+Tone+Generation

    Code and discussion found here:

    http://www.vbforums.com/showthread.php?356482-create-wav-file&highlight=frequency

    Code:
        Option Explicit
     
        Private Type tWAVEFORMATEX
            wFormatTag As Integer
            nChannels As Integer
            nSamplesPerSec As Long
            nAvgBytesPerSec As Long
            nBlockAlign As Integer
            wBitsPerSample As Integer
            cbSize As Integer
            ExtraData(1 To 32) As Byte ' makes the structure 50 bytes long
        End Type
     
        Private Type FileHeader
            lRiff As Long
            lFileSize As Long
            lWave As Long
            lFormat As Long
            lFormatLength As Long
        End Type
     
        Private Type WaveFormat
            wFormatTag As Integer
            nChannels As Integer
            nSamplesPerSec As Long
            nAvgBytesPerSec As Long
            nBlockAlign As Integer
            wBitsPerSample As Integer
        End Type
     
        Private Type ChunkHeader
            lType As Long
            lLen As Long
        End Type
     
        Private Sub Form_Load()
            Dim Buff(0 To 44100) As Integer
       
            GenerateTone 440, Buff, 1
       
            SaveWaveFile "C:\test_Wave.wav", Buff
        End Sub
     
        Private Sub GenerateTone(ByVal Frequency As Single, IntBuff() As Integer, Optional Amplitude As Single = 1, Optional SamplesPerSec As Long = 44100, Optional Startpos As Long = 0, Optional Length As Long = -1)
            Dim K As Long, V1 As Double
            Const PI As Double = 3.14159265358979
     
            V1 = SamplesPerSec / (PI * 2 * Frequency)
       
            If Length = -1 Then Length = UBound(IntBuff) - Startpos
       
            For K = Startpos To Startpos + Length
                IntBuff(K) = CInt(Fix(Sin(K / V1) * (32766.5 * Amplitude)))
            Next K
        End Sub
     
        Private Sub SaveWaveFile(ByVal WaveFileName As String, ByRef Buffer() As Integer, Optional SamplesPerSec As Long = 44100)
            Dim WF As tWAVEFORMATEX
       
            WF.wFormatTag = 1 'WAVE_FORMAT_PCM
            WF.nChannels = 1
            WF.wBitsPerSample = 16
            WF.nSamplesPerSec = SamplesPerSec
       
            WF.nBlockAlign = (WF.wBitsPerSample * WF.nChannels) \ 8
            WF.nAvgBytesPerSec = WF.nSamplesPerSec * WF.nBlockAlign
       
            Open WaveFileName For Binary Access Write Lock Write As #1
                WaveWriteHeader 1, WF
                Put #1, , Buffer
                WaveWriteHeaderEnd 1
            Close #1
        End Sub
     
        Private Sub WaveWriteHeader(ByVal OutFileNum As Integer, WaveFmt As tWAVEFORMATEX)
            Dim header As FileHeader
            Dim HdrFormat As WaveFormat
            Dim chunk As ChunkHeader
       
            With header
                .lRiff = &H46464952 ' "RIFF"
                .lFileSize = 0
                .lWave = &H45564157 ' "WAVE"
                .lFormat = &H20746D66 ' "fmt "
                .lFormatLength = Len(HdrFormat)
            End With
       
            With HdrFormat
                .wFormatTag = WaveFmt.wFormatTag
                .nChannels = WaveFmt.nChannels
                .nSamplesPerSec = WaveFmt.nSamplesPerSec
                .nAvgBytesPerSec = WaveFmt.nAvgBytesPerSec
                .nBlockAlign = WaveFmt.nBlockAlign
                .wBitsPerSample = WaveFmt.wBitsPerSample
            End With
       
            chunk.lType = &H61746164 ' "data"
            chunk.lLen = 0
       
            Put #OutFileNum, 1, header
            Put #OutFileNum, , HdrFormat
            Put #OutFileNum, , chunk
        End Sub
     
        Private Sub WaveWriteHeaderEnd(ByVal OutFileNum As Integer)
            Dim header As FileHeader
            Dim HdrFormat As WaveFormat
            Dim chunk As ChunkHeader
            Dim Lng As Long
       
            Lng = LOF(OutFileNum)
            Put #OutFileNum, 5, Lng
       
            Lng = LOF(OutFileNum) - (Len(header) + Len(HdrFormat) + Len(chunk))
            Put #OutFileNum, Len(header) + Len(HdrFormat) + 5, Lng
        End Sub
  9. RaceRay

    RaceRay Administrator Staff Member SimAxe Beta Tester

    Joined:
    Nov 8, 2006
    Messages:
    4,656
    Occupation:
    Self-employed | Web and application development
    Location:
    Hamburg, Germany
    Balance:
    23,842Coins
    Ratings:
    +1,969 / 13 / -0
    My Motion Simulator:
    2DOF, DC motor, SimAxe, SimforceGT
    • Like Like x 2
  10. RaceRay

    RaceRay Administrator Staff Member SimAxe Beta Tester

    Joined:
    Nov 8, 2006
    Messages:
    4,656
    Occupation:
    Self-employed | Web and application development
    Location:
    Hamburg, Germany
    Balance:
    23,842Coins
    Ratings:
    +1,969 / 13 / -0
    My Motion Simulator:
    2DOF, DC motor, SimAxe, SimforceGT
    Very cool. It works. Attached you find a compiled exe and the source code to generate a sinus with 100 hz.

    Put the following code into a empty form:

    Attached Files:

    Last edited: Oct 7, 2013
  11. RaceRay

    RaceRay Administrator Staff Member SimAxe Beta Tester

    Joined:
    Nov 8, 2006
    Messages:
    4,656
    Occupation:
    Self-employed | Web and application development
    Location:
    Hamburg, Germany
    Balance:
    23,842Coins
    Ratings:
    +1,969 / 13 / -0
    My Motion Simulator:
    2DOF, DC motor, SimAxe, SimforceGT
    Ton generator with slider like Dustin asked - That should be good for a first starting.

    Edit: Fixed source

    Attached Files:

    Last edited: Oct 8, 2013
  12. RacingMat

    RacingMat Well-Known Member Gold Contributor

    Joined:
    Feb 22, 2013
    Messages:
    2,234
    Location:
    Marseille - FRANCE
    Balance:
    20,896Coins
    Ratings:
    +2,084 / 21 / -2
    My Motion Simulator:
    2DOF, DC motor, Arduino
    Yes, very nice piano, René! :) I feel like Mozart!

    What's your opinion about the Heave - mean value proposal?

    What have you in mind?

    BTW after a short while, I got an error "System.OverflowException" with Win Seven and vista! :-(
    Code:
    System.OverflowException:
    WindowsApplication1.Form1.SoundGenerator.AppendSine(Double frequency, Int16 amplitude, TimeSpan duration) dans C:\Users\IronMan\Desktop\tonegenerator2\Tonegenerator\Tonegenerator\Form1.vb:ligne 173
      à WindowsApplication1.Form1.TrackBar1_Scroll(Object sender, EventArgs e) dans C:\Users\IronMan\Desktop\tonegenerator2\Tonegenerator\Tonegenerator\Form1.vb:ligne 286
  13. eaorobbie

    eaorobbie Well-Known Member SimTools Developer Gold Contributor

    Joined:
    May 26, 2009
    Messages:
    2,574
    Occupation:
    CAD Detailer
    Location:
    Ellenbrook, Western Australia
    Balance:
    20,401Coins
    Ratings:
    +1,683 / 23 / -2
    My Motion Simulator:
    2DOF, DC motor, JRK, SimforceGT, 6DOF
    OMG this has great potential, mmm with rpm from the dash out, we should be able to drive this output.
    Now working back from what RacingMat has stated a sub woofers range is 5-200Hz that would be 200Hz at lowest rpm and increase to 5Hz on the maxium Rpm or I might have that backwards let all take a look.
  14. RacingMat

    RacingMat Well-Known Member Gold Contributor

    Joined:
    Feb 22, 2013
    Messages:
    2,234
    Location:
    Marseille - FRANCE
    Balance:
    20,896Coins
    Ratings:
    +2,084 / 21 / -2
    My Motion Simulator:
    2DOF, DC motor, Arduino
    I keep trying detailing the awaiting results,
    I hope this help :)

    App output:
    For each effect, a sound signal is produced.
    Two sound output parameters have to be defined by the app:
    - sound frequency
    - and sound volume.​

    For the engine effect:
    The vibration frequency is given by the telemetry: it's the round per minute RPM!
    Sound Freq (Hz) = Rpm / 60
    Engine Freq.jpg it's in the range of buttkicker

    The vibration amplitude (vibration force) should be stronger for low rpm and weaker for high rpm.
    As the vibration amplitude is driven by the volume, the sound volume has to vary opppositely to frequence.
    Highest volume for lowest RPM...
    (Min RPM and max RPM should be defined)​

    For Road bumps effects:
    Frequency: if Heave is sampled at constant rate,
    we can store previous Hdata(t-2) and Hdata(t-1),
    acquire Hdata(t),
    and we can compute BumpFrequency(t) as Hdata(t) - Hcomputed(t)
    like this Heave Mean2.jpg
    or smoother with a moving average over 3 periods for example heave mean3.jpg

    Volume:
    like RPM, the sound volume has to vary opppositely to frequence.​
    • Like Like x 3
  15. RaceRay

    RaceRay Administrator Staff Member SimAxe Beta Tester

    Joined:
    Nov 8, 2006
    Messages:
    4,656
    Occupation:
    Self-employed | Web and application development
    Location:
    Hamburg, Germany
    Balance:
    23,842Coins
    Ratings:
    +1,969 / 13 / -0
    My Motion Simulator:
    2DOF, DC motor, SimAxe, SimforceGT
    Interesting stuff...have to examine that more! The code i posted is not usable because there was no way to have multiple streams. Also there are cracks when the single tones are played multiple times in a loop so i decided to look for a real software synthesizer which is able to make tons in "realtime".
    I found a library which is able to load Soundfonts. This are (midi) patterns which can played with various settings.
    Library is called FluidSynth.

    A Soundfont is something like a set of pattern from a specific instrument like a e-Bass, drums, synthesizer etc. Unfortunately i did not found a clean sinus wave as a Soundfont (Its able to create such a sounds with several audio tools but i tried several instrument patterns and the result is very good imho, much better than a clean sinus wave.

    I wrote a small quick and dirty testapp for the Soundfonts possibilities.
    Download here (22mb): http://www.xsimulator.net/download/tonegenerator.zip
    tone.PNG

    Attention: Some sounds, specially the one with deep frequencies are very dangerous for your speaker. So tune them down:)

    Go in the code and look for .sf2. There you can try out different instrument patterns.:

    Code:
     ' load soundfont files
            ' Thats a bass guitar
            'Dim path As String = "sounds\BassFing.sf2"
            ' Thats a Piano
            Dim path As String = "sounds\gorts.sf2"
            'Thats a Drum Kit
            'Dim path As String = "sounds\StomperSet.sf2"
            'Thats a synthesizer
            'Dim path As String = "sounds/198_Juno_StringPad.sf2"
            'Dim path As String = ""
    As there are real instrument pattern played and not only steril and clean sinus waves its very easy to create realistic engine vibrations:

    Try out (gorts soundfont):
    • Total play time = 5sec
    • Duration of tone in ms = 48ms - 100ms
    • freq / tone = 20 (thats the lowest adjustement of the slider)

    If we want very clean sounds we only have to create some sinus soundfonts.
    Alternatively i think a similar result can be reached with sinus wave samples. I deliver some of them in the download package.


    Btw. Every note is played separately. The volume can vary with every tone.

    The slider freq / tone plays the different tones which are on a bank most times roughly from 20 - 60 less or more. So its possible that you select a soundfont and there is no ton when u set the slider to 1-35. So u have to try out to find the range of tones on the virtual keyboard bank,

    One thing is still missing in the testapp:
    Selection of a specific soundcard. Per default the MS audio driver dsound is used (primary one - can only be changed when the FluidSynth dll are recompiled. )
    But its possible to write the sound into stream and choose than the audio card we want:

    From: http://fluidsynth.sourceforge.net/api/index.html#MIDIPlayer
    Some resources:
    http://sourceforge.net/apps/trac/fluidsynth/wiki/SoundFont
    http://fluidsynthwrapper.codeplex.com/wikipage?title=Samples&referringTitle=Home

    Download Soundfonts:
    http://soundfonts.homemusician.net/
    http://hammersound.net/cgi-bin/soundlink.pl

    About Midi Keymapping, notes and frequencies (german)
    http://www.sengpielaudio.com/Rechner-notennamen.htm

    If someone can deliver better code, please go ahead.
    • Like Like x 1
    Last edited: Oct 11, 2013
  16. eaorobbie

    eaorobbie Well-Known Member SimTools Developer Gold Contributor

    Joined:
    May 26, 2009
    Messages:
    2,574
    Occupation:
    CAD Detailer
    Location:
    Ellenbrook, Western Australia
    Balance:
    20,401Coins
    Ratings:
    +1,683 / 23 / -2
    My Motion Simulator:
    2DOF, DC motor, JRK, SimforceGT, 6DOF
    Ok here is a basic app that works in rpm for rfactor only as a test app.
    Dash output needs to be set to 50ms in Game Manager, not sure how to reduce this yet.

    This doesnt include the new information from Raceray, bugger need to revise

    Attached Files:

  17. RacingMat

    RacingMat Well-Known Member Gold Contributor

    Joined:
    Feb 22, 2013
    Messages:
    2,234
    Location:
    Marseille - FRANCE
    Balance:
    20,896Coins
    Ratings:
    +2,084 / 21 / -2
    My Motion Simulator:
    2DOF, DC motor, Arduino
    Hey! "Soundfonts": I used it for a tabla player application (coded with Delphiyears ago)! :) very rich sounds!
    yes, it's (quite) easy to play soundfont sounds called by a midi format.
    But for our msecond real time needs, it may be tricky calling sounds or applying affect?...

    This morning, I continued my searches:
    DirectSound is complex
    Windows Multimedia API should do the job

    Here is a nice (short) explanation about Signal Synthesis
    And I looked for VB sine wave generator (without wav file) code: here are the results (zip files uploaded).

    http://www.planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=63252&lngWId=1
    "A Simple Sine Wave Generator (DirectSound)"
    __________
    http://forums.codeguru.com/showthread.php?409239-Direct-Soundcard-Control
    WaveManip.zip
    ___________
    I hope the first example is fine (I haven't VB2010 to test :eek:)

    Attached Files:

  18. RacingMat

    RacingMat Well-Known Member Gold Contributor

    Joined:
    Feb 22, 2013
    Messages:
    2,234
    Location:
    Marseille - FRANCE
    Balance:
    20,896Coins
    Ratings:
    +2,084 / 21 / -2
    My Motion Simulator:
    2DOF, DC motor, Arduino
    Great! Great! I'll test tonight!
    Thanks :grin
  19. Historiker

    Historiker Dramamine Adict Gold Contributor

    Joined:
    Dec 16, 2010
    Messages:
    2,158
    Occupation:
    Retired
    Location:
    Michigan USA
    Balance:
    9,176Coins
    Ratings:
    +2,159 / 19 / -1
    My Motion Simulator:
    3DOF, DC motor, Arduino, Motion platform, 6DOF
    Arg...have to go install a counter top upstate tonight so I cannot play around with it until Sunday.
  20. eaorobbie

    eaorobbie Well-Known Member SimTools Developer Gold Contributor

    Joined:
    May 26, 2009
    Messages:
    2,574
    Occupation:
    CAD Detailer
    Location:
    Ellenbrook, Western Australia
    Balance:
    20,401Coins
    Ratings:
    +1,683 / 23 / -2
    My Motion Simulator:
    2DOF, DC motor, JRK, SimforceGT, 6DOF
    I haven't worked out yet how to send it to a different sound card , so its really just a proof of the concept that it might actually work.
    Im sure that there is a free vb10 on the mircosoft site that might work for compiling and editing.