Playing audio with directshow from resource file - c++

i am currently writing a script mod for Grand Theft Auto IV using Scripthook C++.
I have been playing audio files by using this template: Simple C++ MP3 Player Class
My problem starts by attempting to use files from resource.
I have no idea how to load them.
Every time i tried to load sound from resource using my code, game crashed.
Already spend hours browsing google about how to play DirectShow audio from resource without any result.
Inside resource files:
(resource.h) #define WAV_Sound 106
(resource.rc) WAV_Sound RCDATA "MySound.wav"
Loading files from HDD is working fine
Load(TEXT("MySound.wav"));
But game crashes when i try one of this:
// Load from resource #1
LPCWSTR file = MAKEINTRESOURCE(WAV_Sound);
Load(file);
// Load from resource #2
Load((LPCWSTR) WAV_Sound);
Hope someone can help me out, thanks !

Microsoft recommended using DirectX Audio to provides multilayer interfaces for developing game, namely XAudio2, X3DAudio, XACT, ... etc, they are very powerful to process sound effect in 2d/3d game environment. Here is the link http://msdn.microsoft.com/en-us/library/ee415737%28v=vs.85%29
Please download and install the DirectX software development kit, and find the 'XAudio2BasicSound' example to open wave file from file or in resource or in dll and playback with IXAudio2 interface, it was very simple example but next step you can add sound effect to your game.

Related

get the current frame of video c++

I am using visual studio.
I need to write a c++ dll, that plays video (h265 codec) in the background. The video shouldn't been seen, only the sound should be heard. I need to catch every frame and gives it to the host application.
I couldn't find a simple API that let me access the current frame of a player.
Any help would be appreciated
This question is very broad, but, there are three choices:
libVLC - easy to use and implement, but you don't have full access (but for your requirements, it should be enough)
ffmpeg - you can spawn the ffmpeg command line tool and just get play audio output. This should be easy. If you want more control, you can use ffmpeg as library, what is kinda hard to use and implement, but you have full control over process of decoding and playback
gstreamer - same as ffmpeg

Convert frames to video on demand

I'm working on a c++ project that generates frames to be converted to a video later.
The project currently dumps all frames as jpg or png files in a folder and then I run ffmpeg manually to generate a mp4 video file.
This project runs on a web server and an ios/android app (under development) will call this web server to have the video generated and downloaded.
The web service is pretty much done and working fine.
I don't like this approach for obvious reasons like a server dependency, cost etc...
I successfully created a POC that exposes the frame generator lib to android and I got it to save the frames in a folder, my next step now is to convert it to video. I considered using any ffmpeg for android/ios lib and just call it when the frames are done.
Although it seems like I fixed half of the problem, I found a new one which is... each frame depending on the configuration could end up having 200kb+ in size, so depending on the amount of frames, it will take a lot of space from the user's device.
I'm sure this will become a huge problem very easily.
So I believe that the ideal solution would be to generate the mp4 file on demand as each frame is created, so in the end there would be no storage space being taken as I woudn't need to save a file for the frame.
The problem is that I don't know how to do that, I don't know much about ffmpeg, I know it's open source but I have no idea how to include a reference to it from the frames generator and generate the video "on demand".
I heard about libav as well but again, same problem...
I would really appreciate any sugestion on how to do it. What I need is basically a way to generate a mp4 video file given a list of frames.
thanks for any help!

Record directshow audio device to file

I've stumbled through some code to enumerate my microphone devices (with some help), and am able to grab the "friendly name" and "clsid" information from each device.
I've done some tinkering with GraphEd.exe to try and figure out how I can take audio from directshow and write it to a file (I'm not currently concerned about the format, wav should be fine), and can't seem to find the right combination.
One of the articles I've read linked to this Windows SDK sample, but when I examined the code, I ended up getting pretty confused at how to use that code, ie. setting the output file, or specifying which audio capture device to use.
I also came across a codeguru article that has a nicely featured audio recorder, but it does not have an interface for selecting the audio device, and I can't seem to find where it statically picks which recording device to use.
I think I'd be most interested in figuring out how to use the Windows SDK sample, but any explanation on either of the two approaches would be fantastic.
Edit: I should mention my knowledge and ability as a win32 COM programmer is very low on the scale, so if this is easy, just explain it to me like I'm five, please.
Recording audio into file with DirectShow needs you to build the right filter graph, as you should have figured out already. The parts include:
The device itself, which you instantiate via moniker (not CLSID!), it is typically PCM format
Multiplexer component that converts streams into container format
File Writer Filter that takes file-compatible stream and writes into a file
The tricky moment is #2 since there is not standard component available. Windows SDK samples however contains the missing part - WavDest Filter Sample. Building it and making it ready for use, you can build a graph that records from device into .WAV file.
Your graph will look like this, and it's built easily programmatically as well:
I noticed that I have a variation of WavDest installed with Google Earth - for the case you have troubles building it yourself and you will be looking for prebuilt binary.
You can instruct ffmpeg to record from a directshow device, and output to a file.

Play and pause an .wav or .mp3 file on windows

I am working on a console application where i have to deal with multiple sound file with all of same type(.wav or .mp3). I have the option to chose between the two.
I can play the sound fine using PlaySound() function but i need some extra features.
I need pausing the sound and then play from the position where it paused last time.
OR
Set a time from where playing should start and time for which it should play.
1 of the two options will do the job.
I am working on windows with DEV C++ compiler.
Any help and ideas?
Use the Windows Media Player activeX control for anything more sophistcated than straight playing of a WAV file. It is far easier.
See "Using the Windows Media Player Control in a C++ Program" on MSDN:
http://msdn.microsoft.com/en-us/library/windows/desktop/dd564580(v=vs.85).aspx

playing audio files and adjusting system volume

I'm writing a tiny music program and I'm trying to play an mp3. Right now, I have something pretty ugly but it works:
std::string command("cd \"C:\\Program Files (x86)\\Windows Media Player\" && wmplayer /play \"D:\\music\\A Perfect Circle\\eMOTIVe\\a.mp3");
system(command.c_str());
Is there a better or more reliable way to do this? Also, how would one go about adjusting the master system volume?
If there is an MP3 codec installed in Windows, you might be able to use the Win32 API PlaySound() function or the VCL TMediaPlayer component to play the file.
To set the master volumne, use the Win32 API waveOutSetVolume() function.
I've created a C++ audio library named "Crosstalk".
Crosstalk is a real-time C++ audio engine that allows you to create and route audio systems.
Here's an example of how you can play your mp3 file:
XtSystem system;
XtMp3Decoder mp3Decoder;
XtAudioDevice audioDevice;
long md = system.addComponent(&mp3Decoder);
long ad = system.addComponent(&audioDevice);
system.connOutToIn(md,0,ad,0);
system.connOutToIn(md,1,ad,1);
mp3Decoder.loadFile("../05 Tchaikovski-Swan Lake-Scene.mp3");
mp3Decoder.play();
Included with Crosstalk are example Xcode and Visual Studio projects.
You can download Crosstalk and check out the API documentation and licensing details here.
EDIT:
In terms of setting volume: there is a component included with Crosstalk called "XtGain" that you can wire in series between the mp3 decoder outputs and the audio device inputs. This will allow you to set the volume of the audio stream before it reaches the speakers.
EDIT (01-12-2012):
Crosstalk has been replaced by an open-source project called "DSPatch". DSPatch is essentially an upgraded version of the routing engine behind Crosstalk that is no longer limited to only audio processing. DSPatch allows you to create and route almost any type of process chain imaginable, and its free for personal AND proprietary use :)