Search code examples
c#vb.netwindows-store-apps

Convert C# code to vb.net issue (Windows RT)


I'm trying to implements sound feature to a Windows RT app in vb.net with SharpDX, and I need to convert this class to vb.net:

using SharpDX.IO;
using SharpDX.Multimedia;
using SharpDX.XAudio2;
using System.Collections.Generic;
using System.Linq;

namespace PlayWaveRT {
public class WaveManager {
    private XAudio2 xAudio;
    private List<Wave> waves = new List<Wave>();

    public WaveManager() {
        xAudio = new XAudio2();
        var mastering = new MasteringVoice(xAudio);
        mastering.SetVolume(1, 0);
        xAudio.StartEngine();
    }

    public void LoadWave(string path, string key) {
        var buffer = GetBuffer(path);
        waves.Add(new Wave { Buffer = buffer, Key = key });
    }

    public void PlayWave(string key) {
        var wave = waves.FirstOrDefault(x => x.Key == key);
        if (wave != null) {
            var voice = new SourceVoice(xAudio, wave.Buffer.WaveFormat, true);
            voice.SubmitSourceBuffer(wave.Buffer, wave.Buffer.DecodedPacketsInfo);
            voice.Start();
        }
    }

    private AudioBufferAndMetaData GetBuffer(string soundfile) {
        var nativefilestream = new NativeFileStream(soundfile, NativeFileMode.Open, NativeFileAccess.Read, NativeFileShare.Read);
        var soundstream = new SoundStream(nativefilestream);
        var buffer = new AudioBufferAndMetaData {
            Stream = soundstream.ToDataStream(),
            AudioBytes = (int)soundstream.Length,
            Flags = BufferFlags.EndOfStream,
            WaveFormat = soundstream.Format,
            DecodedPacketsInfo = soundstream.DecodedPacketsInfo
        };
        return buffer;
    }

    private sealed class AudioBufferAndMetaData : AudioBuffer {
        public WaveFormat WaveFormat { get; set; }
        public uint[] DecodedPacketsInfo { get; set; }
    }

    private class Wave {
        public AudioBufferAndMetaData Buffer { get; set; }
        public string Key { get; set; }
    }
}
}

This is what I've done:

Imports SharpDX.IO
Imports SharpDX.Multimedia
Imports SharpDX.XAudio2
Imports System.Collections.Generic
Imports System.Linq

Namespace PlayWaveRT

Public Class WaveManager

    Private xAudio As XAudio2
    Private waves As New List(Of Wave)()

    Public Sub New()
        xAudio = New XAudio2()
        Dim mastering = New MasteringVoice(xAudio)
        mastering.SetVolume(1, 0)
        xAudio.StartEngine()
    End Sub

    Public Sub LoadWave(path As String, key1 As String)
        Dim buffer1 = GetBuffer(path)
        Dim wave As New Wave()
        wave.Buffer = buffer1
        wave.Key = key1
    End Sub

    Public Sub PlayWave(key1 As String)
        Dim wave = waves.FirstOrDefault(Function(x) x.Key = key1)
        If wave IsNot Nothing Then
            Dim voice = New SourceVoice(xAudio, wave.Buffer.WaveFormat, True)
            voice.SubmitSourceBuffer(wave.Buffer, wave.Buffer.DecodedPacketsInfo)
            voice.Start()
        End If
    End Sub

    Private Function GetBuffer(soundfile As String) As AudioBufferAndMetaData
        Dim nativefilestream = New NativeFileStream(soundfile, NativeFileMode.Open, NativeFileAccess.Read,  NativeFileShare.Read)
        Dim soundstream = New SoundStream(nativefilestream)
        Dim buffer = New AudioBufferAndMetaData()
        buffer.Stream = soundstream.ToDataStream()
        buffer.AudioBytes = CInt(soundstream.Length)
        buffer.Flags = BufferFlags.EndOfStream
        buffer.WaveFormat = soundstream.Format
        buffer.DecodedPacketsInfo = soundstream.DecodedPacketsInfo
        Return buffer
    End Function

    Private Class AudioBufferAndMetaData
        Inherits AudioBuffer

        Private m_WaveFormat As WaveFormat
        Private m_DecodedPacketsInfo As UInteger()

        Public Property WaveFormat As WaveFormat
            Get
                Return m_WaveFormat
            End Get
            Set(value As WaveFormat)
                m_WaveFormat = value
            End Set
        End Property

        Public Property DecodedPacketsInfo As UInteger()
            Get
                Return m_DecodedPacketsInfo
            End Get
            Set(value As UInteger())
                m_DecodedPacketsInfo = value
            End Set
        End Property

    End Class

    Private Class Wave
        Private m_Buffer As AudioBufferAndMetaData
        Private m_Key As String

        Public Property Buffer As AudioBufferAndMetaData
            Get
                Return m_Buffer
            End Get
            Set(value As AudioBufferAndMetaData)
                m_Buffer = value
            End Set
        End Property

        Public Property Key As String
            Get
                Return m_Key
            End Get
            Set(value As String)
                m_Key = value
            End Set
        End Property
    End Class

End Class

End Namespace

I have no compilator errors, but the code isn't working and I don't know where's the problem.

I've imported to my project SharpDX.dll and SharpDX.XAudio2.dll

This is how I usage this class in my Project:

Imports The_Game_of_15.PlayWaveRT
Public NotInheritable Class SelectPage
Inherits Page

Private waveManager As New WaveManager()

Protected Overrides Sub OnNavigatedTo(e As Navigation.NavigationEventArgs)
    ..........
    ..........
    waveManager.LoadWave("select.wav", "select")
End Sub

Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
    waveManager.PlayWave("select")
End Sub

End Class

I can compile the project with no errors, but no sound it's played when I click the button. Can anyone help me? Thanks in advance.


Solution

  • VB.Net inline:

    Public Sub LoadWave(path As String, key1 As String)
        Dim buffer1 = GetBuffer(path)
        waves.Add(New Wave With {.Buffer = buffer,.Key = key})
    End Sub