NAudio set Left and Right speakers levels through my code (Balance control) - c#

I'm trying to control the volume of the left speaker and right speaker separately through my app. I'm using the library NAUDIO. At the moment i am trying to write a program that will change the master volume of the system depending on a buttons that exist in my app. off course, the windows form, but i am unable to understand how to control volume. I need it to be specific to the master volume. Is there any class to do so?
Volume mixer, Device, Speakers properties, Levels, Balance
Regards

I'm not sure if this is what you're looking for but you could do something like this...
MMDeviceEnumerator deviceiterator = new MMDeviceEnumerator();
MMDeviceCollection devices = deviceiterator.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active);
foreach(MMDevice device in devices)
{
// Go through the devices you want to update and set your volume..
device.EndpointVolume.MasterVolumeLevelScalar = 0.8f;
}
According to NAudio, the max volume as a scalar is 1.0f. So setting it to 0.8f would essentially change your master control volume to 80.
Hope it helps. I'm still trying to figure out NAudio myself so good luck :-).

Related

Controlling Sounds in audio in multiple forms

I want to play a playlist in the resources folder for a windows forms project I'm working on (wav or mp3). The application will be completely offline. (actually a game project)
What I want to do is this:
I need to be able to stop or convert a music that I created in one form to another sound file, depending on the situation.
In this case, I couldn't figure out how to write a code block. There are many examples that describe how we can pause and start the music on single form, but I have not come across an example that covers the whole project and can be intervened from different forms.
I'm not sure if I explained exactly what I wanted to ask, but I can give an example as follows. Let's say I created 3 forms.
1st form and 2nd form game menus.
In these menus, the same music should continue to play.
But when I reach the 3rd form, this music should be cut and replaced with in-game music.
What I want to ask is: How can I control the music I created in form 1, in form 2 or form 3?
The reason I don't use the soundplayer class is because I have to play multiple sounds at once.
i.e., somehow I need to make the music player "static" so that I can control it from all forms, but I couldn't find the way around it.
var importer = new RawSourceWaveStream(Properties.Resources.sound, new WaveFormat());
var soundFx = new WaveOut();
soundFx.Init(importer);
soundFx.Play();
For example, in this way, I start playing the music in the form1. I need to give the command to change this music in the form3, but somehow I cannot reach the "soundfx" object that I defined there because I cannot make this process static.
I think that your intuition is valid. It is probably not the best way, but I inffer that it is a way that you would manage to do.
Have a public static class, that holds a private (or public) instance of you sound player. Have public static methods of the different things that you want to do with the audio player.
Control that class from all forms.
Since this is your intuition. I am curious of what have you so far in this way?
Anyhow, I have a few questions.
When you say "convert" the music, what do you mean?
What do you mean that you couldnt find a way around it?
Can you add some code of how you are controlling the sounds? With some code, people will be able to better support you.
It is recommended that you use MediaPlayer. From your description, MediaPlayer can better meet your needs. Compared with SoundPlayer, MediaPlayer has the following characteristics:
Multiple sounds can be played at the same time (create multiple MediaPlayer objects);
You can adjust the volume (Volume property);
You can use Play, Pause, Stop and other methods to control;
You can set the IsMuted property to True to achieve mute;
You can use the Balance property to adjust the balance of the left and right speakers;
The speed of audio playback can be controlled through the SpeedRatio property;
The length of the audio can be obtained through the NaturalDuration property, and the current playback progress can be obtained through the Position property;
Seek can be performed through the Position property;
Use MediaPlayer to play audio files as follows:
MediaPlayer player = new MediaPlayer();
player.Open(new Uri("BLOW.WAV", UriKind.Relative));
player. Play();
A MediaPlayer object can only play one file at a time. And the file is played asynchronously, you can also call Close to release the file.
For details, please refer to https://learn.microsoft.com/en-us/dotnet/api/system.windows.media.mediaplayer?view=windowsdesktop-7.0
When using it in VS, you need to perform the following steps first:
This way you can find MediaPlayer in the toolbar.
If you still have any questions please speak up.

Display Current Video in Windows Phone 8 using AudioVideoCaptureDevice?

I've managed to setup code for a Windows Phone 8 Application that initializes and can start/stop recording video using an AudioVideoCaptureDevice. (saves it to an IRandomAccessStream)
//Initialize Camera Recording
Windows.Foundation.Size resolution = new Windows.Foundation.Size(640, 480);
captureDevice = await AudioVideoCaptureDevice.OpenAsync(CameraSensorLocation.Back, resolution);
captureDevice.VideoEncodingFormat = CameraCaptureVideoFormat.H264;
captureDevice.AudioEncodingFormat = CameraCaptureAudioFormat.Aac;
captureDevice.RecordingFailed += captureDevice_RecordingFailed;
However, I cannot figure out how to hook this recording up to a VideoBrush to display the recording to the user. I want the user to be able to see the video they are recording as it is happening...
I know there is a tutorial that shows how to do this using the old APIs for Windows Phone 7 (CaptureSource, VideoDevice, etc.) but I specifically need to use the AudioVideoCaptureDevice to record. Anyone know how to display this video on screen?
Well, I was able to solve my problem.
Apparently there is a library in Microsoft.Devices that contains extensions for the VideoBrush class.
Therefore, in order to set the videobrush source to an AudioVideoCaptureDevice, you must first have:
using Microsoft.Devices;
at the top of your class in which your using the videobrush.
Hope this is able to help someone else.
You should be able to simply use VideoBrush.SetSource(captureDevice).

Remove Screen Capture from Video Device Type

in Microsoft.Expression.Encoder is it possible to remove Screen capture from Video device type.
If I do
VideoDevices = EncoderDevices.FindDevices(EncoderDeviceType.Video);
It returns me 2 device ( The webcam plugged in and the Screen capture )
Tho i don't want the ability to use Screen Capture as the recording device.
Should i just remove it manually while checking the name of it, or did they add some kind of functionnality to only get webcam devices in video devices.
Thanks.
Yes, filtering according to a string match seems to be your best choice.
Either filter for the name "Screen Capture Source" or the fact that it's DevicePath does not contain the substring "usb".

Windows 8 app XNA on two monitors with different resolutions

I'm using Monogame to write an XNA game for Windows 8 app store. I'm also using a laptop hooked up to an external monitor. Naturally the resolution on my external monitor is much higher than my laptop's screen. When I drag the app from one screen to another the resolution on the view port changes.
In my constructor I'm using
_graphics = new GraphicsDeviceManager(this);
_graphics.PreferredBackBufferHeight = 768;
_graphics.PreferredBackBufferWidth = 1366;
To set my viewport resolution. This makes the app to work fine when the application runs on either monitors, however dragging the app from one monitor to another changes the resolution on the GraphicsDeviceManager. Is there anyway to prevent this change?
So I figured it out
First I wrote a method that checks to see if the port resolution on the graphic device has changed
private bool hasResolutionChanegd()
{
if ((GraphicsDevice.Viewport.Width != ScreenManager.Instance.ScreenWidth) || (GraphicsDevice.Viewport.Height != ScreenManager.Instance.ScreenHeight))
{
return true;
}
else
{
return false;
}
}
I call this method on every update
if (hasResolutionChanegd())
{
Debug.WriteLine("Resolution Change new width= " + GraphicsDevice.Viewport.Width +" new height="+ GraphicsDevice.Viewport.Height);
_graphics.PreferredBackBufferHeight = 768;
_graphics.PreferredBackBufferWidth = 1366;
_graphics.ApplyChanges();
}
This way every time the resolution changes on the Graphic Device Manager (when the user drags the app from one screen environment to another), the preferred resolution is enforced.
I get the same thing as the original poster, but with XNA, not MonoGame. When I drag the window between the two monitors, the resolution changes, but the ClientSizeChanged event is not (of course) triggered. The suggestion above to use the SizeChanged event is helpful but only for Windows8 the documentation says.
I appear to have fixed it by handling the Window.ScreenDeviceNameChanged event - I hooked it up to the same handler as the ClientSizeChanged.
I would have put this in as a comment to the original post, but I don't have enough "reputation" points for the system to let me.

4 point multitouch gestures in WP7 (silverlight)

I want to use 4 point multitouch gestures in my app. The app is in silverlight (not xna), but the gestures won't apply to any controls, they will just check if user drags 4 fingers to the left or to the right of the screen.
Are there any libraries that I can use? Or what is the easiest way to implement it on my own? Can I use XNA multitouch libraries?
Cheers
As you are probably aware the WP7 silverlight API makes the assumption of two contact points for multi touch i.e. PinchStarted, PinchDelta, and PinchCompleted.
Please Check out the TouchPanel class which is in the Microsoft.Xna.Framework.Input.Touch namespace.
//Determine the maximum number of touches permited (four for WP7):
TouchPanelCapabilities tc = TouchPanel.GetCapabilities();
if(tc.IsConnected)
{
return tc.MaximumTouchCount;
}
//To read multitouch data from the touch input device you can do the following:
// Process touch events
TouchCollection touchColl = TouchPanel.GetState();
foreach (TouchLocation t in touchColl)
{
if ((t.State == TouchLocationState.Pressed)
|| (t.State == TouchLocationState.Moved))
{
//You can check the coordinates of each point (and the previous coordinate TryGetPreviousLocation())
float xcoordiante = t.Position.X;
float ycoordiante = t.Position.Y;
//Determine if touch point was moved/pressed or released use the State property
TouchLocationState st = t.State;
}
}
More details can be found here: http://msdn.microsoft.com/en-us/library/ff827744.aspx
I have not seen any libraries that specifically target 4 point touch, however, if you are looking for libraries that help with multi touch debugging I would strongly recommend http://multitouch.codeplex.com/.
The Silverlight WP7 Toolkit is awesome for doing Gesture stuff.
Download WP7 Toolkit
Then check out this awesome tutorial

Categories

Resources