The following forum(s) have migrated to Microsoft Q&A (Preview): Developing Universal Windows apps!
Visit Microsoft Q&A (Preview) to post new questions.

Learn More

 locked
[UWP][C#]WASAPI using C# ? RRS feed

  • Question

  • Hi ,

    I'm trying to repro from the WASAPI example in Cpp the fourth example. The PCM Audio Capture.

    The goal is that I want to record Audio to  my windows app store App and at the same time display this red line that the fourth example has , and show the ups and downs of the voice.

    I know how to record audio using C# so let's not stay there. I 'm not so familiar with CPP so I can take the part of the code that makes the red line "waves" according to my voice.

    Is there any example that does that or something in C# so I can succeed my goal ?

    Take a look at the below. When you press the start button and start talking the red line is reproducing your voice.

    This is what I try to accomplish in C#

    Can I do that ?

    Appreciate any help you coulf give me.


    • Edited by Breeze Liu Monday, October 16, 2017 9:31 AM [UWP][C#]
    Wednesday, October 11, 2017 12:23 PM

All replies

  • Hi zakkar

    You could make the polyline waves according to your voice.  To make the polyline waves, you need the get all the points which define the path of the polyline.

    Here are the steps how the points are generated:

    1. Run the WindowsAudioSession CPP sample, start to capture the audio in Scenario2.
    2. In the WASAPICapture.cpp file, when the device starts to capture, the OnAudioSampleRequested() function is called.  Get the BYTE “Data” and DWORD “cbBytesToCapture” using IAudioCaptureClient.GetBuffer() as data and size for the point array.
    3. Send the “Data” and “cbBytesToCapture” to another function called ProcessScopeData(), In this function, system will copy the sample data to the buffer array and send off a event which will be handled by a handler called OnPlotDataReady() in Scenario4.xaml.cpp.
    4. In the OnPlotDataReady() function, system will convert the value of the array to points and then add these points to the pointcollection of the Polyline. Then the polyline changes.
    5. When the data keeps updating, the polyline will keeps changing like waves.

    Since the official sample only have the CPP version, you need to implement the process yourself in C# referring the above steps as reference. As you mentioned above, you have already known about how to record audio using C#. You could get the byte data and size from you captured audio data. Then you could convert them to point array like the official sample. Add the points to the pointcollection of the Polyline and the Polyline will change like waves.

    You could refer the following code about how to change the Polyline:

         private void First_Click(object sender, RoutedEventArgs e)
            {
                for (int i =0; i< list.Count; i++)
                {
                    Point newpoint = list[i];
                    Oscilloscope.Points.Insert(i+1,newpoint);
                }
            }

    Best regards,

    Roy 


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, October 12, 2017 9:58 AM
    Moderator
  • Hi Roy ,

    I've lost you.

    The cpp sample does that on the fly while you are speaking on the mic. This is what I'm trying to accomplish. Not to display the changed polyline afterwards while playing the captured audio.

    I'm starting the devices(initializing etc) and then I'm using the below line of code to store the captured data in a storagefile because I;m keeping the recordings in SQLITE.

     await m_mediaCaptureMgr.StartRecordToStorageFileAsync(recordProfile, this.m_recordStorageFile);

    How am I suppose to tell in the m_mediaCaptureMgr that every byte you are adding change the polyline.

    Is there any event that it is being triggered everytime so I can do something in there ?

    Thursday, October 12, 2017 1:28 PM
  • We could use an example in C#.
    Thursday, October 12, 2017 1:36 PM
  • Hi Roy ,

    I have managed after a lot of search to get the byte array using NAudio.

    The had an example with the event RecorderOnDataAvailable were it is triggered every time and gets the waveInEventArgs.Buffer which is the byte array

    below is the line of code which does that. 

     await recordStream.WriteAsync(waveInEventArgs.Buffer, 0, waveInEventArgs.BytesRecorded);

    Now since that I have the buffer how do I convert it to point array and display it in the Oscilloscope ?

    Appreciate your help on this

    Friday, October 13, 2017 1:13 PM
  • Anyone ?
    Monday, October 16, 2017 1:10 PM
  • If you need help beyond the scope of the forum you can open an assisted support case at http://aka.ms/storesupport and somebody will work with you one-on-one, though for how to issues that still will be business hours only and not over the weekend. The short answer on why there aren’t C# WASAPI samples is that WASAPI cannot be correctly used from managed code. Well have a media specialist follow up with more details and best practices, but it may be a few days for him to be available.
    Monday, October 16, 2017 4:14 PM
  • Hi zakkar,

    Unfortunately NAudio is an open source component so if you need help using or interpreting the sample data from that component you will need to talk with the maintainers of that project. That said, I can give you the recommended way to get audio sample data from a file or capture source using Microsoft APIs.

    As Rob pointed out WASAPI is only supported from C++. A couple of updates back we added the Audio Graph APIs to the UWP platform. These APIs allow you to access, mix and otherwise manipulate low latency audio data from any supported UWP language including C#.

    You can configure a custom Audio Graph with an AudioFrameOutputNode. This node will deliver raw audio data to you over time. You can then access the raw data and process it however you need. Unfortuantely we don't have a good sample that describes the use of the AudioFrameOutputNode but I've included a link below that contains a code snip it as well as a link to the official sample that will help you get the audio graph configured for your scenario.

    Unfortunately helping you take the raw data and render it in the context of an Oscilloscope like output is going to be beyond the scope of this forum. However I can try and give you some really high level recommendations for rendering the data.

    One of the things that you will need to pay particular attention to is the format of the raw wave data that you receive. Depending on the format negotiated with the endpoint you will need to do different maths to render the waveform accurately (again describing the potentially complex maths is beyond the scope of the forum.)


    The recommended drawing technology is going to be Win2D. This is a WinRT wrapper around the lower level Direct2D. Using this drawing technique will give you the greatest flexibility and the lowest latency when drawing in "real time" to the video surface. Using this technology you can either draw a point per sample or create a poly line per frame that plots the individual samples.

    AudioFrameOutputNode
    https://docs.microsoft.com/en-us/uwp/api/windows.media.audio.audioframeoutputnode

    Audio graphs - Audio frame output node
    https://docs.microsoft.com/en-us/windows/uwp/audio-video-camera/audio-graphs#audio-frame-output-node

    Microsoft/Win2D
    https://github.com/Microsoft/Win2D

    Win2D documentation
    http://microsoft.github.io/Win2D/html/Introduction.htm

    I hope this helps,

    James


    Windows SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/

    Monday, October 16, 2017 11:26 PM
    Owner
  • Hi Rob ,

    I'm not so sure that the Store Support will help me on this. They will redirect me to the Forums.

    This support is most dashboard or apps related issues about publishing, payouts etc. I do have a developer account  but when it comes to code I don't think that this is the solution for me.

    I know a lot off guys who are trying to accomplish what I am trying to do and cannot find a straight forward example. Perhaps it is time to help us here with a good example in C#.

     I just tried to add something cool in my app and now I have to process all the information about WAVs , MP3s Ieef formats ...Which is useless for the app that I'm building because I just need to add an Audio Meter. That's all.

    And now I will have to  delay the delivered app for this because I have to dig into the deep for bits and bytes and chunks.

    It's time to think here what do we want to do with the developers who continue to support the platform. 

    Are we going to help them ? Are we going to shut down the platform so there is no need to continue helping the people ?

    I have also tried to buy if there was one a control for that but I cannot find any.

    And I don't think that asking for example in C# is out of the scope for this Forum because with the examples programmers are building the apps for the store. If you had a complete example in C# which is one of the most common language in the world then we were not going to have this discussion. If you search the forums for WASAPI you will see how many people faced issues and want to build things. Is this a hidden knowledge? Hidden technology that must not leak ? Don't know.

    We are trying to find a way to access in low level the devices and we cannot "marry" the technologies. That is why we need guidance here.

    Anyway Rob.

    Thank you

    Tuesday, October 17, 2017 7:16 AM
  • hi James ,

    I have already downloaded all these and I'm trying to see how am I going to use them.

    I will also contact NAudio guys and see what will come app because as I see from the Git they are transferring the code from Codeplex so perhaps they are not ready yet.

    Tuesday, October 17, 2017 7:17 AM
  • I'm not so sure that the Store Support will help me on this. They will redirect me to the Forums.

    This support is most dashboard or apps related issues about publishing, payouts etc. I do have a developer account  but when it comes to code I don't think that this is the solution for me.

    There are two support sections in the Contact Us area on http://aka.ms/storesupport . For help with coding issues use the second:

    Need help writing UWP apps (including Mixed Reality)?

    Professional Support for troubleshooting, debugging, and code-level functionality assistance.

    I mentioned this primarily because this seemed high priority for you: you expressed frustration that you didn't get a response over the weekend. If this is that high priority then you'll likely be better off through paid support rather than forums.

    Wednesday, October 18, 2017 9:17 PM
  • I 'll do.

    Thank you Rob

    Thursday, October 19, 2017 7:09 AM