Author Topic: How to write into wav-file?[WASAPI]  (Read 86 times)

AudioFan

  • Posts: 18
How to write into wav-file?[WASAPI]
« on: 13 Oct '17 - 08:45 »
Edit: Solved ;D

I try'd it with BassEnc, first only a WAv-File was created, but it was still empty (0kB), now i found the Error, sometimes used IntPtr.Zero for some Arguments, now using Nothing for these and it Works.


Hi,

i'm tryin' to Make a Recorder for Loopback-Devices with basswasapi in VB.Net. In the example "basswasapi24.zip/vb/rectest" the Source is in VB6, i don't want use the the APIs like CopyMemory GlobalAlloc etc... to write into a wav-file. It is possible to use BassEnc.Start_Encoding here? When yes how to do? It seems this Code is working ok, without write into file.
Code: [Select]
Imports Un4seen.Bass
Imports Un4seen.BassWasapi
Imports Un4seen.Bass.AddOn.Mix
Imports Un4seen.Bass.AddOn.Enc

Public Class Form1

    Private Class Dev
        Public Property Index As Integer
        Public Property Name As String
    End Class

    Private Devs As New List(Of Dev)
    Private Loaded As Boolean = False

    Private Indev As Integer
    Private Inmixer As Integer

    Private Outdev As Integer
    Private Outmixer As Integer

    Private Instream As Integer
    Private Outstream As Integer

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        While BassWasapi.BASS_WASAPI_Free()
        End While
        Bass.BASS_Free()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Bass.BASS_SetConfig(BASSConfig.BASS_CONFIG_UPDATETHREADS, 0)
        Bass.BASS_Init(0, 48000, 0, IntPtr.Zero, Guid.Empty)
        Dim dInfo As New BASS_WASAPI_DEVICEINFO
        Dim devCount As Integer = BassWasapi.BASS_WASAPI_GetDeviceCount
        Dim tmp As Integer = -1
        For i = -1 To devCount - 1
            BassWasapi.BASS_WASAPI_GetDeviceInfo(i, dInfo)
            If ((dInfo.flags And BASSWASAPIDeviceInfo.BASS_DEVICE_INPUT) = BASSWASAPIDeviceInfo.BASS_DEVICE_INPUT AndAlso (dInfo.flags And BASSWASAPIDeviceInfo.BASS_DEVICE_ENABLED) = BASSWASAPIDeviceInfo.BASS_DEVICE_ENABLED) Then
                Devs.Add(New Dev() With {.Index = i, .Name = dInfo.name})
                If ((dInfo.flags And BASSWASAPIDeviceInfo.BASS_DEVICE_DEFAULT) = BASSWASAPIDeviceInfo.BASS_DEVICE_DEFAULT) Then ' it's the default
                    Indev = i
                    tmp = Devs.Count - 1
                End If
            End If
        Next
        cbbDevices.DataSource = Devs
        cbbDevices.DisplayMember = "Name"
        cbbDevices.SelectedIndex = tmp
        If BassWasapi.BASS_WASAPI_Init(-1, 0, 0, 0, 0.4, 0.05, AddressOf OutWasapiProc, IntPtr.Zero) Then
            Dim wInfo As New BASS_WASAPI_INFO
            Outdev = BassWasapi.BASS_WASAPI_GetDevice()
            BassWasapi.BASS_WASAPI_GetInfo(wInfo)
            Outmixer = BassMix.BASS_Mixer_StreamCreate(wInfo.freq, wInfo.chans, BASSFlag.BASS_SAMPLE_FLOAT Or BASSFlag.BASS_STREAM_DECODE Or BASSFlag.BASS_MIXER_END Or BASSFlag.BASS_MIXER_POSEX)
        End If
        Loaded = True
        InitInputDevice()
    End Sub

    Private Function InWasapiProc(ByVal buffer As IntPtr, ByVal length As Integer, ByVal user As IntPtr) As Integer
        Bass.BASS_StreamPutData(Instream, buffer, length)
        Debug.WriteLine(Date.Now)
        Return 1
    End Function

    Private Function OutWasapiProc(ByVal buffer As IntPtr, ByVal length As Integer, ByVal user As IntPtr) As Integer
        Dim c As Integer = Bass.BASS_ChannelGetData(Outmixer, buffer, length)
        If (c < 0) Then
            If (BassWasapi.BASS_WASAPI_GetData(IntPtr.Zero, BASSData.BASS_DATA_AVAILABLE) = 0) Then
                BassWasapi.BASS_WASAPI_Stop(False)
            End If
            Return 0
        End If
        Return c
    End Function

    Private Sub cbbDevices_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbbDevices.SelectedIndexChanged
        If Loaded Then
            BassWasapi.BASS_WASAPI_SetDevice(Indev)
            BassWasapi.BASS_WASAPI_Free()
            Bass.BASS_StreamFree(Instream)
            Instream = 0
            Indev = DirectCast(cbbDevices.SelectedItem, Dev).Index
        End If
        InitInputDevice()
    End Sub

    Private Sub InitInputDevice()
        BassWasapi.BASS_WASAPI_Init(Indev, 0, 0, 0, 0.4, 0.05, AddressOf InWasapiProc, IntPtr.Zero)
        Dim type As String, di As New BASS_WASAPI_DEVICEINFO
        BassWasapi.BASS_WASAPI_GetDeviceInfo(indev, di)
        Select Case (di.type)
            Case BASSWASAPIDeviceType.BASS_WASAPI_TYPE_NETWORKDEVICE
                type = "Remote Network Device"
            Case BASSWASAPIDeviceType.BASS_WASAPI_TYPE_SPEAKERS
                type = "Speakers"
            Case BASSWASAPIDeviceType.BASS_WASAPI_TYPE_LINELEVEL
                type = "Line In"
            Case BASSWASAPIDeviceType.BASS_WASAPI_TYPE_HEADPHONES
                type = "Headphones"
            Case BASSWASAPIDeviceType.BASS_WASAPI_TYPE_MICROPHONE
                type = "Microphone"
            Case BASSWASAPIDeviceType.BASS_WASAPI_TYPE_HEADSET
                type = "Headset"
            Case BASSWASAPIDeviceType.BASS_WASAPI_TYPE_HANDSET
                type = "Handset"
            Case BASSWASAPIDeviceType.BASS_WASAPI_TYPE_DIGITAL
                type = "Digital"
            Case BASSWASAPIDeviceType.BASS_WASAPI_TYPE_SPDIF
                type = "SPDIF"
            Case BASSWASAPIDeviceType.BASS_WASAPI_TYPE_HDMI
                type = "HDMI"
            Case Else
                type = "undefined"
        End Select
        If (di.flags And BASSWASAPIDeviceInfo.BASS_DEVICE_LOOPBACK) = BASSWASAPIDeviceInfo.BASS_DEVICE_LOOPBACK Then
            type = type & " (loopback)"
        End If
        LblType.Text = type ' display the type
    End Sub

    Private Sub StartRecording()
        Inmixer = BassMix.BASS_Mixer_StreamCreate(44100, 2, BASSFlag.BASS_STREAM_DECODE)
        BassMix.BASS_Mixer_StreamAddChannel(Inmixer, Instream, 0)
        If Not BassWasapi.BASS_WASAPI_SetDevice(Indev) Or Not BassWasapi.BASS_WASAPI_Start() Then
            MessageBox.Show("Can't start recording")
            Bass.BASS_StreamFree(Inmixer)
            Inmixer = 0
        Else
            'bassenc????
        End If
    End Sub

    Private Sub StopRecording()
        BassWasapi.BASS_WASAPI_SetDevice(Indev)
        BassWasapi.BASS_WASAPI_Stop(True)
        Bass.BASS_StreamFree(Inmixer)
        Inmixer = 0
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        StartRecording()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        StopRecording()
    End Sub

End Class
« Last Edit: 13 Oct '17 - 09:26 by AudioFan »