VERSION 5.00
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCOMCTL.OCX"
Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
Object = "{0D452EE1-E08F-101A-852E-02608C4D0BB4}#2.0#0"; "FM20.DLL"
Begin VB.Form Binaural 
   BorderStyle     =   1  'Fixed Single
   Caption         =   "Brainwave Synchronizer"
   ClientHeight    =   5790
   ClientLeft      =   4080
   ClientTop       =   3135
   ClientWidth     =   5910
   ControlBox      =   0   'False
   Icon            =   "Binaural.frx":0000
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   ScaleHeight     =   5790
   ScaleWidth      =   5910
   WhatsThisHelp   =   -1  'True
   Begin VB.Frame Frame9 
      Caption         =   "Flasher Options"
      Height          =   2295
      Left            =   120
      TabIndex        =   35
      Top             =   3360
      Width           =   3975
      Begin VB.CheckBox chkFlasherRate 
         Caption         =   "Run flashers at different rate"
         Height          =   375
         Left            =   2160
         TabIndex        =   45
         ToolTipText     =   "Check this to run the flasher bars at a different flashing rate, specified below"
         Top             =   960
         Width           =   1575
      End
      Begin VB.TextBox txtFlasherRate 
         Alignment       =   2  'Center
         BeginProperty DataFormat 
            Type            =   1
            Format          =   "0.000"
            HaveTrueFalseNull=   0
            FirstDayOfWeek  =   0
            FirstWeekOfYear =   0
            LCID            =   1033
            SubFormatType   =   1
         EndProperty
         Height          =   285
         Left            =   2280
         Locked          =   -1  'True
         TabIndex        =   43
         TabStop         =   0   'False
         Text            =   "6"
         ToolTipText     =   "Separate frequency to run the flashers at"
         Top             =   1695
         Width           =   945
      End
      Begin VB.CheckBox chkAlternate 
         Caption         =   "Alternated flashing"
         Height          =   255
         Left            =   120
         TabIndex        =   37
         ToolTipText     =   "Turns on and off beat alternations"
         Top             =   1800
         Value           =   1  'Checked
         Width           =   1695
      End
      Begin VB.HScrollBar ShapeType 
         Height          =   255
         Left            =   1920
         Max             =   5
         TabIndex        =   36
         Top             =   480
         Width           =   1695
      End
      Begin MSComctlLib.Slider Slider4 
         Height          =   375
         Left            =   120
         TabIndex        =   38
         ToolTipText     =   "Sets the size of the flashing bars, based on the percent of the screen"
         Top             =   480
         Width           =   1695
         _ExtentX        =   2990
         _ExtentY        =   661
         _Version        =   393216
         Min             =   1
         Max             =   100
         SelStart        =   10
         TickFrequency   =   10
         Value           =   10
      End
      Begin MSComctlLib.Slider Slider3 
         Height          =   375
         Left            =   120
         TabIndex        =   39
         ToolTipText     =   "Sets the size of the flashing bars, based on the percent of the screen"
         Top             =   1200
         Width           =   1695
         _ExtentX        =   2990
         _ExtentY        =   661
         _Version        =   393216
         Min             =   1
         Max             =   100
         SelStart        =   100
         TickFrequency   =   10
         Value           =   100
      End
      Begin VB.Label lblFlasherRate 
         Alignment       =   2  'Center
         Caption         =   "Flasher rate:"
         Height          =   255
         Left            =   2280
         TabIndex        =   46
         Top             =   1440
         Width           =   1095
      End
      Begin MSForms.SpinButton spinFlasherRate 
         Height          =   300
         Left            =   3225
         TabIndex        =   44
         ToolTipText     =   "Change flasher frequency"
         Top             =   1680
         Width           =   165
         Size            =   "291;529"
         Min             =   1
         Max             =   350
         Position        =   60
      End
      Begin VB.Label lblBarWidth 
         Alignment       =   2  'Center
         Caption         =   "Bar Width (percent):"
         Height          =   255
         Left            =   120
         TabIndex        =   42
         Top             =   240
         Width           =   1695
      End
      Begin VB.Label lblBarHeight 
         Alignment       =   2  'Center
         Caption         =   "Bar Height (percent):"
         Height          =   255
         Left            =   120
         TabIndex        =   41
         Top             =   960
         Width           =   1695
      End
      Begin VB.Label Label3 
         Alignment       =   2  'Center
         Caption         =   "Shape Type:"
         Height          =   255
         Left            =   1920
         TabIndex        =   40
         Top             =   240
         Width           =   1695
      End
   End
   Begin VB.OptionButton Option3 
      Caption         =   "100"
      Height          =   255
      Left            =   2760
      TabIndex        =   8
      ToolTipText     =   "Choose how much you want the frequency changer arrows above to change by"
      Top             =   2040
      Width           =   615
   End
   Begin VB.OptionButton Option2 
      Caption         =   "10"
      Height          =   255
      Left            =   2160
      TabIndex        =   7
      ToolTipText     =   "Choose how much you want the frequency changer arrows above to change by"
      Top             =   2040
      Width           =   615
   End
   Begin VB.OptionButton Option1 
      Caption         =   "1"
      Height          =   255
      Left            =   1560
      TabIndex        =   6
      ToolTipText     =   "Choose how much you want the frequency changer arrows above to change by"
      Top             =   2040
      Value           =   -1  'True
      Width           =   615
   End
   Begin VB.TextBox txtSetFreq1 
      Alignment       =   2  'Center
      BeginProperty DataFormat 
         Type            =   1
         Format          =   "0.000"
         HaveTrueFalseNull=   0
         FirstDayOfWeek  =   0
         FirstWeekOfYear =   0
         LCID            =   1033
         SubFormatType   =   1
      EndProperty
      Height          =   285
      Left            =   240
      Locked          =   -1  'True
      TabIndex        =   31
      TabStop         =   0   'False
      Text            =   "159"
      ToolTipText     =   "Right ear frequency"
      Top             =   360
      Width           =   945
   End
   Begin VB.CommandButton Command6 
      Caption         =   "Flashers Off"
      Height          =   375
      Left            =   4440
      TabIndex        =   16
      ToolTipText     =   "Turn sample flashers off"
      Top             =   4680
      Width           =   1215
   End
   Begin VB.CommandButton Command5 
      Caption         =   "Flashers On"
      Height          =   375
      Left            =   4440
      TabIndex        =   15
      ToolTipText     =   "Turn sample flashers on"
      Top             =   4320
      Width           =   1215
   End
   Begin VB.Frame Frame8 
      Caption         =   "Flasher Visuals"
      Height          =   3135
      Left            =   3480
      TabIndex        =   26
      Top             =   120
      Width           =   2295
      Begin VB.CommandButton Command4 
         Caption         =   "Set Color 2"
         Height          =   375
         Left            =   1200
         TabIndex        =   14
         Top             =   2640
         Width           =   975
      End
      Begin VB.CommandButton Command3 
         Caption         =   "Set Color 2"
         Height          =   375
         Left            =   120
         TabIndex        =   13
         Top             =   2640
         Width           =   975
      End
      Begin VB.CommandButton Command2 
         Caption         =   "Set Color 1"
         Height          =   375
         Left            =   1200
         TabIndex        =   12
         Top             =   2160
         Width           =   975
      End
      Begin VB.CommandButton Command1 
         Caption         =   "Set Color 1"
         Height          =   375
         Left            =   120
         TabIndex        =   11
         Top             =   2160
         Width           =   975
      End
      Begin VB.Shape Shape2 
         BackColor       =   &H00FFFFFF&
         BorderColor     =   &H00000000&
         BorderStyle     =   0  'Transparent
         FillStyle       =   0  'Solid
         Height          =   1575
         Left            =   1440
         Top             =   360
         Width           =   495
      End
      Begin VB.Shape Shape1 
         BackColor       =   &H00000000&
         BackStyle       =   1  'Opaque
         BorderColor     =   &H00000000&
         BorderStyle     =   0  'Transparent
         FillStyle       =   0  'Solid
         Height          =   1575
         Left            =   360
         Top             =   360
         Width           =   495
      End
   End
   Begin VB.HScrollBar scrToneVol 
      Height          =   255
      LargeChange     =   500
      Left            =   1680
      Max             =   0
      Min             =   -5000
      SmallChange     =   50
      TabIndex        =   2
      Top             =   360
      Value           =   -2000
      Width           =   1575
   End
   Begin VB.Frame Frame1 
      Caption         =   "Frequency 1"
      Height          =   615
      Left            =   120
      TabIndex        =   17
      Top             =   120
      Width           =   1335
      Begin MSForms.SpinButton SpinButton1 
         Height          =   300
         Left            =   1065
         TabIndex        =   1
         ToolTipText     =   "Change frequency 1"
         Top             =   225
         Width           =   165
         Size            =   "291;529"
         Min             =   50
         Max             =   40000
         Position        =   159
      End
   End
   Begin VB.Frame Frame4 
      Caption         =   "Frequency 2"
      Height          =   615
      Left            =   120
      TabIndex        =   19
      Top             =   720
      Width           =   1335
      Begin VB.TextBox txtSetFreq2 
         Alignment       =   2  'Center
         BeginProperty DataFormat 
            Type            =   1
            Format          =   "0.000"
            HaveTrueFalseNull=   0
            FirstDayOfWeek  =   0
            FirstWeekOfYear =   0
            LCID            =   1033
            SubFormatType   =   1
         EndProperty
         Height          =   285
         Left            =   120
         Locked          =   -1  'True
         TabIndex        =   20
         TabStop         =   0   'False
         Text            =   "165"
         ToolTipText     =   "Left ear frequency"
         Top             =   240
         Width           =   945
      End
      Begin MSForms.SpinButton SpinButton2 
         Height          =   300
         Left            =   1065
         TabIndex        =   3
         ToolTipText     =   "Change frequency 2"
         Top             =   225
         Width           =   165
         Size            =   "291;529"
         Min             =   50
         Max             =   40000
         Position        =   165
      End
   End
   Begin VB.Frame Frame3 
      Caption         =   "Masking"
      Height          =   615
      Left            =   120
      TabIndex        =   23
      Top             =   1320
      Width           =   1335
      Begin VB.TextBox txtMask 
         Alignment       =   2  'Center
         CausesValidation=   0   'False
         Height          =   285
         Left            =   120
         Locked          =   -1  'True
         TabIndex        =   25
         TabStop         =   0   'False
         Text            =   "Inverse Pink"
         ToolTipText     =   "Preselected Mask (background noise)"
         Top             =   240
         Width           =   1095
      End
   End
   Begin VB.Frame Frame6 
      Caption         =   "Function"
      Height          =   735
      Left            =   120
      TabIndex        =   22
      Top             =   2550
      Width           =   3255
      Begin VB.CommandButton Command7 
         Caption         =   "Help"
         Height          =   375
         Left            =   1680
         TabIndex        =   32
         ToolTipText     =   "Brainwave synchronizer help"
         Top             =   240
         Width           =   615
      End
      Begin VB.CommandButton cmdGenerate 
         Caption         =   "Test"
         Height          =   375
         Left            =   240
         TabIndex        =   0
         ToolTipText     =   "Click to Generate"
         Top             =   240
         Width           =   615
      End
      Begin VB.CommandButton cmdStop 
         Caption         =   "Stop"
         Height          =   375
         Left            =   960
         TabIndex        =   9
         ToolTipText     =   "Click to Pause or Stop"
         Top             =   240
         Width           =   615
      End
      Begin VB.CommandButton cmdExit 
         Caption         =   "Exit"
         Height          =   375
         Left            =   2400
         TabIndex        =   10
         ToolTipText     =   "Click to Exit Config Screen"
         Top             =   240
         Width           =   615
      End
   End
   Begin VB.Frame Frame2 
      Caption         =   "Tone Volume"
      Height          =   615
      Left            =   1560
      TabIndex        =   18
      Top             =   120
      Width           =   1815
   End
   Begin VB.Frame Frame5 
      Caption         =   "Tone Separation"
      Height          =   615
      Left            =   1560
      TabIndex        =   21
      Top             =   720
      Width           =   1815
      Begin VB.HScrollBar scrBalance 
         Height          =   255
         LargeChange     =   500
         Left            =   120
         Max             =   5000
         SmallChange     =   100
         TabIndex        =   4
         Top             =   240
         Value           =   5000
         Width           =   1575
      End
   End
   Begin VB.Frame Frame7 
      Caption         =   "Mask Volume"
      Height          =   615
      Left            =   1560
      TabIndex        =   24
      Top             =   1320
      Width           =   1815
      Begin VB.HScrollBar scrMaskVol 
         Height          =   255
         LargeChange     =   500
         Left            =   120
         Max             =   0
         Min             =   -6000
         SmallChange     =   50
         TabIndex        =   5
         Top             =   240
         Value           =   -2000
         Width           =   1575
      End
   End
   Begin VB.Timer Timer1 
      Enabled         =   0   'False
      Left            =   3240
      Top             =   0
   End
   Begin MSComDlg.CommonDialog CommonDialog1 
      Left            =   0
      Top             =   0
      _ExtentX        =   847
      _ExtentY        =   847
      _Version        =   393216
   End
   Begin VB.Timer Timer3 
      Enabled         =   0   'False
      Left            =   0
      Top             =   600
   End
   Begin MSComctlLib.Slider Slider1 
      Height          =   375
      Left            =   1560
      TabIndex        =   34
      ToolTipText     =   "Selects between one and two flasher bars"
      Top             =   2280
      Width           =   1695
      _ExtentX        =   2990
      _ExtentY        =   661
      _Version        =   393216
      LargeChange     =   1
      Min             =   1
      Max             =   2
      SelStart        =   2
      Value           =   2
   End
   Begin VB.Timer Timer2 
      Interval        =   100
      Left            =   0
      Top             =   3000
   End
   Begin VB.Timer ActTimer 
      Enabled         =   0   'False
      Interval        =   100
      Left            =   0
      Top             =   2520
   End
   Begin VB.Label Label5 
      Alignment       =   2  'Center
      BorderStyle     =   1  'Fixed Single
      Caption         =   "6"
      Height          =   255
      Left            =   4680
      TabIndex        =   28
      Top             =   3600
      Width           =   735
   End
   Begin VB.Label Label1 
      Alignment       =   2  'Center
      Caption         =   "# of flasher bars:"
      Height          =   255
      Left            =   120
      TabIndex        =   33
      Top             =   2280
      Width           =   1335
   End
   Begin VB.Label Label8 
      Alignment       =   2  'Center
      Caption         =   "Increase freq by:"
      Height          =   255
      Left            =   120
      TabIndex        =   30
      Top             =   2040
      Width           =   1335
   End
   Begin VB.Label Label7 
      Alignment       =   2  'Center
      Caption         =   "Type"
      Height          =   240
      Left            =   4320
      TabIndex        =   29
      ToolTipText     =   "Type of binaural beat"
      Top             =   3960
      Width           =   1455
   End
   Begin VB.Label Label4 
      Alignment       =   2  'Center
      Caption         =   "Binaural Beat Rate:"
      Height          =   255
      Left            =   4320
      TabIndex        =   27
      Top             =   3360
      Width           =   1455
   End
End
Attribute VB_Name = "Binaural"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'Virtual Hypnotist
'©2003-2008 FollowTheWatch Software
'http://vhypno.sourceforge.net
'http://sourceforge.net/projects/vhypno
'Contact - followthewatch55@yahoo.com

'This program is free software; you can redistribute it and/or
'modify it under the terms of the GNU General Public License
'as published by the Free Software Foundation; either version 2
'of the License, or (at your option) any later version.

'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'GNU General Public License for more details.

'You should have received a copy of the GNU General Public License
'along with this program; if not, write to the Free Software
'Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

'--------------------------------------
'A program to generate binaural beats for brainwave
'synchronization. The difference between the tones should
'fall within the ranges of 1 to 3 Hz (Delta or sleep state),
'3 to 6 Hz (Theta or deep meditative state) or 6 to 12 Hz
'(Alpha or relaxed alert state). Pink noise masking is used
'to enhance the brainwave entrainment. Volume levels should
'be set fairly low for use with headphones.

'Contrary to popular opinion the brainwave entrainment occurs
'even when used with speakers at higher volume levels.

'Put on your phones, sit back and relax....
'Just don't blame me if you're late for work tomorrow ;-)

'Originally by Michael Herbert at runes2@concentric.net on 2/23/2002
'Found at http://www.a1vbcode.com/coderedirect.asp?type=application&ID=1516
'Original version with my bugfixes is on the VH website

Option Explicit

Private dx As New DirectX8
Private ds As DirectSound8
Private dsBuffer(3) As DirectSoundSecondaryBuffer8
Private bufferDesc(3) As DSBUFFERDESC
Private freq1 As Double
Private freq2 As Double
Private freq3 As Double
Private playing As Boolean
Private Fatal As Boolean
Private Loader As Boolean
Private ErrorSource As String
Private ErrorDesc As String
Private ErrorNumber As Long
Public OldFreq1 As Double
Public OldFreq2 As Double
Public DoNotPlay As Boolean

Private Type Stereo
    l As Integer
    r As Integer
End Type

Private Sub Init()
If ExceptionHandlers = True Then On Error GoTo ErrorHandler
If BWFailed = True Then Exit Sub
Set ds = dx.DirectSoundCreate("")
ds.SetCooperativeLevel Me.hwnd, DSSCL_PRIORITY
Exit Sub

ErrorHandler:
ErrorDesc = err.Description
ErrorNumber = err.Number
err.Source = "Brainwave Synchronizer - Init"
Fatal = True
Call BinErrorReport
End Sub

Public Sub Start_Gen()
Binaural.Visible = False
If BWFailed = True Then Exit Sub
If (dsBuffer(0) Is Nothing) Then Call Init
Call cmdGenerate_Click
End Sub

Public Sub Stop_Gen()
Call cmdExit_Click
Binaural.Hide
End Sub

Public Sub PlayTone(Frequency As Double, Duration As Long)
If ExceptionHandlers = True Then On Error GoTo ErrorHandler

If (dsBuffer(0) Is Nothing) Then Call Init
freq3 = Frequency
SineWave 3
    
dsBuffer(3).SetVolume GrockVolume
dsBuffer(3).Play DSBPLAY_LOOPING

Timer3.Interval = Duration
Timer3.Enabled = True

Exit Sub
ErrorHandler:
ErrorDesc = err.Description
ErrorNumber = err.Number
err.Source = "Brainwave Synchronizer - PlayTone"
Call BinErrorReport
End Sub

Private Sub ActTimer_Timer()
ActTimer.Enabled = False
If Abs(Val(txtSetFreq2 - txtSetFreq1)) <> 0 And BWFailed = False Then
    freq1 = Val(txtSetFreq1.Text)
    freq2 = Val(txtSetFreq2.Text)
    SineWave 0
    SineWave 1
    Play
End If
End Sub

Private Sub cmdExit_Click()
If ExceptionHandlers = True Then On Error GoTo ErrorHandler
Timer1.Enabled = False

If BWFailed = True Then
    Me.Hide
    Exit Sub
End If

If (dsBuffer(0) Is Nothing) Then Cleanup: Me.Hide: Exit Sub
   
    dsBuffer(0).Stop
    dsBuffer(1).Stop
    If Not (dsBuffer(2) Is Nothing) Then
        dsBuffer(2).Stop
    End If
    If Not (dsBuffer(3) Is Nothing) Then
        dsBuffer(3).Stop
    End If
    Call Cleanup
    Me.Hide
Exit Sub
ErrorHandler:
ErrorDesc = err.Description
ErrorNumber = err.Number
err.Source = "Brainwave Synchronizer - cmdExit"
Call BinErrorReport
End Sub

Private Sub Cleanup()
If ExceptionHandlers = True Then On Error GoTo ErrorHandler
    If Not (dsBuffer(0) Is Nothing) Then dsBuffer(0).Stop
    If Not (dsBuffer(1) Is Nothing) Then dsBuffer(1).Stop
    If Not (dsBuffer(2) Is Nothing) Then dsBuffer(2).Stop
    If Not (dsBuffer(3) Is Nothing) Then dsBuffer(3).Stop
    Set dsBuffer(0) = Nothing
    Set dsBuffer(1) = Nothing
    Set dsBuffer(2) = Nothing
    Set dsBuffer(3) = Nothing
    Set ds = Nothing
    Set dx = Nothing
Exit Sub
ErrorHandler:
ErrorDesc = err.Description
ErrorNumber = err.Number
err.Source = "Brainwave Synchronizer - Cleanup"
Call BinErrorReport
End Sub

Private Sub Command1_Click()
Dim i As Long
CommonDialog1.Color = ShapeAColor1
CommonDialog1.flags = cdlCCFullOpen Or cdlCCRGBInit
CommonDialog1.ShowColor
i = CommonDialog1.Color
If i <> -1 Then ShapeAColor1 = i
End Sub

Private Sub Command2_Click()
Dim i As Long
CommonDialog1.Color = ShapeBColor1
CommonDialog1.flags = cdlCCFullOpen Or cdlCCRGBInit
CommonDialog1.ShowColor
i = CommonDialog1.Color
If i <> -1 Then ShapeBColor1 = i
End Sub

Private Sub Command3_Click()
Dim i As Long
CommonDialog1.Color = ShapeAColor2
CommonDialog1.flags = cdlCCFullOpen Or cdlCCRGBInit
CommonDialog1.ShowColor
i = CommonDialog1.Color
If i <> -1 Then ShapeAColor2 = i
End Sub

Private Sub Command4_Click()
Dim i As Long
CommonDialog1.Color = ShapeBColor2
CommonDialog1.flags = cdlCCFullOpen Or cdlCCRGBInit
CommonDialog1.ShowColor
i = CommonDialog1.Color
If i <> -1 Then ShapeBColor2 = i
End Sub

Private Sub Command5_Click()
Timer1.Enabled = True
End Sub

Private Sub Command6_Click()
Timer1.Enabled = False
End Sub

Private Sub Command7_Click()
If ExceptionHandlers = True Then On Error GoTo ErrorHandler
Shell "hh " + App.path + "\vh.chm::/brainwave.html", vbNormalFocus
Exit Sub

ErrorHandler:
err.Source = "Brainwave Synchronizer - Command7"
   Call ErrorReport("An error occurred while loading Windows HTML Help.", vbExclamation)
  
End Sub

Public Sub SetValues()
If ExceptionHandlers = True Then On Error GoTo ErrorHandler
scrToneVol.value = SetVolume
scrBalance.value = PanVal
scrMaskVol.value = SetMVolume
DoNotPlay = True
SpinButton1.value = Frequency1
SpinButton2.value = Frequency2
DoNotPlay = False
Slider4.value = BarWidth
Exit Sub

ErrorHandler:
err.Source = "Brainwave Synchronizer - SetValues"
   Call ErrorReport("An error occurred while changing the control values.", vbExclamation)
End Sub

Private Sub Form_Activate()
If Loader = True Then Exit Sub
Call SetValues
Call Command5_Click
If BWFailed = True Then Exit Sub
If (dsBuffer(0) Is Nothing) Then Call Init
End Sub

Private Sub Form_Load()
    Loader = True
    Me.Left = (ScreenX / 2) - (Me.Width / 2)
    Me.top = (ScreenY / 2) - (Me.Height / 2)
    'If Loading = False Then Me.Show
    Call Init
    Loader = False
End Sub

Private Sub Form_Unload(Cancel As Integer)
If Fatal = False Then Call Cleanup
End Sub

Private Sub Option1_Click()
SpinButton1.SmallChange = 1
SpinButton2.SmallChange = 1
End Sub

Private Sub Option2_Click()
SpinButton1.SmallChange = 10
SpinButton2.SmallChange = 10
End Sub

Private Sub Option3_Click()
SpinButton1.SmallChange = 100
SpinButton2.SmallChange = 100
End Sub

Private Sub scrBalance_Scroll()
Call scrBalance_Change
End Sub

Private Sub scrMaskVol_Scroll()
Call scrMaskVol_Change
End Sub

Private Sub scrToneVol_Scroll()
Call scrToneVol_Change
End Sub

Private Sub Slider1_Change()
If Me.Visible = True Then
    If Slider1.value = 1 Then
        chkAlternate.value = 0
    Else
        chkAlternate.value = 1
    End If
End If
End Sub

Private Sub Slider4_Change()
BarWidth = Slider4.value
End Sub

Private Sub SpinButton1_Change()
If Loading = False And playing = True And DoNotPlay = False Then
    ActTimer.Enabled = False
    ActTimer.Enabled = True
End If
txtSetFreq1.Text = LTrim$(Str$(SpinButton1.value))
Frequency1 = SpinButton1.value
End Sub

Private Sub SpinButton2_Change()
If Loading = False And playing = True And DoNotPlay = False Then
    ActTimer.Enabled = False
    ActTimer.Enabled = True
End If
txtSetFreq2.Text = LTrim$(Str$(SpinButton2.value))
Frequency2 = SpinButton2.value
End Sub

Private Sub spinFlasherRate_Change()
txtFlasherRate.Text = spinFlasherRate.value / 10
End Sub

Private Sub Timer1_Timer()
If ShapeData = False Then
    Shape1.FillColor = ShapeAColor1
    If Slider1.value = 2 Then Shape2.FillColor = ShapeBColor1
    ShapeData = True
    Exit Sub
End If

If ShapeData = True Then
    Shape1.FillColor = ShapeAColor2
    If Slider1.value = 2 Then Shape2.FillColor = ShapeBColor2
    ShapeData = False
    Exit Sub
End If
End Sub

Private Sub Timer2_Timer()
If IsNumeric(txtSetFreq2.Text) = False Then Exit Sub
If IsNumeric(txtSetFreq1.Text) = False Then Exit Sub
On Error Resume Next
Dim OutFreq As Integer
OutFreq = Abs(Val(txtSetFreq2.Text) - Val(txtSetFreq1))
Label5.Caption = OutFreq
If chkFlasherRate.value = 0 Then
    If chkAlternate.value = 1 Then
        Timer1.Interval = 1000 / OutFreq
    Else
        Timer1.Interval = 1000 / (OutFreq * 2)
    End If
Else
    If chkAlternate.value = 1 Then
        Timer1.Interval = 1000 / (spinFlasherRate.value / 10)
    Else
        Timer1.Interval = 1000 / ((spinFlasherRate.value / 10) * 2)
    End If
End If
If OutFreq > 0 And OutFreq <= 4 Then Label7.Caption = "Type: Delta"
If OutFreq > 4 And OutFreq <= 8 Then Label7.Caption = "Type: Theta"
If OutFreq > 8 And OutFreq <= 14 Then Label7.Caption = "Type: Alpha"
If OutFreq > 14 Then Label7.Caption = "Type: Beta"
If Slider1.value = 1 Then
    'disable second flasher buttons
    Command2.Enabled = False
    Command4.Enabled = False
    Shape2.FillColor = RGB(0, 0, 0)
Else
    'enable second flasher buttons
    Command2.Enabled = True
    Command4.Enabled = True
End If
Shape1.Shape = ShapeType.value
Shape2.Shape = ShapeType.value
End Sub

Private Sub scrBalance_Change()
If ExceptionHandlers = True Then On Error GoTo ErrorHandler
PanVal = scrBalance.value
If BWFailed = True Then Exit Sub
If (dsBuffer(0) Is Nothing) Then Exit Sub
If Loading = False And playing = True Then
    dsBuffer(0).SetPan PanVal
    dsBuffer(1).SetPan PanVal * -1
End If
Exit Sub
ErrorHandler:
ErrorDesc = err.Description
ErrorNumber = err.Number
err.Source = "Brainwave Synchronizer - scrBalance"
Call BinErrorReport
End Sub

Private Sub scrToneVol_Change()
If ExceptionHandlers = True Then On Error GoTo ErrorHandler
SetVolume = scrToneVol.value
If BWFailed = True Then Exit Sub
If (dsBuffer(0) Is Nothing) Then Exit Sub
If Loading = False And playing = True Then
    dsBuffer(0).SetVolume SetVolume
    dsBuffer(1).SetVolume SetVolume
End If
Exit Sub
ErrorHandler:
ErrorDesc = err.Description
ErrorNumber = err.Number
err.Source = "Brainwave Synchronizer - scrToneVol"
Call BinErrorReport
End Sub

Private Sub scrMaskVol_Change()
If ExceptionHandlers = True Then On Error GoTo ErrorHandler
SetMVolume = scrMaskVol.value
If BWFailed = True Then Exit Sub
If (dsBuffer(0) Is Nothing) Then Exit Sub
If Loading = False And playing = True Then
    If Not (dsBuffer(2) Is Nothing) Then
        dsBuffer(2).SetVolume SetMVolume
    End If
End If
Exit Sub
ErrorHandler:
ErrorDesc = err.Description
ErrorNumber = err.Number
err.Source = "Brainwave Synchronizer - scrMaskVol"
Call BinErrorReport
End Sub

Private Sub cmdGenerate_Click()
    If BWFailed = True Then Exit Sub
    freq1 = Val(txtSetFreq1.Text)
    If freq1 < 10 Or freq1 > 600 Then
        MsgBox "Frequency must be within 10 to 600 Hz range."
        Exit Sub
    End If
    SineWave 0
    freq2 = Val(txtSetFreq2.Text)
    If freq2 < 10 Or freq2 > 600 Then
        MsgBox "Frequency must be within 10 to 600 Hz range."
        Exit Sub
    End If
    If Val(Label5.Caption) < 1 Then
        MsgBox "Frequency 2 must be greater than Frequency 1."
        Exit Sub
    End If
    SineWave 1
    Play
End Sub

Private Sub Play()
    If ExceptionHandlers = True Then On Error GoTo ErrorHandler
    If BWFailed = True Then Exit Sub
    playing = True
    
    PanVal = scrBalance.value
    dsBuffer(0).SetPan PanVal
    dsBuffer(1).SetPan PanVal * -1
    dsBuffer(0).SetVolume SetVolume
    dsBuffer(0).Play DSBPLAY_LOOPING
    dsBuffer(1).SetVolume SetVolume
    dsBuffer(1).Play DSBPLAY_LOOPING

    bufferDesc(2).lFlags = DSBCAPS_CTRLVOLUME Or DSBCAPS_CTRLPAN Or DSBCAPS_STATIC Or DSBCAPS_GLOBALFOCUS
    Set dsBuffer(2) = ds.CreateSoundBufferFromFile(App.path & "\pink_inv.wav", bufferDesc(2))
    If Not (dsBuffer(2) Is Nothing) Then
        dsBuffer(2).SetVolume SetMVolume
        dsBuffer(2).Play DSBPLAY_LOOPING
    End If
Exit Sub
ErrorHandler:
ErrorDesc = err.Description
ErrorNumber = err.Number
err.Source = "Brainwave Synchronizer - Play"
Call BinErrorReport
End Sub
Private Sub BinErrorReport()
BWFailed = True
Call ErrorReport("An error occurred in the binaural brainwave generator subsystem. Shutting off the brainwave generator.", vbExclamation)
If Fatal = False Then Call Stop_Gen
Me.Hide
End Sub
Private Sub cmdStop_Click()
If ExceptionHandlers = True Then On Error GoTo ErrorHandler
If BWFailed = True Then Exit Sub
playing = False
If (dsBuffer(0) Is Nothing) Then Exit Sub
    dsBuffer(0).Stop
    dsBuffer(1).Stop
    If Not (dsBuffer(2) Is Nothing) Then
        dsBuffer(2).Stop
    End If
    If Not (dsBuffer(3) Is Nothing) Then
        dsBuffer(3).Stop
    End If
Exit Sub
ErrorHandler:
ErrorDesc = err.Description
ErrorNumber = err.Number
err.Source = "Brainwave Synchronizer - cmdStop"
Call BinErrorReport
End Sub

Private Sub SineWave(Number As Integer)
If ExceptionHandlers = True Then On Error GoTo ErrorHandler
Dim TwoPI#, X%, Y%, LY%
Dim Buf(4800) As Stereo
Dim freq As Double

With bufferDesc(Number).fxFormat
    .nFormatTag = WAVE_FORMAT_PCM: .nChannels = 2 'Stereo
    .nBitsPerSample = 16: .lSamplesPerSec = 48000
    .nBlockAlign = .nChannels * .nBitsPerSample \ 8
    .lAvgBytesPerSec = .nBlockAlign * .lSamplesPerSec

    If Number = 0 Then freq = freq1
    If Number = 1 Then freq = freq2
    If Number = 3 Then freq = freq3
    
    TwoPI = 8 * Atn(1): freq = TwoPI / .lSamplesPerSec * freq
    For X = 0 To UBound(Buf) 'Create sine wave inside a buffer
        Y = 32767 * Sin(X * freq)
        If LY < 0 And Y >= 0 Then Exit For
        LY = Y: Buf(X).l = Y: Buf(X).r = Y
    Next X

    bufferDesc(Number).lBufferBytes = X * .nBlockAlign
    bufferDesc(Number).lFlags = DSBCAPS_CTRLVOLUME Or DSBCAPS_CTRLPAN Or DSBCAPS_STATIC Or DSBCAPS_GLOBALFOCUS
    Set dsBuffer(Number) = ds.CreateSoundBuffer(bufferDesc(Number))
    dsBuffer(Number).WriteBuffer 0, X * .nBlockAlign, Buf(0).l, DSBLOCK_DEFAULT
End With
    
Exit Sub
ErrorHandler:
ErrorDesc = err.Description
ErrorNumber = err.Number
err.Source = "Brainwave Synchronizer - SineWave"
Call BinErrorReport
End Sub

Private Sub Timer3_Timer()
Timer3.Enabled = False
If ExceptionHandlers = True Then On Error GoTo ErrorHandler
    dsBuffer(3).Stop
    
Exit Sub
ErrorHandler:
ErrorDesc = err.Description
ErrorNumber = err.Number
err.Source = "Brainwave Synchronizer - Timer3"
Call BinErrorReport
End Sub
