Low Latency Audio Output C++ - c++

I am currently working on a project, wherein I am supposed to process frames. The task at hand is to read the frame data, detect if the data crosses a particular threshold and then make a sound.
The frame per second is about 45-50.
I did that with the function Beep () and tried 2 ms for the duration but then the beeps were in a queue. It still takes for a while even though the input does not have values ​​above the threshold. So I just made a delay of 175ms and then it worked fine, but now I'm just processing 5 samples per second out of the 45-50 samples.
Now I'm using the function PlaySound() and playing a 1ms sound file in the player. Now it takes about 250 ms to play the sound. So now even without delay 4 samples.
I am completely new to real-time programming like this. I am facing the Latency issue.
I would really appreciate it if any of you can help me with the project.
Thank you in advance.
OS:- Windows 10
IDE:- Visual Studio 2017 Community
Language:- C++

Related

How to send audio data playing on PC to C++ program as input

I'm a beginner when it comes to programming and I wanted to do a personal project in C++ to develop my skills. The project I had in mind involves playing audio on my laptop (running Windows 10), analyzing it, and sending data to an arduino that will change the color and brightness of LED lights in sync with the audio that's playing. I would like it so that I can simply, for example, just play a song on Spotify or a music video on Youtube etc. and the program will get data from that audio stream as an input. Elsewhere I've seen programs use audio from recorded WAV files or streams from a microphone as input, but not what I have in mind. I want to use this program for parties, so using a microphone as a workaround wouldn't be ideal.
Is this even possible? And if so how should I approach this problem? Are there certain APIs I should look to or what? If the program gets audio as the input, would I still be able to play music on something like a bluetooth speaker as well? Or can it only send data to one place at a time?
My roommate who is much better at programming than me accomplished this on Mac using Swift, and while I don't have a Mac, would using Linux instead make this easier?
Modern windows has “Stereo Mix” recording device, just for that. Here’s how to enable: https://technicalustad.com/enable-stereo-mix-in-windows-10/
After that setup, in your C++ program use any recording API you want.
Here’s a sample that does what you ask for, opens a recording device, starts recording, and sends audio samples to the class provided in the argument: https://learn.microsoft.com/en-us/windows/win32/coreaudio/capturing-a-stream You probably want to trade CPU time for latency for your application, i.e. don’t Sleep for hnsActualDuration/REFTIMES_PER_MILLISEC/2, change into Sleep( 0 ) or Sleep( 1 )

Implementing Xaudio2 for windows phone 8 application

I am new to coding for windows phone 8, but have coded a lot in python before. I am trying to make an application that uses Xaudio2 to generate a sine wave in real time and loop it infinitely as a drone pitch. I am using Visual Studio Express 2012 and writing the application in C++ and XAML. I have figured out the graphical interface, which was not very hard, and tried using MediaElement to loop a short sound file to make the constant pitch, but there was a small gap in the sound every time the file restarted. I read that using Xaudio2 could remove the gap and even generate the sound in real time without the need for a recorded sound (since I am concerned about memory) without any gaps. This sounds like exactly what I needed, but I have had extreme troubles with Xaudio2. I have looked at tutorials, as many as I can find, but I am not sure how to start the function, how to call the function, or where the code even goes on the page. I am assuming it goes on the C++ page, but I don't even know that. What I am trying to do is really simple, but I cannot get started because I do not know how to use this API in my code. If anyone could help me to figure out how to get Xaudio2 started and working in my code, that would be really amazing!

EDSDK 3.4.0 on OS X 10.12.1 with Rebel t6i: `kEdsObjectEvent_DirItemCreated` event is not received for up to 30 seconds after photo is taken

When using EDSDK version 3.4.0 to take a photo with the Rebel T6i it can take anywhere from 2 to 30 seconds after calling EdsSendCommand(camera, kEdsCameraCommand_TakePicture, 0); for the corresponding kEdsObjectEvent_DirItemCreated to be received, signalling that the image is ready to download from the camera. Note that the camera itself takes the photo and the flash goes off almost instantly after sending the TakePicture command - it is only the kEdsObjectEvent_DirItemCreated event that is delayed for seemingly random, large amounts of time.
The delays become much longer and more frequent when connecting to a second Rebel T6i, even when only taking photos with one of the cameras. This even occurs when both cameras are ran from separate applications.
We're hoping to use both of these cameras as a part of an installation that requires we're able to download each photo from the camera within at most 5 seconds from when EdsSendCommand(camera, kEdsCameraCommand_TakePicture, 0) is called.
If anyone has any ideas on why this large delay might be occurring or any other suggestions on how to fix it, we'd greatly appreciate it!
Note: We're building 64-bit at the moment but are currently attempting to get a 32-bit build working in the meantime to see if that improves anything.
EDSDK v3.4.0
OS X 10.12.1
64-bit
Rebel T6i
Not using live view will fix the problem. You need to download the image direct to the computer instead of saving to the SD card first as well. If ANY other camera is plugged in that is using live mode then you will continue to have the above problem unfortunately.

Problems capturing with Media Foundation & Windows 8 in C++

This is going to be my first question in StackOverflow after several days looking for an explanation. Please, be gentle with me for asking because I know my problem is a bit bizarre to be a general problem.
I made a MF capture video application, based in the Microsoft example 'CaptureToFile'. It did work on Windows 7 x64. I upgraded to Visual Studio 2013 without problems. Problems arose when I try to put all the development on a Windows 8.1 x64 machine.
The app compiles and executes without error, but it's UNABLE to capture samples by using m_pReader->ReadSample() in asynchronous mode; only the first two samples arrive to OnReadSample method; and there must be 'control' samples, because the IMFSample is null in all of them. After that, the app gets 'hanged' waiting for data.
I've tried the original MFCaptureToFile sample with the same sad results.
Of course, I think hardware and software are similar (the same capture card with the same driver version, both are desktop PC...)
Do you know any possible reason for this behaviour? in Win7 everything is working flawless! Or at least, if you could light me a bit about new paths for finding what's happening
Thanks in advance
UPDATE: There is another 'player' in the game. Looking into the threads, I see that a worker thread is in 'RTWorkQ.dll', the real-time work queue container, specific only for Windows 8. I will go on investigating. In the meantime, if you have any idea, something to share, I'll be glad to hear you.
UPDATE 2: I've modified the sample MFCaptureToFile to get the video samples synchronously, because I thought the problem could be due to the asynchronous behaviour; related with the queues. I've to say that the problem persist even with this change. The second time it tries to read a sample, the application gets 'hanged' waiting for a reading that doesn't never arrives.
UPDATE 3: I've tried with the CaptureEngine sample application that uses another MF way to capture video (MFCaptureEngine). It builds and runs flawlessly but doesn't show any images when starting the 'preview' and doesn't record any useful, only non-playable files.
UPDATE 4: I've installed Visual Studio 2010 Ultimate in Windows 8 PRO. The sample MFCaptureToFile fails again in the sample. It's unable to read a 2nd sample from the frame grabber. I'm starting to think that can be an incompatibility between the capture card (Datapath VisionRGB-E1S) and Windows 8 PRO despite the driver assures it works fine in this platform and the test program shows images. Tomorrow I'm going to try the test with an external USB webcam.
Finally, I have figured out the reason of this problem.
With Windows 8.1 release Microsoft has introduced New AVStream Interfaces for Windows 8.1
There is a small but very important change in KS_FRAME_INFO structure - the new FrameCompletionNumber member.
An identifying sequence number for the frame in the completed queue.
This number is used to verify proper frame order. When this value is
0, the frame was cancelled. This member is available starting with
Windows 8.1.
DirectShow doesn't care about this number. And MediaFoundation cares.
So, you cannot just fix that on your user-mode side. The manufacture developers must release an update. Btw, I have two webcams - Logitech C270 and Creative Live Socialize HD. Logitech supports Metro while Creative does not.
I have successfully updated my driver with only a few lines of code (to set up FrameCompletionNumber properly).
UPD. similar thread http://www.osronline.com/showthread.cfm?link=255004
It must be a problem of the frame grabber Datapath VisionRGB-E1S. I've tried with the brand-new USB webcam LifeCam Studio, and everything worked fine.
I will left for other future thread why this unpaired behaviour between Windows 8 and Windows 7, but it could be something related to the User-mode access...
I had the same kind of issue:
IMFSourceReader was obtained successfully
reader->SetCurrentMediaType() reported no error.
reader->ReadSample() was successful.
then OnReadSample() was called only once and the hrStatus argument 0x80070491
For me, the issue was that I modified the video subtype IMFMediaType, then applied to the reader as current media type.

Cocos2d on application start gives random fps drop?

I am developing an iphone game using cocos2d and have it set to to show the FPS. Now most of the time the application will start and run at 60 fps, However apparently randomly sometimes the application will start and top at only 40 fps.
This does not happen often and seems to happen only when you quit the application and restart it as soon as possible, allthough this does not trigger the effect most of the time. But after a few quick quit-restart sequences the game eventually starts and has only 40 fps instead of the normal 60. If I quit the application at this point and wait for about 3 seconds it will again start with the normal 60 fps.
I am assuming this has something to do with the application not being able to free all of its memory before starting again?, this problem seems to be related to the iPhone OS.
It is important to note that this never happens if I quit the application wait 5 seconds and restart. Has anyone had this sort of problem before?. Any ideas of why this might be happening?.
If anyone is wondering this is because there used to be a bug when emulating the OPEN GL 2.0 in the iPhone 3GS/iPod Touch 3G, this has been fixed for OS 3.1. And all you have to do is the following:
// Try to use CADisplayLink director
// if it fails (SDK < 3.1) use Threaded director
if( ! [Director setDirectorType:CCDirectorTypeDisplayLink] )
[Director setDirectorType:CCDirectorTypeThreadMainLoop];