Sub CSV_To_WAV(none)
    
    Dim csvPath As String, wavPath As String
    
    On Error GoTo ErrHandler
    
    csvPath = Application.GetOpenFilename("CSV Files (*.csv), *.csv", , "Select CSV File")
    If csvPath = "False" Then Exit Sub
    
    wavPath = Application.GetSaveAsFilename("output.wav", "WAV Files (*.wav), *.wav", , "Save WAV File As")
    If wavPath = "False" Then Exit Sub
    
    Dim fileNum As Integer, wavNum As Integer
    Dim samples() As Double
    Dim i As Long, sampleCount As Long
    Dim sampleValue As Double
    Dim dataBytes() As Byte
    
    ' Read CSV samples into array
    Dim temp As String
    Dim values As Variant
    Dim allSamples As Collection
    Set allSamples = New Collection
    
    fileNum = FreeFile
    Open csvPath For Input As #fileNum
        Do While Not EOF(fileNum)
            Line Input #fileNum, temp
            values = Split(temp, ",")
            For i = LBound(values) To UBound(values)
                If IsNumeric(values(i)) Then
                    allSamples.Add CDbl(values(i))
                End If
            Next i
        Loop
    Close #fileNum
    
    sampleCount = allSamples.Count
    If sampleCount = 0 Then
        MsgBox "No numeric samples found in CSV.", vbExclamation
        Exit Sub
    End If
    
    ' Convert samples to 16-bit PCM byte array
    ReDim dataBytes(0 To sampleCount * 2 - 1) As Byte
    For i = 1 To sampleCount
        sampleValue = allSamples(i)
        ' Clamp to -1.0 to 1.0 range
        If sampleValue > 1 Then sampleValue = 1
        If sampleValue < -1 Then sampleValue = -1
        ' Scale to 16-bit signed integer
        Dim intSample As Long
        intSample = CLng(sampleValue * 32767)
        ' Little-endian byte order
        dataBytes((i - 1) * 2) = intSample And &HFF
        dataBytes((i - 1) * 2 + 1) = (intSample \ &H100) And &HFF
    Next i
    
    ' Write WAV file
    wavNum = FreeFile
    Open wavPath For Binary As #wavNum
        Call WriteWavHeader(wavNum, sampleCount, SAMPLE_RATE, NUM_CHANNELS, BITS_PER_SAMPLE)
        Put #wavNum, , dataBytes
    Close #wavNum
    
    MsgBox "WAV file created successfully: " & wavPath, vbInformation
    Exit Sub
    
ErrHandler:
    MsgBox "Error: " & Err.Description, vbCritical
    On Error Resume Next
    Close #fileNum
    Close #wavNum
End Sub

Private Sub WriteWavHeader(ByVal fileNum As Integer, ByVal sampleCount As Long, _
                           ByVal sampleRate As Long, ByVal channels As Integer, _
                           ByVal bitsPerSample As Integer)
    Dim byteRate As Long
    Dim blockAlign As Integer
    Dim subchunk2Size As Long
    Dim chunkSize As Long
    
    blockAlign = channels * (bitsPerSample \ 8)
    byteRate = sampleRate * blockAlign
    subchunk2Size = sampleCount * blockAlign
    chunkSize = 36 + subchunk2Size
    
    ' RIFF header
    Put #fileNum, , "RIFF"
    Put #fileNum, , LongToBytes(chunkSize)
    Put #fileNum, , "WAVE"
    
    ' fmt subchunk
    Put #fileNum, , "fmt "
    Put #fileNum, , LongToBytes(16) ' Subchunk1Size for PCM
    Put #fileNum, , IntToBytes(1)   ' AudioFormat = PCM
    Put #fileNum, , IntToBytes(channels)
    Put #fileNum, , LongToBytes(sampleRate)
    Put #fileNum, , LongToBytes(byteRate)
    Put #fileNum, , IntToBytes(blockAlign)
    Put #fileNum, , IntToBytes(bitsPerSample)
    
    ' data subchunk
    Put #fileNum, , "data"
    Put #fileNum, , LongToBytes(subchunk2Size)
End Sub

Private Function LongToBytes(ByVal value As Long) As Byte()
    Dim b(0 To 3) As Byte
    b(0) = value And &HFF
    b(1) = (value \ &H100) And &HFF
    b(2) = (value \ &H10000) And &HFF
    b(3) = (value \ &H1000000) And &HFF
    LongToBytes = b
End Function

Private Function IntToBytes(ByVal value As Integer) As Byte()
    Dim b(0 To 1) As Byte
    b(0) = value And &HFF
    b(1) = (value \ &H100) And &HFF
    IntToBytes = b
End Function
