Sound aufnehmen als bytes in varible und wieder abspielen

  • VB.NET

Es gibt 6 Antworten in diesem Thema. Der letzte Beitrag () ist von thefiloe.

    Sound aufnehmen als bytes in varible und wieder abspielen

    Hallo liebe Gemeinde ,

    Ich stehe vor einem kleinen Problem.

    Problem :

    Ich möchte gerne den Sound von meinem Micro aufnehmen. Habe dazu auch schon ein bisschen Code im Internet gefunden, und mir den von C# in VB.Net übersetzt. Dieser Code arbeitet mit DirectX sprich mit DirectSound. Allerdings muss ich wenn ich diesen Code benutze immer ein Fensterhandle angeben, das die Instanz meiner Anwendung darstellt.

    Sobald ich aber ein anderes Fenster im Focus habe, hört die Aufnahme auf. Das ist ja nicht Sinn der Sache. Gibt es dort noch andere Möglichkeiten, als mit DirectX zu arbeiten ? Wichtig ist aber, das ich den aufgenommenen Sound als Bytes brauche. Wichtig ist auch das ich diese Bytes mit der selben library oder whatever wieder abzuspielen.
    Boardregeln §4 1e) Mehrere Postings hintereinander (Doppelposts) in einem Thread (Thema) von einem einzigen User sind nur erlaubt,
    wenn dies unbedingt erforderlich ist oder der vorherige Post schon längere Zeit zurückliegt.


    Längere Zeit ist nicht 1 Tag.

    -> Pushposts gelöscht.

    RodFromGermany schrieb:


    Kannst Du mal ein wenig Code posten?


    Naja Wenig ist anders :D So viel Code, um einfach nur vom micro aufzunehmen ^^


    Spoiler anzeigen

    VB.NET-Quellcode

    1. Imports Microsoft.DirectX.DirectSound
    2. Imports System.IO
    3. Imports System.Net.Sockets
    4. Imports System.Net
    5. Public Class Voice
    6. Private captureBufferDescription As CaptureBufferDescription
    7. Private autoResetEvent As Threading.AutoResetEvent
    8. Private notify As Notify
    9. Private waveFormat As WaveFormat
    10. Private Shadows capture As Capture
    11. Private bufferSize As Integer
    12. Private captureBuffer As CaptureBuffer
    13. Private device As Device
    14. Private playbackBuffer As SecondaryBuffer
    15. Private playbackBufferDescription As BufferDescription
    16. Public bStop As Boolean = True
    17. Private SendClient As New UdpClient
    18. Private byteData As Byte() = New Byte(1023) {}
    19. Private PPort As Integer
    20. Sub New(ByVal Port As Integer)
    21. PPort = Port
    22. Init()
    23. End Sub
    24. Public Sub Init()
    25. device = New Device()
    26. device.SetCooperativeLevel(frm_test, CooperativeLevel.Normal)
    27. Dim captureDeviceCollection As New CaptureDevicesCollection()
    28. Dim deviceInfo As DeviceInformation = captureDeviceCollection(0)
    29. capture = New Capture(deviceInfo.DriverGuid)
    30. Dim channels As Short = 1
    31. 'Stereo.
    32. Dim bitsPerSample As Short = 16
    33. '16Bit, alternatively use 8Bits.
    34. Dim samplesPerSecond As Integer = 44100
    35. '11KHz use 11025 , 22KHz use 22050, 44KHz use 44100 etc.
    36. 'Set up the wave format to be captured.
    37. waveFormat = New WaveFormat()
    38. waveFormat.Channels = channels
    39. waveFormat.FormatTag = WaveFormatTag.Pcm
    40. waveFormat.SamplesPerSecond = samplesPerSecond
    41. waveFormat.BitsPerSample = bitsPerSample
    42. waveFormat.BlockAlign = CShort(channels * (bitsPerSample \ CShort(8)))
    43. waveFormat.AverageBytesPerSecond = waveFormat.BlockAlign * samplesPerSecond
    44. captureBufferDescription = New CaptureBufferDescription()
    45. captureBufferDescription.BufferBytes = CInt(waveFormat.AverageBytesPerSecond / 5)
    46. 'approx 200 milliseconds of PCM data.
    47. captureBufferDescription.Format = waveFormat
    48. playbackBufferDescription = New BufferDescription()
    49. playbackBufferDescription.BufferBytes = CInt(waveFormat.AverageBytesPerSecond / 5)
    50. playbackBufferDescription.Format = waveFormat
    51. playbackBuffer = New SecondaryBuffer(playbackBufferDescription, device)
    52. bufferSize = captureBufferDescription.BufferBytes
    53. End Sub
    54. Public Sub Senden()
    55. If bStop = False Then
    56. Else
    57. Dim T As New System.Threading.Thread(AddressOf Send)
    58. T.Start()
    59. End If
    60. End Sub
    61. Private Sub Send()
    62. Try
    63. captureBuffer = New CaptureBuffer(captureBufferDescription, capture)
    64. CreateNotifyPositions()
    65. Dim halfBuffer As Integer = CInt(bufferSize / 2)
    66. captureBuffer.Start(True)
    67. Dim readFirstBufferPart As Boolean = True
    68. Dim offset As Integer = 0
    69. Dim memStream As New MemoryStream(halfBuffer)
    70. bStop = False
    71. While Not bStop
    72. autoResetEvent.WaitOne()
    73. memStream.Seek(0, SeekOrigin.Begin)
    74. captureBuffer.Read(offset, memStream, halfBuffer, LockFlag.None)
    75. readFirstBufferPart = Not readFirstBufferPart
    76. offset = If(readFirstBufferPart, 0, halfBuffer)
    77. Dim dataToWrite As Byte() = memStream.GetBuffer()
    78. 'Hier wird das ganze dann per UDP Rausgefeuert
    79. End While
    80. Catch ex As Exception
    81. MsgBox(ex.ToString, MsgBoxStyle.Information, "VoiceChat-Send ()")
    82. Finally
    83. captureBuffer.Stop()
    84. bStop = True
    85. End Try
    86. End Sub
    87. Public Sub Empfangen()
    88. Dim T As New Threading.Thread(AddressOf Receive)
    89. T.Start()
    90. End Sub
    91. Private Sub Receive()
    92. Try
    93. bStop = False
    94. Dim remoteEP As New IPEndPoint(IPAddress.Any, 0)
    95. SendClient.Client.Bind(remoteEP)
    96. While Not bStop
    97. 'Receive data.
    98. Dim byteData As Byte() = SendClient.Receive(remoteEP)
    99. 'G711 compresses the data by 50%, so we allocate a buffer of double
    100. 'the size to store the decompressed data.
    101. Dim byteDecodedData As Byte() = New Byte(byteData.Length * 2 - 1) {}
    102. byteDecodedData = New Byte(byteData.Length - 1) {}
    103. byteDecodedData = byteData
    104. 'Play the data received to the user.
    105. playbackBuffer = New SecondaryBuffer(playbackBufferDescription, device)
    106. playbackBuffer.Write(0, byteDecodedData, LockFlag.None)
    107. playbackBuffer.Play(0, BufferPlayFlags.[Default])
    108. End While
    109. MsgBox("Ende")
    110. Catch ex As Exception
    111. MsgBox(ex.ToString, MsgBoxStyle.Information, "VoiceChat-Receive ()")
    112. Finally
    113. End Try
    114. End Sub
    115. Private Sub CreateNotifyPositions()
    116. Try
    117. autoResetEvent = New Threading.AutoResetEvent(False)
    118. notify = New Notify(captureBuffer)
    119. Dim bufferPositionNotify1 As New BufferPositionNotify()
    120. bufferPositionNotify1.Offset = CInt(bufferSize / 2 - 1)
    121. bufferPositionNotify1.EventNotifyHandle = autoResetEvent.SafeWaitHandle.DangerousGetHandle()
    122. Dim bufferPositionNotify2 As New BufferPositionNotify()
    123. bufferPositionNotify2.Offset = bufferSize - 1
    124. bufferPositionNotify2.EventNotifyHandle = autoResetEvent.SafeWaitHandle.DangerousGetHandle()
    125. notify.SetNotificationPositions(New BufferPositionNotify() {bufferPositionNotify1, bufferPositionNotify2})
    126. Catch ex As Exception
    127. MsgBox(ex.Message, MsgBoxStyle.Information, "VoiceChat-CreateNotifyPositions ()")
    128. End Try
    129. End Sub
    130. End Class



    Hoffe du kannst mir helfen ;)

    Slayers schrieb:

    Hoffe du kannst mir helfen

    Noch nicht.
    Da fehlt noch eine ganze Menge.
    Leg Dir ein neues leeres Projekt an, kopier die Klasse Voice da hinein und alles, was der Compiler anmeckert, fehlt.
    Du musst nicht alle Deine Klassen postren, nur soviel, dass ich den Effekt nachvollziehen kann.
    Dann sehen wir weiter. :thumbup:
    ---------------
    Sieh Dir mal diesen Threadan.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „RodFromGermany“ ()

    Habe mal mit den DX-Coms bisschen rumgespielt. Kann mich errinnern, dass ich dort das Desktophandle angegeben hab.
    msdn.microsoft.com/en-us/libra…s633504%28v=vs.85%29.aspx


    Opensource Audio-Bibliothek auf github: KLICK, im Showroom oder auf NuGet.