DirectX, Buffers, LAME, and VB .NET
DirectX, Buffers, LAME, and VB .NET
DirectX, Buffers, LAME, and VB .NET
DirectX, Buffers, LAME, and VB .NET
DirectX, Buffers, LAME, and VB .NET
DirectX, Buffers, LAME, and VB .NET DirectX, Buffers, LAME, and VB .NET DirectX, Buffers, LAME, and VB .NET DirectX, Buffers, LAME, and VB .NET DirectX, Buffers, LAME, and VB .NET DirectX, Buffers, LAME, and VB .NET DirectX, Buffers, LAME, and VB .NET DirectX, Buffers, LAME, and VB .NET
DirectX, Buffers, LAME, and VB .NET DirectX, Buffers, LAME, and VB .NET
DirectX, Buffers, LAME, and VB .NET
Go Back  Xtreme Visual Basic Talk > > > > DirectX, Buffers, LAME, and VB .NET


Reply
 
Thread Tools Display Modes
  #1  
Old 03-15-2008, 06:09 PM
izzy4505 izzy4505 is offline
Regular
 
Join Date: Dec 2004
Location: IL, USA
Posts: 84
Exclamation DirectX, Buffers, LAME, and VB .NET


I apologize for this terribly broad post, but I'm a bit stumped and I was wondering if someone can point me in the right direction.

I'm experimenting with using LAME to encode captured sound from the sound card into MP3 format, which will eventually be sent on its way streaming, but the network part I can figure out.. just stuck on audio right now.

Last week, after reading through a bunch of pages, and translating a bunch of code from VB 6 and C#, I was able to make a crude VU meter using the following code and a couple of progress bars:

Code:
   Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
 
      Dim cap As New DirectSound.Capture(DXDevices(cboSoundDevices.SelectedIndex).DriverGuid)
      Dim desc As New DirectSound.CaptureBufferDescription()
      Dim wf As New DirectSound.WaveFormat
      wf.BitsPerSample = 16
      wf.SamplesPerSecond = 44100
      wf.Channels = 2
      wf.BlockAlign = CShort(wf.Channels * wf.BitsPerSample / 8)
      wf.AverageBytesPerSecond = wf.BlockAlign * wf.SamplesPerSecond
      wf.FormatTag = DirectSound.WaveFormatTag.Pcm

      desc.Format = wf
      desc.BufferBytes = 8 * wf.BlockAlign

      Buffer = New DirectSound.CaptureBuffer(desc, cap)
      buffer.Start(True)

      Dim UpdateVUThread As New Thread(New ThreadStart(AddressOf ThreadUpdateVU))
      UpdateVUThread.Priority = ThreadPriority.Lowest
      UpdateVUThread.Start()

   End Sub

   Delegate Sub UpdateVUCallback(ByVal LValue As Long, ByVal RValue As Long)

   Private Sub UpdateVU(ByVal LValue As Long, ByVal RValue As Long)
      Try
         If lblVU.InvokeRequired Then
            Dim d As New UpdateVUCallback(AddressOf UpdateVU)
            Invoke(d, New Object() {LValue, RValue})
         Else
            'lblVU.Text = "L: " & LValue & "  R: " & RValue
            lblVU.Text = LValue
            prgVUL.Value = Math.Abs(LValue)
            prgVUR.Value = Math.Abs(RValue)
         End If
      Catch
      End Try
   End Sub

   Private Sub ThreadUpdateVU()
      Do While True
         Dim SAMPLE_FORMAT_ARRAY As Integer() = {8, 2, 1}
         Dim samples As Array = buffer.Read(0, GetType(Int16), DirectSound.LockFlag.FromWriteCursor, SAMPLE_FORMAT_ARRAY)

         Dim x As Integer
         Dim LAvg As Long
         Dim RAvg As Long
         For x = 0 To 7
            LAvg += Math.Abs(CLng(samples(x, 0, 0)))
            RAvg += Math.Abs(CLng(samples(x, 1, 0)))
         Next x
         LAvg = CInt(LAvg / 8)
         RAvg = CInt(RAvg / 8)

         UpdateVU(LAvg, RAvg)

      Loop

   End Sub
I should also point out that this is my first dab at multithreading.

Anyway... here are my questions....
  1. That buffer that I created... does it catch every sample from the card? What if I quit reading it for a few seconds, does DirectX have a thread running the background continuously allocating memory (when possible) so that this buffer is ready to go for me with as few overruns as possible?
  2. Do you know of a good .NET wrapper around LAME? I've found a handful attached to other projects, but nothing too modular. I'll hack one out if I need to, but since I'm not too confident in my abilities of it working right now, I'd rather stick with something more reliable.

Thank you for your time. Any advice, general direction, or any help at all would be fantastic!
Reply With Quote
  #2  
Old 03-15-2008, 06:54 PM
Leade's Avatar
Leade Leade is offline
Junior Contributor
 
Join Date: Jul 2005
Location: Wherever i fall over
Posts: 222
Default

If you create a thread, and you allow the directsound device to have exclusive access it will run in the background as well as the foreground.
__________________
Fact: The best ideas come when your smashed of your face. So drink beer and be merry!!!
Reply With Quote
  #3  
Old 03-15-2008, 10:42 PM
izzy4505 izzy4505 is offline
Regular
 
Join Date: Dec 2004
Location: IL, USA
Posts: 84
Default

I've got that much figured out, but does DirectX itself have a thread running, peeking data from the sound card's physical buffer, and then putting it into my buffer object? Does the buffer object created with New DirectSound.CaptureBuffer represent a logical buffer in memory, or the buffer on the sound card itself? I guess what I need to know is, can I call Buffer.Start, and then do a buffer.read within a (potentially unreliable) timer object every second or so without fear of losing samples?
Reply With Quote
  #4  
Old 03-16-2008, 06:31 AM
Leade's Avatar
Leade Leade is offline
Junior Contributor
 
Join Date: Jul 2005
Location: Wherever i fall over
Posts: 222
Default

Quote:
Originally Posted by izzy4505 View Post
I've got that much figured out, but does DirectX itself have a thread running, peeking data from the sound card's physical buffer, and then putting it into my buffer object? Does the buffer object created with New DirectSound.CaptureBuffer represent a logical buffer in memory, or the buffer on the sound card itself? I guess what I need to know is, can I call Buffer.Start, and then do a buffer.read within a (potentially unreliable) timer object every second or so without fear of losing samples?
ok, been a long time since done sound recording, but i think i remember how its done.
Directx will put the data into the record buffer automatically, but the buffer is usually small and circular, so when it gets to the end of the buffer it starts again and overwriting existing data, its up to you to get the data from the buffer before it is overwritten with new data. You can add notifcation points, which will trigger an event when the write/read cursor is at one of these points and can add them wherever you like, but the begining or the end are the best places really in my opinion.
The end of the buffer is probably the best place, for ovbious reasons, when the point is triggered you can have your event read all data from buffer and add to a temp file(if neccessary) or in your case convert the data to VNU info, The VNU will be one second out, but i think VNUs are like that anyway.

As for the buffer it's not on the soundcard itself, as many soundcards won't have enough or any memory to execute such an operation.
__________________
Fact: The best ideas come when your smashed of your face. So drink beer and be merry!!!
Reply With Quote
  #5  
Old 03-16-2008, 09:37 AM
izzy4505 izzy4505 is offline
Regular
 
Join Date: Dec 2004
Location: IL, USA
Posts: 84
Default

How do I go about setting these notification points? I have a VB 6 example for this, but had a hard time setting it up in managed DirectX. What's VNU info? It's fine if all of the delays add up to a few seconds.

One other problem I'm having...

Code:
      Dim ReadPos As Long
      Dim WritePos As Long
      AudSource.Buffer.GetCurrentPosition(WritePos, ReadPos)
      lblCPU.Text = AudSource.Buffer.Capturing & " / " & WritePos & " / " & ReadPos
(Aud Source is the class I have doing most of my DirectX stuff. Buffer is a reference directly to the CaptureBuffer object.)

For some reason, WritePos and ReadPos always return 0. Any idea why?

Thanks!
Reply With Quote
  #6  
Old 03-16-2008, 12:47 PM
Leade's Avatar
Leade Leade is offline
Junior Contributor
 
Join Date: Jul 2005
Location: Wherever i fall over
Posts: 222
Default

Quote:
Originally Posted by izzy4505 View Post
How do I go about setting these notification points? I have a VB 6 example for this, but had a hard time setting it up in managed DirectX. What's VNU info? It's fine if all of the delays add up to a few seconds.

One other problem I'm having...

Code:
      Dim ReadPos As Long
      Dim WritePos As Long
      AudSource.Buffer.GetCurrentPosition(WritePos, ReadPos)
      lblCPU.Text = AudSource.Buffer.Capturing & " / " & WritePos & " / " & ReadPos
(Aud Source is the class I have doing most of my DirectX stuff. Buffer is a reference directly to the CaptureBuffer object.)

For some reason, WritePos and ReadPos always return 0. Any idea why?

Thanks!
Ok, you sure making me work for it today aint ya.
Easy stuff first...

You say VU i say VNU, same thing really.

The reason they return zero is because The GetCurrentPosition requires a DSCURSORS item and not two longvalues

When creating the buffer you have to add the flag DSBCAPS_CTRLPOSITIONNOTIFY just to let the system know notifications are needed.


heres how i did mine in VB6:
PosNotify(0) As DSBPOSITIONNOTIFY
PosNotify(0).lOffset = DSBPN_OFFSETSTOP
PosNotify(0).hEventNotify = EventLong 'Here you must give a long handle of the event doing the notifications
SoundBuffer.SetNotificationPositions 1, PosNotify()

Becuase of the way VB6 is designed you have to create a form with an event handler item and subroutine.
There may be a way of doing it in a single class, but i couldn't be bothered to figure it out.
__________________
Fact: The best ideas come when your smashed of your face. So drink beer and be merry!!!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
DirectX, Buffers, LAME, and VB .NET
DirectX, Buffers, LAME, and VB .NET
DirectX, Buffers, LAME, and VB .NET DirectX, Buffers, LAME, and VB .NET
DirectX, Buffers, LAME, and VB .NET
DirectX, Buffers, LAME, and VB .NET
DirectX, Buffers, LAME, and VB .NET DirectX, Buffers, LAME, and VB .NET DirectX, Buffers, LAME, and VB .NET DirectX, Buffers, LAME, and VB .NET DirectX, Buffers, LAME, and VB .NET DirectX, Buffers, LAME, and VB .NET DirectX, Buffers, LAME, and VB .NET
DirectX, Buffers, LAME, and VB .NET
DirectX, Buffers, LAME, and VB .NET
 
DirectX, Buffers, LAME, and VB .NET
DirectX, Buffers, LAME, and VB .NET
 
-->