0

What I'm currently doing is while recording I get the bytes from the data received event and encode the bytes with a codec. The codecs I have set up to currently use are ALaw, G722, GSM, etc (like in the NetworkChatDemo)

The code used to start recording

    If waveIn Is Nothing Then
        waveIn = New WaveIn
        waveIn.DeviceNumber = 0
        waveIn.BufferMilliseconds = 100
        waveIn.WaveFormat = codec.RecordFormat
        waveIn.StartRecording()
    End If

The code used to work with the recorded bytes

Private Sub OnDataAvailable(ByVal sender As Object, ByVal e As WaveInEventArgs) Handles waveIn.DataAvailable
    Dim encoded As Byte() = codec.Encode(e.Buffer, 0, e.BytesRecorded)
    ...
    //Send encoded audio over UdpClient
End Sub

This is working well for me but I'd like to add support for the AAC format. So I'd like to achieve the same result as in the above DataReceived event, a byte array of encoded AAC audio which I can send over udp, from MediaFoundationEncoder.EncodeToAac(...) or another class/function I am not aware of within NAudio. As far as I can tell, MediaFoundationEncoder.EncodeToAac(...) only encodes audio to a file and not a byte array. It also doesn't accept a byte array as a parameter. Is there a way to achieve what I want or does NAudio only let me work with an AAC file after it has been fully recorded and saved?

Jeremy K
  • 523
  • 1
  • 6
  • 20
  • 2
    A lot of this stuff is new in NAudio. The Media Foundation library does appear to allow for stream-based encoding (according to the documentation for `MFCreateSinkWriterFromURL`), so I expect that Mark will build that functionality into NAudio at some stage. – Corey Oct 28 '13 at 08:19

1 Answers1

4

Yes, as Corey says, this is a new addition to NAudio for 1.7, and adding support to encode to a Stream is something I would definitely like to add in the future.

If you don't mind working at a lower level, you could try to construct the AAC encoder as a MFT creating a derived class from MediaFoundationTransform (see MediaFoundationResampler as an example of how to do this.

Mark Heath
  • 48,273
  • 29
  • 137
  • 194
  • 1
    Does it use `IStream` or another form? If `IStream` then the `InteropStream` I banged up for http://stackoverflow.com/questions/19421460 might be useful - I can flesh it out if you like. – Corey Oct 28 '13 at 20:15
  • It's an IMFByteStream – Mark Heath Oct 28 '13 at 20:41