Simple alternative to VolumeSampleProvider that will have a left and right volume property - visual-studio

when playing trying to play audio in a chat application that I'm making I got the exception {"Source sample provider must be mono"} in this line var panProvider = new PanningSampleProvider(volumeProvider);
Code:
private void ReceiveUdpMessage(IAsyncResult ar)
{
try
{
byte[] bytesRead = UDPc.EndReceive(ar, ref ep);
var waveProvider = new BufferedWaveProvider(new WaveFormat(44100, 16, 2));
waveProvider.DiscardOnBufferOverflow = true;
waveProvider.AddSamples(bytesRead, 0, bytesRead.Length);
var volumeProvider = new VolumeSampleProvider(waveProvider.ToSampleProvider());
var panProvider = new PanningSampleProvider(volumeProvider);
mixer.AddMixerInput(panProvider);
UDPc.BeginReceive(new AsyncCallback(ReceiveUdpMessage), null);
}
catch(Exception ex)
{
}
UDPc.BeginReceive(new AsyncCallback(ReceiveUdpMessage), null);
}
I saw this answer Implementing Output audio panning with Naudio
but when mark answered in the comments:"I'd make a very simple alternative to VolumeSampleProvider that had a left and right volume property in that case".
he didn't elaborate and I'm new to this so have no idea what to do from here.
Does someone know what I'm supposed to do?
Thx

Related

.Audio Timeout Error: NET Core Google Speech to Text Code Causing Timeout

Problem Description
I am a .NET Core developer and I have recently been asked to transcribe mp3 audio files that are approximately 20 minutes long into text. Thus, the file is about 30.5mb. The issue is that speech is sparse in this file, varying anywhere between 2 minutes between a spoken sentence or 4 minutes of length.
I've written a small service based on the google speech documentation that sends 32kb of streaming data to be processed from the file at a time. All was progressing well until I hit this error that I share below as follows:
I have searched via google-fu, google forums, and other sources and I have not encountered documentation on this error. Suffice it to say, I think this is due to the sparsity of spoken words in my file? I am wondering if there is a programmatical centric workaround?
Code
I have used some code that is a slight modification of the google .net sample for 32kb streaming. You can find it here.
public async void Run()
{
var speech = SpeechClient.Create();
var streamingCall = speech.StreamingRecognize();
// Write the initial request with the config.
await streamingCall.WriteAsync(
new StreamingRecognizeRequest()
{
StreamingConfig = new StreamingRecognitionConfig()
{
Config = new RecognitionConfig()
{
Encoding =
RecognitionConfig.Types.AudioEncoding.Flac,
SampleRateHertz = 22050,
LanguageCode = "en",
},
InterimResults = true,
}
});
// Helper Function: Print responses as they arrive.
Task printResponses = Task.Run(async () =>
{
while (await streamingCall.ResponseStream.MoveNext(
default(CancellationToken)))
{
foreach (var result in streamingCall.ResponseStream.Current.Results)
{
//foreach (var alternative in result.Alternatives)
//{
// Console.WriteLine(alternative.Transcript);
//}
if(result.IsFinal)
{
Console.WriteLine(result.Alternatives.ToString());
}
}
}
});
string filePath = "mono_1.flac";
using (FileStream fileStream = new FileStream(filePath, FileMode.Open))
{
//var buffer = new byte[32 * 1024];
var buffer = new byte[64 * 1024]; //Trying 64kb buffer
int bytesRead;
while ((bytesRead = await fileStream.ReadAsync(
buffer, 0, buffer.Length)) > 0)
{
await streamingCall.WriteAsync(
new StreamingRecognizeRequest()
{
AudioContent = Google.Protobuf.ByteString
.CopyFrom(buffer, 0, bytesRead),
});
await Task.Delay(500);
};
}
await streamingCall.WriteCompleteAsync();
await printResponses;
}//End of Run
Attempts
I've increased the stream to 64kb of streaming data to be processed and then I received the following error as can be seen below:
Which, I believe, means the actual api timed out. Which is decidely a step in the wrong direction. Has anybody encountered a problem such as mine with the Google Speech Api when dealing with a audio file with sparse speech? Is there a method in which I can filter the audio down to only spoken words progamatically and then process that? I'm open to suggestions, but my research and attempts have only lead me to further breaking my code.
There is to way for recognize audio in Google Speech API:
normal recognize
long running recognize
Your sample is uses the normal recognize, which has a limit for 15 minutes.
Try to use the long recognize method:
{
var speech = SpeechClient.Create();
var longOperation = speech.LongRunningRecognize( new RecognitionConfig()
{
Encoding = RecognitionConfig.Types.AudioEncoding.Linear16,
SampleRateHertz = 16000,
LanguageCode = "hu",
}, RecognitionAudio.FromFile( filePath ) );
longOperation = longOperation.PollUntilCompleted();
var response = longOperation.Result;
foreach ( var result in response.Results )
{
foreach ( var alternative in result.Alternatives )
{
Console.WriteLine( alternative.Transcript );
}
}
return 0;
}
I hope it helps for you.

How to refresh images in every second in windows 7 phone

i am showing images from server. In server image is changing in every second. I want that in my application image should be change automatically after one second.M new in windows 7 programming. Kindly suggest me where i am lacking in concept. M using this code.
This process will start when i will tab on my image.
private void image1_Tap(object sender, GestureEventArgs e)
{
System.Windows.Threading.DispatcherTimer dt = new System.Windows.Threading.DispatcherTimer();
dt.Interval = new TimeSpan(0, 0, 0, 0, 1000); // 500 Milliseconds
dt.Tick += new EventHandler(dt_Tick);
dt.Start();
}
This is calling this method .
void dt_Tick(object sender, EventArgs e)
{
status.Text = "chking" + counter++;
// Do Stuff here.
image1.Source = null;
Uri imgUri = new Uri(base_url,UriKind.Absolute);
BitmapImage BI = new BitmapImage(imgUri);
int H = BI.PixelHeight;
int w = BI.PixelWidth;
image1.Source = BI;
}
In this code my Counter is working fine and status.Text is sucessfully change in every second. But image is changing once after that its not changing.
Kinldy suggest me where i am commiting mistake.
Thanks in advance
Gaurav Gupta
I think you should declare System.Windows.Threading.DispatcherTimer dt = new System.Windows.Threading.DispatcherTimer(); as a member variable instead of declaring it in the images tap event.
I do the same thing when grabbing images from a camera in my wp8 app. I hold the URL including the current datetime-value as a url-param in my viewmodel. When i want to refresh, i just reset my URL-property.
Here's my sample:
this.MyUrlProperty = string.Format("{0}?timestamp={1:yyyy-MM-dd HH:mm:ss:fff}", _originalCameraUrl, DateTime.Now);
Works great for me...

Record sounds from speaker in silverlight wp7

I need to record different sounds in a file. the file may be .mp3, .wav etc. how it is possible in windows phone 7?
There is a simple way to do this in Windows Phone. You are basically using the Microphone class provided by the framework. For a great article on the topic go to http://msdn.microsoft.com/en-us/magazine/gg598930.aspx
void OnRecordButtonClick(object sender, RoutedEventArgs args)
{
if (microphone.State == MicrophoneState.Stopped)
{
// Clear the collection for storing buffers
memoBufferCollection.Clear();
// Stop any playback in progress (not really necessary, but polite I guess)
playback.Stop();
// Start recording
microphone.Start();
}
else
{
StopRecording();
}
// Update the record button
bool isRecording = microphone.State == MicrophoneState.Started;
UpdateRecordButton(isRecording);
}
void StopRecording()
{
// Get the last partial buffer
int sampleSize = microphone.GetSampleSizeInBytes(microphone.BufferDuration);
byte[] extraBuffer = new byte[sampleSize];
int extraBytes = microphone.GetData(extraBuffer);
// Stop recording
microphone.Stop();
// Create MemoInfo object and add at top of collection
int totalSize = memoBufferCollection.Count * sampleSize + extraBytes;
TimeSpan duration = microphone.GetSampleDuration(totalSize);
MemoInfo memoInfo = new MemoInfo(DateTime.UtcNow, totalSize, duration);
memoFiles.Insert(0, memoInfo);
// Save data in isolated storage
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = storage.CreateFile(memoInfo.FileName))
{
// Write buffers from collection
foreach (byte[] buffer in memoBufferCollection)
stream.Write(buffer, 0, buffer.Length);
// Write partial buffer
stream.Write(extraBuffer, 0, extraBytes);
}
}
// Scroll to show new MemoInfo item
memosListBox.UpdateLayout();
memosListBox.ScrollIntoView(memoInfo);
}

xuggler: no video in encoded 3gp file

i am trying to encode videos into 3gp format using xuggler, i somehow got it to work, work as in the program stopped throwing errors and exceptions, but the new file that is created does not have any video. Now there is no error or exception for me to work with so i have stuck a wall.
EDIT: Note the audio is working as it shud.
This is the code for the main function where the listeners are configured
IMediaReader reader = ToolFactory.makeReader("/home/hp/mms/b.flv");
IMediaWriter writer = ToolFactory.makeWriter("/home/hp/mms/xuggle/a_converted.3gp", reader);
IMediaDebugListener debugListener = ToolFactory.makeDebugListener();
writer.addListener(debugListener);
ConvertVideo convertor = new ConvertVideo(new File("/home/hp/mms/b.flv"), new File("/home/hp/mms/xuggle/a_converted.3gp"));
// convertor.addListener(writer);
reader.addListener(writer);
writer.addListener(convertor);
while (reader.readPacket() == null)
;
And this is the code for the convertor that i wrote.
public ConvertVideo(File inputFile, File outputFile)
{
this.outputFile = outputFile;
reader = ToolFactory.makeReader(inputFile.getAbsolutePath());
reader.addListener(this);
}
private IVideoResampler videoResampler = null;
private IAudioResampler audioResampler = null;
#Override
public void onAddStream(IAddStreamEvent event)
{
if (writer == null)
{
writer = ToolFactory.makeWriter(outputFile.getAbsolutePath(), reader);
}
int streamIndex = event.getStreamIndex();
IStreamCoder streamCoder = event.getSource().getContainer().getStream(streamIndex).getStreamCoder();
if (streamCoder.getCodecType() == ICodec.Type.CODEC_TYPE_AUDIO)
{
streamCoder.setFlag(IStreamCoder.Flags.FLAG_QSCALE, false);
writer.addAudioStream(streamIndex, 0, 1, 8000);
}
else if (streamCoder.getCodecType() == ICodec.Type.CODEC_TYPE_VIDEO)
{
streamCoder.setFlag(IStreamCoder.Flags.FLAG_QSCALE, false);
streamCoder.setCodec(ICodec.findEncodingCodecByName("h263"));
writer.addVideoStream(streamIndex, 0, VIDEO_WIDTH, VIDEO_HEIGHT);
}
super.onAddStream(event);
}
// //
#Override
public void onVideoPicture(IVideoPictureEvent event)
{
IVideoPicture pic = event.getPicture();
if (videoResampler == null)
{
videoResampler = IVideoResampler.make(VIDEO_WIDTH, VIDEO_HEIGHT, pic.getPixelType(), pic.getWidth(), pic.getHeight(), pic.getPixelType());
}
IVideoPicture out = IVideoPicture.make(pic.getPixelType(), VIDEO_WIDTH, VIDEO_HEIGHT);
videoResampler.resample(out, pic);
IVideoPictureEvent asc = new VideoPictureEvent(event.getSource(), out, event.getStreamIndex());
super.onVideoPicture(asc);
out.delete();
}
#Override
public void onAudioSamples(IAudioSamplesEvent event)
{
IAudioSamples samples = event.getAudioSamples();
if (audioResampler == null)
{
audioResampler = IAudioResampler.make(1, samples.getChannels(), 8000, samples.getSampleRate());
}
if (event.getAudioSamples().getNumSamples() > 0)
{
IAudioSamples out = IAudioSamples.make(samples.getNumSamples(), samples.getChannels());
audioResampler.resample(out, samples, samples.getNumSamples());
AudioSamplesEvent asc = new AudioSamplesEvent(event.getSource(), out, event.getStreamIndex());
super.onAudioSamples(asc);
out.delete();
}
}
I just cant seem to figure out where the problem is. I wud be thankful if someone wud plz point me in the right direction.
EDIT: If i see the properties of my newly encoded video, its audio properties are set and its video properties are not i.e in video properties, dimension= 0 x 0, frame rate= N/A and codec= h.263. The problem here is the 0 x 0 dimension.
well i found the answer, well not exactly the answer but a way to do what i was doing. Right now i am not quiet sure why my code was not working but the hre u can find the solution that worked for me. the author here just makes a seperate resizer class, adds it as a listener to the reader,. It has the onPictureEvent method overridden. Then he makes another class MyVideoListener and overrides the onAddStream method and adds it as a listener to the writer. and then he links the two parts by adding writer as a listener to resizer. works like a a charm.

Transition of images in Windows Forms Picture box

I'm new to Windows Forms, in my project, i need to change the image in the picture box at runtime. I'm able to do that with the help of a timer. The picture just gets changed. Is it possible to do some transitions when image changes, for example fade in, fade out, blur etc.. If possible could some one please let me know how to do it. I searched in net but in vain.Thanks in advance.
Varun
Simply take new code file and paste below code in it
an original answer for the similar question, answer taken from another question
Answer
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
public class BlendPanel : Panel
{
private Image mImg1;
private Image mImg2;
private float mBlend;
public BlendPanel()
{
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);
}
public Image Image1
{
get { return mImg1; }
set { mImg1 = value; Invalidate(); }
}
public Image Image2
{
get { return mImg2; }
set { mImg2 = value; Invalidate(); }
}
public float Blend
{
get { return mBlend; }
set { mBlend = value; Invalidate(); }
}
protected override void OnPaint(PaintEventArgs e)
{
if (mImg1 == null || mImg2 == null)
e.Graphics.FillRectangle(new SolidBrush(this.BackColor), new Rectangle(0, 0, this.Width, this.Height));
else
{
Rectangle rc = new Rectangle(0, 0, this.Width, this.Height);
ColorMatrix cm = new ColorMatrix();
ImageAttributes ia = new ImageAttributes();
cm.Matrix33 = mBlend;
ia.SetColorMatrix(cm);
e.Graphics.DrawImage(mImg2, rc, 0, 0, mImg2.Width, mImg2.Height, GraphicsUnit.Pixel, ia);
cm.Matrix33 = 1F - mBlend;
ia.SetColorMatrix(cm);
e.Graphics.DrawImage(mImg1, rc, 0, 0, mImg1.Width, mImg1.Height, GraphicsUnit.Pixel, ia);
}
base.OnPaint(e);
}
}
Build your project. You can now drop a BlendPanel from the top of the toolbox onto your form. Here's a sample program that uses it:
private float mBlend;
private int mDir = 1;
public int count = 0;
public Bitmap[] pictures;
public void myPhoto()
{
pictures = new Bitmap[9];
pictures[0] = new Bitmap(#"Library Images\cf3.jpg");
pictures[1] = new Bitmap(#"Library Images\cf4.jpg");
pictures[2] = new Bitmap(#"Library Images\l1.JPG");
pictures[3] = new Bitmap(#"Library Images\l2.JPG");
pictures[4] = new Bitmap(#"Library Images\l3.JPG");
pictures[5] = new Bitmap(#"Library Images\l4.JPG");
pictures[6] = new Bitmap(#"Library Images\l5.JPG");
pictures[7] = new Bitmap(#"Library Images\l6.JPG");
pictures[8] = new Bitmap(#"Library Images\l7.JPG");
timer1.Interval = 50; //time of transition
timer1.Tick += BlendTick;
try
{
blendPanel1.Image1 = pictures[count];
blendPanel1.Image2 = pictures[++count];
}
catch
{
}
timer1.Enabled = true;
}
private void BlendTick(object sender, EventArgs e)
{
mBlend += mDir * 0.02F;
if (mBlend > 1)
{
mBlend = 0.0F;
if ((count + 1) < pictures.Length)
{
blendPanel1.Image1 = pictures[count];
blendPanel1.Image2 = pictures[++count];
}
else
{
blendPanel1.Image1 = pictures[count];
blendPanel1.Image2 = pictures[0];
count = 0;
}
}
blendPanel1.Blend = mBlend;
}
You'll need to modify the new Bitmap(#"yourimagePath"); calls. Build and run. You should see the displayed image smoothly morph from your first image to your second image without any flickering.
I hope it helps for other...
There is no built-in support for such effects, but you can implement them. I'd suggest to write a custom control that renders the image and have a method for fade-swap, fade itself can be reached with alpha-blending drawing with .NET Graphics class.
However, Graphics class isn't very fast, I don't recommend to use this technique for big images. If you need some fancy UI with hw-accelerated effects, take a look at WPF.
Blend effects are easy to get going by using the ColorMatrix class. There's a good example available in my answer in this thread.
A simple way to get a blur is to resize the image, making it smaller, then redraw it back, making it larger. The Graphics.InterpolationMode property affects the type of blur you'll get.
Those are quicky do-it-yourself solutions. Any decent graphics library has these kind of operations built-in. You probably want something free, check out ImageMagick.NET
To put it simply, not without external (3rd-party) libraries.

Resources