dcsimg
Results 1 to 9 of 9

Thread: Frequency manipulation program. need help!

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2019
    Posts
    25

    Frequency manipulation program. need help!

    HI there

    I have been inspired by a VB6 program I have seen online (written by a man called Petr Supina, all due credit for him for building such a program).

    http://www.planet-source-code.com/vb...01910334560850

    I want to build a program that...
    * Takes any file inputted into the program
    * Reads the binary data of the file
    * Converts each 8 bit long section of binary data in sequence sourced from the input file (therefore 256 different values at a single moment in time) into a different numerical value (between 0-255).
    each 8 bit long binary value has a unique number for each binary value between 0-255. These values are stored inside a MS access database

    so for eg a 8 bit long section of binary from file is 00001001 (9 in decimal) has a textbox with 39 associated with it.

    The binary value for decimal number is 00100111 (as Im sure you know. Im not patronising anyone, just speaking in detail for the sake of clarity)

    Next I want to create a sound wave where 0 binary values have a higher elevation in comparison to 1 binary values (MoDem stuff . Modulation/demodulation) So that the binary value of 39 will have a Sine wave as such


    /LOW\ /HIGH\ /LOW\ /HIGH\
    \LOW/ \LOW/ \HIGH/ \HIGH/


    In summary I want

    ** Input a file
    ** Convert each 8 bit section of binary from input file into its new binary value (represented by decimal number inside each binary value's associated text box
    ** Save the contents of the newly edited file to disk
    ** Then create a singular, continuous sine wave (through soundcard outputted through speaker 3.5 AUX port) that modulates each binary value of the newly created output file at a specified frequency declared inside the program.

    I know how to get vb6 to

    * Read the binary contents of a file
    * Retrieve and save data to and from a MS access database
    * Save/write processed data to a file on HDD

    I dont know how to

    * Take binary data from file and convert it to audio sine wave.

    The program by Petr Supina I know has the capacity to unlock the technical knowledge needed to do this but ATM its above my skill set. Can anyone shed light how I can fulfil the task listed above (convery binary values into sone wave)? Any help I can receive I shall be grateful for. This project means a lot to me!
    Last edited by greenelephant150; Aug 6th, 2019 at 03:43 AM.

  2. #2
    Hyperactive Member Peekay's Avatar
    Join Date
    Sep 2006
    Location
    Witbank, South Africa
    Posts
    344

    Re: Frequency manipulation program. need help!

    Maybe you can start here:

    https://www.researchgate.net/post/Ho..._wave_as_sound

    and break the questions down into segments.

    PK

  3. #3
    Frenzied Member
    Join Date
    Feb 2015
    Posts
    1,351

    Re: Frequency manipulation program. need help!

    Then create a singular, continuous sine wave (through soundcard outputted through speaker 3.5 AUX port) that modulates each binary value of the newly created output file at a specified frequency declared inside the program.
    Can you make a preview image that demonstrates the principle of modulation (what the input and the according output)? I can help you just i don't understand the all correctly.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jul 2019
    Posts
    25

    Re: Frequency manipulation program. need help!

    Sorry for the delay in response

    Say I have a text file which has the followin Hex values

    1F E1 F5 CC C5 54 38 9F BA AD DF F2

    On my vb6 program I have the screen with the following setup

    [label][textbox] [label][textbox] [label][textbox]
    [label][textbox] [label][textbox] [label][textbox]
    [label][textbox] [label][textbox] [label][textbox]
    [label][textbox] [label][textbox] [label][textbox]
    [label][textbox] [label][textbox] [label][textbox]

    each label has a unique numerical digit next to a textbox. therefore

    1 [textbox] 4 [textbox] 7 [textbox]
    2 [textbox] 5 [textbox] 8 [textbox]
    3 [textbox] 6 [textbox] 9 [textbox]

    this is done 255 times.
    Each textbox has a unique number between 1 and 255. Each number is unique to every other textbox number and every unique number in a textbox is not the same as the label number next to the textbox

    My VB6 program takes the original text file (for example) and reads the binary composition of the input file in 8 bit long sections. So the first 8 bit section of our text file example is 1F in HEX (00011111 in binary) The decimal equivalent is 31. So in our form the label '31'has a text box that says (for example) 64. What the vb6 program does is write the binary equivalent of 64 (01000000) to an output file. This process is repeated till the entire file is written.
    I have the experience to complete this without help.

    However what I need help with is this. Taking each 8 bit section of binary from output file and converting it into a singular and continous sine wave. So the binary value of 64 is 01000000 so the sine wave will start with short upperwave and a long downwave. Then the rest of the sine waves (remaining six) will be short sinewaves as they represent the binary value of 0

    How do I convert the output binary file into a singular sine wave with the necessary modulation at a specific frequency outputting from the speaker jack of my soundcard??

    I know I have to use MS DirectSound ActiveX. The http://www.planet-source-code.com/vb...01910334560850 link contains a program that can output a sine wave at a specific frequency but I cant understand the code inside of it. I need help hence why I am here on this forum
    Last edited by greenelephant150; Aug 6th, 2019 at 03:46 AM.

  5. #5
    Frenzied Member
    Join Date
    Feb 2015
    Posts
    1,351

    Re: Frequency manipulation program. need help!

    However what I need help with is this. Taking each 8 bit section of binary from output file and converting it into a singular and continous sine wave. So the binary value of 64 is 01000000 so the sine wave will start with short upperwave and a long upperwave. Then the rest of the sine waves (remaining six) will be short sinewaves as they represent the binary value of 0
    What's the duration of waves? Please make a picture that demonstrates the output waveform. Thanks.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jul 2019
    Posts
    25

    Re: Frequency manipulation program. need help!

    The time duration of the output sine wave from soundcard is directly proportionate to the size of the binary file chosen to be converted to sine wave format. Below is a diagram of an example of a randomly chosen 8 bit long sine wave

    Name:  sine.png
Views: 94
Size:  3.3 KB

    Binary digits from the binary file which are 0 will be of short wave height and binary 1 digits will be represented as a greater wave height. Obviously the sine wave will be longer than 8 waves as the diagram shows above. The sine wave I want to create will be one singular wave comprising of the necessary modulation (wave heights representing 0 and 1s) whose length (sine wave) will be proportionate to the binary size of the binary file. Does this make sense.

    Using MS DirectSound ActiveX control I want to convert the binary of any given file into a singular modulation sine wave (at a specific frequency) with a specific wave height (depending on the binary value at a single moment in time) pulsed through the soundcard speaker jack. Can you help me?
    Last edited by greenelephant150; Aug 6th, 2019 at 03:47 AM.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jul 2019
    Posts
    25

    Re: Frequency manipulation program. need help!

    PS when you mention
    What's the duration of waves?
    If you are referring to Hertz frequency. There will be a textbox where the end user will input a decimal number to represent the frequency in Hertz to be outputted from soundcard

  8. #8
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,663

    Re: Frequency manipulation program. need help!

    greenelephant150,

    I suppose I'll jump in here for a bit. First, it sounds like you're just decrypting sound data, whereas the the original data was coded (encrypted) via some translation table with 256 entries. So, to get the original sound data back, you're going back through the same table, but in reverse.

    First, I'm not sure I'd get an MS-Access database involved in that. With only 256 entries in your translation table, a text file (or some other simple file) would do quite nicely. Also, if this translation table never changed, I'd just hard-code it.

    Ok, moving on ... next, you're talking about playing this sound. Essentially, you're just talking about 8-bit sound. I've never done it, but I'm confident I could do this: A .WAV file can be nothing but a header followed by almost precisely what you outlined (bytes representing values on a sound wave). It sounds like you'd also just want to indicate that it was a MONO (non-Stereo) file as well. In the post just prior, you mentioned the frequency, which must be specified in that .WAV file header. Here's the Wikipedia link for a .WAV file. (Also see this.) There are also more good links at the bottom of that file. Also, I didn't search, but I'd be a bit surprised if there's not some code somewhere in these forums for outputting a .WAV file header, followed by its 8-bit mono data.

    Now, I saw that you said "through soundcard". I could certainly shell to some player to play my .WAV file. If you'd also like to do that entirely with VB6, that also gets a bit tricky. I've done it with MultiMedia calls, but it's been a while, and I no longer use that code. I did find some of that old code and I essentially used the MultiMedia control (MMControl, MCI32.ocx), along with the following two API calls:

    Code:
    
    Private Declare Function waveOutGetVolume Lib "WINMM.DLL" (ByVal uDeviceID As Long, lpdwVolume As Long) As Long
    Private Declare Function waveOutSetVolume Lib "WINMM.DLL" (ByVal uDeviceID As Long, ByVal dwVolume As Long) As Long
    
    

    Also, The Trick knows about all there is to know about Windows, VB6, and playing sounds.

    Maybe some of the ideas I've outlined will help you. Good Luck.

    Elroy

    EDIT1: This looks like a pretty good description of how to output a .WAV file.
    Last edited by Elroy; Aug 5th, 2019 at 10:27 PM.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  9. #9
    Frenzied Member
    Join Date
    Feb 2015
    Posts
    1,351

    Re: Frequency manipulation program. need help!



    Code:
    ' //
    ' // By The trick
    ' // 2019
    ' //
    
    Option Explicit
    
    Private Const WAVE_MAPPER         As Long = -1&
    Private Const CALLBACK_WINDOW     As Long = &H10000
    Private Const WAVE_FORMAT_PCM     As Long = 1
    Private Const MMSYSERR_NOERROR    As Long = 0
    Private Const GWL_WNDPROC         As Long = (-4)
    Private Const MM_WOM_DONE         As Long = &H3BD
    Private Const SAMPLE_RATE         As Long = 44100
    Private Const PI                  As Double = 3.14159265358979
    
    Private Type OPENFILENAME
        lStructSize         As Long
        hwndOwner           As Long
        hInstance           As Long
        lpstrFilter         As Long
        lpstrCustomFilter   As Long
        nMaxCustFilter      As Long
        nFilterIndex        As Long
        lpstrFile           As Long
        nMaxFile            As Long
        lpstrFileTitle      As Long
        nMaxFileTitle       As Long
        lpstrInitialDir     As Long
        lpstrTitle          As Long
        flags               As Long
        nFileOffset         As Integer
        nFileExtension      As Integer
        lpstrDefExt         As Long
        lCustData           As Long
        lpfnHook            As Long
        lpTemplateName      As Long
    End Type
    
    Private Type WAVEFORMATEX
        wFormatTag          As Integer
        nChannels           As Integer
        nSamplesPerSec      As Long
        nAvgBytesPerSec     As Long
        nBlockAlign         As Integer
        wBitsPerSample      As Integer
        cbSize              As Integer
    End Type
    
    Private Type WAVEHDR
        lpData              As Long
        dwBufferLength      As Long
        dwBytesRecorded     As Long
        dwUser              As Long
        dwFlags             As Long
        dwLoops             As Long
        lpNext              As Long
        Reserved            As Long
    End Type
     
    Private Type tBuffer
        bData()             As Byte
        tHeader             As WAVEHDR
        bStatus             As Boolean
    End Type
    
    Private Declare Function GetOpenFileName Lib "comdlg32.dll" _
                             Alias "GetOpenFileNameW" ( _
                             ByRef pOpenfilename As OPENFILENAME) As Long
    Private Declare Function waveOutOpen Lib "winmm.dll" ( _
                             ByRef lphWaveOut As Long, _
                             ByVal uDeviceID As Long, _
                             ByRef lpFormat As WAVEFORMATEX, _
                             ByVal dwCallback As Long, _
                             ByVal dwInstance As Long, _
                             ByVal dwFlags As Long) As Long
    Private Declare Function waveOutPrepareHeader Lib "winmm.dll" ( _
                             ByVal hWaveOut As Long, _
                             ByRef lpWaveOutHdr As WAVEHDR, _
                             ByVal uSize As Long) As Long
    Private Declare Function waveOutWrite Lib "winmm.dll" ( _
                             ByVal hWaveOut As Long, _
                             ByRef lpWaveOutHdr As WAVEHDR, _
                             ByVal uSize As Long) As Long
    Private Declare Function waveOutUnprepareHeader Lib "winmm.dll" ( _
                             ByVal hWaveOut As Long, _
                             ByRef lpWaveOutHdr As WAVEHDR, _
                             ByVal uSize As Long) As Long
    Private Declare Function waveOutClose Lib "winmm.dll" ( _
                             ByVal hWaveOut As Long) As Long
    Private Declare Function waveOutReset Lib "winmm.dll" ( _
                             ByVal hWaveOut As Long) As Long
    Private Declare Function waveOutPause Lib "winmm.dll" ( _
                             ByVal hWaveOut As Long) As Long
    Private Declare Function waveOutRestart Lib "winmm.dll" ( _
                             ByVal hWaveOut As Long) As Long
    Private Declare Function SetWindowLong Lib "user32" _
                             Alias "SetWindowLongW" ( _
                             ByVal hWnd As Long, _
                             ByVal nIndex As Long, _
                             ByVal dwNewLong As Long) As Long
    Private Declare Function CallWindowProc Lib "user32" _
                             Alias "CallWindowProcW" ( _
                             ByVal lpPrevWndFunc As Long, _
                             ByVal hWnd As Long, _
                             ByVal Msg As Long, _
                             ByVal wParam As Long, _
                             ByVal lParam As Long) As Long
    Private Declare Sub memcpy Lib "kernel32" _
                        Alias "RtlMoveMemory" ( _
                        ByRef Destination As Any, _
                        ByRef Source As Any, _
                        ByVal Length As Long)
    
    Private m_bData()       As Byte
    Private m_lDataSize     As Long
    Private m_lCurPos       As Long
    Private m_lCurMask      As Long
    Private m_bIsActive     As Boolean
    Private m_hWaveOut      As Long
    Private m_tBuffer()     As tBuffer
    Private m_pfnPrevProc   As Long
    Private m_bIsFirst      As Boolean
    Private m_dPhase        As Double
    Private m_fSign         As Single
    
    Private Sub NeedNewData( _
                ByRef bData() As Byte)
        Dim dPhaseDelta As Single
        Dim lIndex      As Long
        Dim lBit        As Long
        Dim lLevel      As Long
        Dim fDrawDelta  As Single
        
        If m_lDataSize = 0 Then Exit Sub
        
        fDrawDelta = (UBound(bData) + 1) / picWaveform.ScaleWidth
        dPhaseDelta = GetLogFrequency(sldFrequency.Value) / SAMPLE_RATE * PI * 2
        
        ' // Get bit
        lBit = -CBool(m_bData(m_lCurPos) And m_lCurMask)
        lLevel = (sldLowLevel.Value - sldHighLevel.Value) * lBit + (255 - sldLowLevel.Value)
            
        For lIndex = 0 To UBound(bData)
            
            bData(lIndex) = (Sin(m_dPhase) * m_fSign) * 0.5 * lLevel + 127
            
            m_dPhase = m_dPhase + dPhaseDelta
            
            If m_dPhase > PI Then
            
                m_dPhase = m_dPhase - PI
                m_fSign = -m_fSign
                
                ' // Next bit
                m_lCurMask = m_lCurMask * 2
                
                If m_lCurMask > 128 Then
                    ' // Next byte
                    
                    m_lCurPos = m_lCurPos + 1
                    
                    If m_lCurPos >= m_lDataSize Then
                        ' // From begin again
                        m_lCurPos = 0
                    End If
                    
                    m_lCurMask = 1
                    
                End If
                
                lBit = -CBool(m_bData(m_lCurPos) And m_lCurMask)
                lLevel = (sldLowLevel.Value - sldHighLevel.Value) * lBit + (255 - sldLowLevel.Value)
                
            End If
            
        Next
        
        picWaveform.Cls
        
        For lIndex = 0 To picWaveform.ScaleWidth - 1
            
            If lIndex Then
                picWaveform.Line -(lIndex, bData(lIndex * fDrawDelta))
            Else
                picWaveform.PSet (lIndex, bData(lIndex * fDrawDelta))
            End If
            
        Next
        
    End Sub
    
    Private Function GetLogFrequency( _
                     ByVal lValue As Long) As Double
        GetLogFrequency = (1000 ^ (lValue / 1000) - 1) / 999 * (SAMPLE_RATE / 2)
    End Function
    
    Public Function CallbackWindowProc( _
                    ByVal hWnd As Long, _
                    ByVal lMsg As Long, _
                    ByVal wParam As Long, _
                    ByVal lParam As Long) As Long
        Dim tHeader As WAVEHDR
        Dim lIndex  As Long
        
        Select Case lMsg
        Case MM_WOM_DONE
            
            memcpy tHeader, ByVal lParam, Len(tHeader)
                
            For lIndex = 0 To UBound(m_tBuffer)
                If m_tBuffer(lIndex).tHeader.lpData = tHeader.lpData Then Exit For
            Next
            
            NeedNewData m_tBuffer(lIndex).bData()
            
            waveOutWrite m_hWaveOut, m_tBuffer(lIndex).tHeader, Len(m_tBuffer(lIndex).tHeader)
            
        Case Else
            CallbackWindowProc = CallWindowProc(m_pfnPrevProc, hWnd, lMsg, wParam, lParam)
        End Select
        
    End Function
    
    Private Sub cmdPlay_Click()
        Dim lIndex  As Long
        
        If m_bIsFirst Then
        
            m_bIsFirst = False
                
            For lIndex = 0 To UBound(m_tBuffer)
                
                NeedNewData m_tBuffer(lIndex).bData()
                
                waveOutWrite m_hWaveOut, m_tBuffer(lIndex).tHeader, Len(m_tBuffer(lIndex).tHeader)
                
            Next
                
        Else
            If m_bIsActive Then
                waveOutPause m_hWaveOut
            Else
                waveOutRestart m_hWaveOut
            End If
        End If
        
        m_bIsActive = Not m_bIsActive
        
        If m_bIsActive Then
            cmdPlay.Caption = "Pause"
        Else
            cmdPlay.Caption = "Play"
        End If
        
    End Sub
    
    Private Sub Form_Load()
        
        sldFrequency_Scroll
        
        If Not InitSubclassing() Then Exit Sub
        If Not InitPlayback() Then Exit Sub
        
        mnuOpen.Enabled = True
        
    End Sub
    
    Private Sub Form_Unload( _
                ByRef iCancel As Integer)
        UninitializePlayback
        UninitializeSubclassing
    End Sub
    
    Private Sub mnuOpen_Click()
        Dim sFileName   As String
        Dim iFileNum    As Integer
        
        sFileName = GetOpenFile(Me.hWnd, "Open binary data", "All files" & vbNullChar & "*.*" & vbNullChar)
        If Len(sFileName) = 0 Then Exit Sub
        
        If m_bIsActive Then
            cmdPlay_Click
        End If
        
        m_lCurPos = 0
        m_lCurMask = 1
        m_dPhase = 0
        m_fSign = 1
        iFileNum = FreeFile
        
        Open sFileName For Binary Access Read As iFileNum
        
        m_lDataSize = LOF(iFileNum)
        
        If m_lDataSize > 0 Then
            
            ReDim m_bData(m_lDataSize - 1)
            Get iFileNum, , m_bData()
            
            cmdPlay.Enabled = True
            
        Else
        
            cmdPlay.Enabled = False
            Erase m_bData()
            
        End If
        
        Close iFileNum
        
    End Sub
    
    Private Sub mnuQuit_Click()
        Unload Me
    End Sub
    
    Private Sub sldFrequency_Change()
        sldFrequency_Scroll
    End Sub
    
    Private Sub sldFrequency_Scroll()
        sldFrequency.ToolTipText = Format$(GetLogFrequency(sldFrequency.Value), "0.00") & " Hz."
        lblFrequency.Caption = sldFrequency.ToolTipText
    End Sub
    
    Private Function InitSubclassing() As Boolean
        
        m_pfnPrevProc = SetWindowLong(Me.hWnd, GWL_WNDPROC, AddressOf WindowProc)
        
        InitSubclassing = m_pfnPrevProc
        
    End Function
    
    Private Sub UninitializeSubclassing()
        
        If m_pfnPrevProc Then
            
            SetWindowLong Me.hWnd, GWL_WNDPROC, m_pfnPrevProc
            m_pfnPrevProc = 0
            
        End If
        
    End Sub
    
    Private Function InitPlayback() As Boolean
        Dim tFormat         As WAVEFORMATEX
        Dim lErr            As Long
        Dim lIndex          As Long
        Dim lSamplesCount   As Long
        
        With tFormat
        
            .cbSize = 0
            .wFormatTag = WAVE_FORMAT_PCM
            .wBitsPerSample = 8
            .nSamplesPerSec = SAMPLE_RATE
            .nChannels = 1
            .nBlockAlign = .nChannels * .wBitsPerSample \ 8
            .nAvgBytesPerSec = .nSamplesPerSec * .nBlockAlign
            
        End With
        
        m_bIsFirst = True
        lSamplesCount = 4096
        
        lErr = waveOutOpen(m_hWaveOut, WAVE_MAPPER, tFormat, Me.hWnd, 0, CALLBACK_WINDOW)
        If lErr Then Exit Function
        
        ReDim m_tBuffer(1)
        
        For lIndex = 0 To UBound(m_tBuffer)
            
            With m_tBuffer(lIndex)
            
                ReDim .bData(lSamplesCount * tFormat.nBlockAlign - 1)
    
                .tHeader.lpData = VarPtr(.bData(0))
                .tHeader.dwBufferLength = UBound(.bData) + 1
                
                lErr = waveOutPrepareHeader(m_hWaveOut, .tHeader, Len(.tHeader))
                
                .bStatus = lErr = MMSYSERR_NOERROR
            
            End With
            
            If lErr Then Exit For
            
        Next
        
        If lErr Then
            UninitializePlayback
        Else
            InitPlayback = True
        End If
        
    End Function
    
    Private Sub UninitializePlayback()
        Dim lIndex  As Long
        
        If m_hWaveOut = 0 Then Exit Sub
        
        waveOutReset m_hWaveOut
        
        For lIndex = 0 To UBound(m_tBuffer)
            
            If m_tBuffer(lIndex).bStatus Then
                waveOutUnprepareHeader m_hWaveOut, m_tBuffer(lIndex).tHeader, Len(m_tBuffer(lIndex).tHeader)
            End If
            
        Next
        
        waveOutClose m_hWaveOut
        
        m_hWaveOut = 0
        
    End Sub
    
    Private Function GetOpenFile( _
                     ByVal hWnd As Long, _
                     ByRef sTitle As String, _
                     ByRef sFilter As String) As String
        Dim tOfn            As OPENFILENAME
        Dim strInputFile    As String
        
        With tOfn
        
            .nMaxFile = 260
            strInputFile = String$(.nMaxFile, vbNullChar)
            
            .hwndOwner = hWnd
            .lpstrTitle = StrPtr(sTitle)
            .lpstrFile = StrPtr(strInputFile)
            .lStructSize = Len(tOfn)
            .lpstrFilter = StrPtr(sFilter)
            
            If GetOpenFileName(tOfn) = 0 Then Exit Function
            
            GetOpenFile = Left$(strInputFile, InStr(1, strInputFile, vbNullChar) - 1)
            
        End With
    
    End Function
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width