Output Raw Data to Speakers - c++

I'm a reasonably advanced C++ programmer, as a bit of background. At this point, I'm wanting to experiment a bit with sound. Rather than use a library to load and play files, I'm wanting to figure out how to actually do that myself, for the understanding. For this application, I would like to read in a .wav file (I already have that part down), then output that data to the speakers. How do I push a waveform or the data from the file to the speakers on my computer? I'm on Windows, by the way.

You can read this article about how to set up the audio device and how to stream data into the device for playback on Windows. If using this library is too high-level for you and you'd like to go deeper and write your own decoding of WAV files and outputting that to a sound card, you have far more research to do than what's appropriate for an answer here.

Related

No audio output from one of two streams when rendering directly to WASAPI

I've been stuck on this problem for weeks now and Google is no help, so hopefully some here can help me.
I am programming a software sound mixer in C++, getting audio packets from the network and Windows microphones, mixing them together as PCM, and then sending them back out over the network and to speakers/USB headsets. This works. I have a working setup using the PortAudio library to handle the interface with Windows. However, my supervisors think the latency could be reduced between this software and our system, so in an attempt to lower latency (and better handle USB headset disconnects) I'm now rewriting the Windows interface layer to directly use WASAPI. I can eliminate some buffers and callbacks doing this, and theoretically use the super low latency interface if that's still not fast enough for the higher ups.
I have it only partially working now, and the partially part is what is killing me here. Our system has the speaker and headphones as three separate mono audio streams. The speaker is mono, and the headset is combined from two streams to be stereo. I'm outputting this to windows as two streams, one for a device of the user's choice for speaker, and one of another device of the user's choice for headset. For testing, they're both outputting to the default general stereo mix on my system.
I can hear the speaker perfectly fine, but I cannot hear the headset, no matter what I try. They both use the same code path, they both go through a WMF resampler to convert to 2 channel audio at the sample rate Windows wants. But I can hear the speaker, but never the headset stream.
It's not an exclusive mode problem: I'm using shared mode on all streams, and I've even specifically tried cutting down the streams to only the headset, in case one was stomping the other or something, and still the headset has no audio output.
It's not a mixer problem upstream, as I haven't modified any code from when it worked with PortAudio streams. I can see the audio passing through the mixer and to the output via my debug visualizers.
I can see the data going into the buffer I get from the system, when the system calls back to ask for audio. I should be hearing something, static even, but I'm getting nothing. (At one point, I bypassed the ring buffer entirely and put random numbers directly into the buffer in the callback and I still got no sound.)
What am I doing wrong here? It seems like Windows itself is the problem or something, but I don't have the expertise on Windows APIs to know what, and I'm apparently the most expert for this stuff in my company. I haven't even looked yet as to why the microphone input isn't working, and I've been stuck on this for weeks now. If anyone has any suggestions, it'd be much appreciated.
Check the re-sampled streams: output the stereo stream to the speaker, and output the mono stream to the handset.
Use IAudioClient::IsFormatSupported to check supported formats for the handset.
Verify your code using an mp3 file. Use two media players to play different files with different devices simultaneously.

How do I play .wav files in C++ on Linux?

I have a question about playing .wav files in C++. I search for a method to play it on Linux and Windows and I want to break up it while a function has been ended, did you know a sample for this?
Thanks for any Answer :)!
I am from Germany, please don't be angry about my grammar or spelling :D.
There are several ways to do this.
The simplest, ugliest and most hackish way to do this is to write directly to your sound device located in /dev/snd/. However this really depends on your linux distribution and some modern ones no longer allows you to do this. In general, direct read / write to /dev/ devices is fading away. Here is a question answering this.
For a very long time, the "official" way was to use alsa library which uses a C style interface. It is usually pretty tricky, full of pitfalls and "workarounds" that depends on your exact audio hardware.
I think that alsa is getting gradually replaced by jack, which I hope is easier to use. I don't have any experience with this one.
The last method I know is with portaudio, which as the name implies, is somewhat portable between at least windows, linux and osx.
All of these library will allow you to control your audio hardware, init / setup / play. What is simple about wav files is that the content is raw pcm, which is usually the format used by those libraries. The wav format is usually like this :
[wav header] [audio data until the end of the file]
If you can manage a few milliseconds of garbage when you start playing, you can "safely" send the header for playback as well and avoid parsing it. You will have to understand PCM formats however and should bring some additional readings.
As an added "trick" which doesn't directly concern C++, I strongly suggest using Audacity. It is a small program which I see like the notepad / vim of audio files. It is small, it loads fast, allows you to copy / paste segments of audio and detect pcm formats. (Actually, you just change pcm settings until the audio "sounds" right, but still useful)
The title mentioned Linux, but then you mentioned Windows and Linux in the post.
For Linux, best is to use gstreamer if you insist on C++. Look through the gstreamer code for gst-launch. It is run as below in a Linux machine.
$ gst-launch filesrc location="location of file" ! wavparse ! alsasink
From, http://sanchayanmaity.github.io/beagleboard-xm/linux/programming/software/2014/08/07/playing-wavmp3-file-using-gstreamer-in-code.html
For windows, or if you want to use OS agnostic code on both Windows and Linux, you can use SDL,
http://lazyfoo.net/SDL_tutorials/lesson11/
Another alternative (cross-platform, Object oriented), is SFML. Check the audio wav file playback example at,
http://www.sfml-dev.org/tutorials/2.0/audio-sounds.php

Using FMOD in C++ to create and playback sounds in realtime. (UE4)

Looking through all the API documentation, I can see how one could create procedural audio, but once an audio file is created, I want it to play on an object, but from I can tell, I believe I need it to play using the function calls PlayEventAtLocation in the UE4 plugin, which means I need to get the sound into an event.
I used to have my setup in Unity 4.x. I want to dynamically construct a wav file in game and play it back. The idea was to have silent audio all over the map that would loop, but play muted. The player when in range would capture audio from this muted audio source at their discretion.
The idea is that you have wav file that plays in game and at any given time I can start grabbing data from where the buffer is at currently until I decide to stop. I take all the data that I created in this new buffer and create a new wav file with it.
Example, like a 20 second file, but I would grab the a 7 second audio clip starting 5 seconds in. So my new audio file would be from 5 to 12. I would think you could do similar things in FMOD because I’ve looked at the recording examples and gapless playback examples, etc. and it does seem to have that same functionality and access to seek the files.
Now I need to migrate this new file that will made in game to something UE4 would use. In FMOD, looking through the .h and .cpp files in the plugin files, I see accept Fmod events only to attach to a UObject. Since I've not made an event in FMOD Studio, I can't use these. What is the sound that createSound makes? is it a wav? and fsb? I just a have a sound, and don't know what format it is.
I can’t use designer to make this sound because its dependent on the player at any given time during play.
In Unity, what I did was access the buffer of an audio file, pull data from the buffer for any given time, and place in a new buffer that I then turned into a file. While pulling data, I would check buffer size and frequency of sound files to make sure I had a gapless playback. (Not perfect, but pretty darn close), I’d use the audio functions in Unity to convert my array of data into a useable audioclip and run it through a sound emitter. It was pretty nice. Because I would keep the original wav file muted, but looping. So the player never knew what they captured. It was really nice.
Since UE4 doesn’t allow access to uncompressed PCM data, I can't do this low level data manipulation in UE4. I had to use FMOD, but its proving to be just as difficult because either its not documented, or lacks the functionality I want. I need help please.
If the data that is created in createsound is just normal pcm wav file data, then I can use a standard AudioComponent, and just save it to a file, and pull it in from UE4. If it is, then I need to turn it into an event so I can use FMODPlayEventAttached from the FMOD plugin library.
I've made a few other posts in various locations that have all been silent. Any comment would be appreciated. I know I've been reading a lot of documentation these last few days on FMOD, but I still may have missed something if people want to point me in a better direction, or if you have something to add, feel free.
Thanks all. I hope I was descriptive enough.

Changin mp3 speed in Qt and C++ [QMediaPlayer]

I'm trying to develop a little application in which you can load a mp3 file and play it in variable speeds! (I know it already exists :-) )
I'm using Qt and C++. I already have the basic player but I'm stuck with the rate thing, because I want to change the rate smoothly (like in Mixxx) without stopping the playback! The QMediaPlayer always stops if I change the value and creates a gap in the sound. Also I don't want the pitch to change!
I already found something called "SoundTouch" but now I'm completely clueless what to do with it, how to process my mp3 data and how to get it to the player! The "SoundTouch" Library is capable of doing what I want, i got that from the samples on the homepage.
How do I have to import the mp3 file, so I can process it with the SoundTouch functions
How can I play the output from the SoundTouch function? (Perhaps QMediaPlayer can do the job?)
How is that stuff done live? I have to do some kind of stream I guess? So I can change the speed during play and keep on playing without gaps. Graphicaly in my head it has to be something that sits between the data and the player, where all data has to go through live, with a small buffer (20-50 ms or so) behind to avoid gaps during processing future data.
Any help appreciated! I'm also open to any another solution then "SoundTouch" as long as I can stay with Qt/C++!
(Second thing: I want to view a waveform overview aswell as moving part of it (around actual position of the song), so I could also use hints on how to get the waveform data)
Thanks in advance!
As of now (Qt 5.5) this is impossible to do with QMediaPlayer only. You need to do the following:
Decode the audio using GStreamer, FFMpeg or (new) QAudioDecoder: http://doc.qt.io/qt-5/qaudiodecoder.html - this will give you raw PCM stream;
Apply SoundTouch or some other library to this raw data to change the pitch. If GPL is ok, take a look at http://nsound.sourceforge.net/examples/index.html, if you develop proprietary stuff, STK might be a better choice: https://ccrma.stanford.edu/software/stk/
Output the modified data into audio device by using QAudioOutput.
This strategy uses Qt as much as possible, and brings you the best platform coverage (you still lose Android though as it does not support QAudioOutput)

Recording application output to video using FFmpeg (or similar)

We have a requirement to lets users record a video of our 3D application. I can already grab the individual rendered frames so this question is specifically about how to write frames into a video file.
I don't think writing each frame as a separate file and post-processing is a workable option.
I can look at options to record to a simple video file for later optimising/encoding, or writing directly to a sensibly encoded format.
FFmpeg was suggested in another post but it looks a bit daunting to me. Is it the best option, if not what can be suggested? We can work with LGPL but not full GPL.
We're working on Windows (Win32 not MFC) in C++. Sample/pseudo code with your recommended library is very much appreciated... basically after how to do 3 functions:
startRecording() does whatever initialization is needed
recordFrame() takes pointer to frame data and encodes it, ideally with timing data
endRecording() finalizes the video file, shuts down video system, etc
Check out the sources to Taksi on sourceforge. http://taksi.sourceforge.net/
You need 2 things.
1. A code to compress the frames.
2. A container file format. Like AVI or MPG.
Taksi useses the old VideoForWindows API and AVI not the newer COM API's but it still might work for you.