|
Is there any option to check how many bytes are waiting?
It's supposed to be some property of the NetworkStream?
|
|
|
|
|
Dear All,
I need to develop a program that gets signals continuously and then play them. So I play audio streams in 100ms periods. My problem is that there is an undesired sound in the onset and offset of each stream. For example I write a program which plays an audio stream with constant values as follow, but it makes a noisy sound instead of constant sound.
What's the problem? and what's the solution?
using System;
using System.Windows.Forms;
using Microsoft.DirectX.DirectSound;
using System.IO;
namespace TestSound
{
class CSound : Form
{
const int HEADER_SIZE = 44;
const bool FLAG_STEREO = true;
const short BITS_PER_SAMPLE = 16;
const int SAMPLE_RATE = 44100;
int numberOfSamples;
MemoryStream stream;
BinaryWriter writer;
Device ApplicationDevice = null;
SecondaryBuffer buffer = null;
BufferDescription description;
public CSound()
{
try
{
ApplicationDevice = new Device();
}
catch
{
MessageBox.Show("Unable to create sound device.");
ApplicationDevice = null;
return;
}
ApplicationDevice.SetCooperativeLevel(this, CooperativeLevel.Priority);
description = new BufferDescription();
description.ControlEffects = false;
stream = new MemoryStream();
writer = new BinaryWriter(stream);
}
private void AddHeader()
{
stream.Position = 0;
writer.Write(0x46464952);
writer.Write((int)(HEADER_SIZE + (numberOfSamples * BITS_PER_SAMPLE * (FLAG_STEREO ? 2 : 1) / 8)) - 8);
writer.Write(0x45564157);
writer.Write(0x20746d66);
writer.Write(16);
writer.Write((short)1);
writer.Write((short)(FLAG_STEREO ? 2 : 1));
writer.Write(SAMPLE_RATE);
writer.Write(SAMPLE_RATE * (FLAG_STEREO ? 2 : 1) * BITS_PER_SAMPLE / 8);
writer.Write((short)((FLAG_STEREO ? 2 : 1) * BITS_PER_SAMPLE / 8));
writer.Write(BITS_PER_SAMPLE);
writer.Write(0x61746164);
writer.Write((int)(numberOfSamples * BITS_PER_SAMPLE * (FLAG_STEREO ? 2 : 1) / 8));
}
public void Play(short[] samples)
{
if (ApplicationDevice == null)
return;
stream.Position = HEADER_SIZE;
numberOfSamples = samples.Length;
for (int i = 0; i < numberOfSamples; i++)
{
writer.Write(samples[i]);
if (FLAG_STEREO)
writer.Write(samples[i]);
}
AddHeader();
stream.Position = 0;
try
{
if (buffer != null)
{
buffer.Dispose();
buffer = null;
}
buffer = new SecondaryBuffer(stream, description, ApplicationDevice);
buffer.Play(0, BufferPlayFlags.Default);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
static short[] samples = new short[4410];
static CSound sound;
static void Main()
{
Form form = new Form();
form.Show();
sound = new CSound();
Random random = new Random();
for (int i = 0; i < samples.Length; i++)
samples[i] = 1000;
while (true)
{
sound.Play(samples);
System.Threading.Thread.Sleep(100);
}
}
}
}
|
|
|
|
|
Hi Everybody
With Graphics Class in C# for example I draw a line on the panel, But after minimizing the form and show again the line disappear in the other hands, after refreshing form all graphic objects will be disappear.
What is your recommendation for permanent graphic object in the form?
Best Regards,
Reza Shojaee
modified on Wednesday, August 19, 2009 1:01 AM
|
|
|
|
|
You are using the CreateGraphics method. This is completely useless unless you're drawing something temporary. Handle the Paint event and draw your line there, that's what it is for
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
i'm trying to create a question using a given paragraph dynamically, is there any one who has done this kind of function, at the moment i'm trying to do it using templates , but it doesn't seem to be work fine.
|
|
|
|
|
I've read this five times and I still have no idea what you mean. you want to take a random paragraph and rephrase it as a question ?
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
|
You want to generate questions based on some text you have?
I don't think you will be able to do this unless your code can "understand" the paragraph and then ask questions.
It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD
|
|
|
|
|
sorry for uncleanness, what i'm trying to do is, create a question relevant to a paragraph, means just imagine i have a C# article and i want to generate a question relevant to this article through a c# application.
i think this is not easy , but i want to know if there is a better way for this.
|
|
|
|
|
There is no trivial way to do this. I would suggest there's little chance of you coming up with a method that asks questions that appear to have been written by a human.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
As I said earlier, this can be done ONLY if the computer can UNDERSTAND the paragraph. And if the paragraph is a random one, there a very minute chance that this can be done.
That too only if the paragraph is like: "my name is XYZ" and questions should be like "what is my name". But to interpret the text, you need a human.
It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD
|
|
|
|
|
I'm trying to set ToolStripControlHost backcolor as ToolStripMenuItem but it only fill control's backcolor. I tried with custom Renderer and override almost every event method but nothing found working...
here is the screenshot of problem
http://i476.photobucket.com/albums/rr121/xmen_xwk/fullbackColor.png
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN%
R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
-----------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|
|
Hello,
I have implemented a logger which is writing the data to a text file. But, the file size gets huge if all the data is written to it.
I want to keep only last one hour data in file. The logger should keep writing all data till one hour and after that it should follow LIFO structure. So that the file size will not be uncontrolled.
How it can be done in C#?
Thanks a lot.
|
|
|
|
|
LIFO is going to be a PITA - why not split the log files into hourly files, just keep withing to the file name, on the hour create a new file and then your garbage collection is a simple jod to delete the old files. Saves thrashing around inside the text file.
|
|
|
|
|
Thanks Holmes,
Actually, the data is getting logged from two different events to same file and i am just writing over there. At a given condition only one event will be fired. The file creation is done in constructor and the same name is referred to write into it.
If we can limit either text length or size then it would be easier.
|
|
|
|
|
gajesh wrote: two different events
That makes your exposure worse, it will take an appreciable amount of time to extract the first in lines from a text file, read in whole file, remove the offending lines and write out the entire file each time you write an entry. You are open to locking if the events can fire too close in time.
I don't know of any way to remove the first in lines elegantly.
|
|
|
|
|
I'd suggest to try logging frameworks like log4net . It will work much better and it has this feature built in.
|
|
|
|
|
Thanks Navneeth,
The download seems to be very big (200+ MB)for log4net. I'll give a try to similar type of logger.
I'll also explore if i can create another file as suggested earlier.
|
|
|
|
|
gajesh wrote: The download seems to be very big (200+ MB)for log4net
From where did you download? i only got 7.8MB for it
|
|
|
|
|
gajesh wrote: The download seems to be very big (200+ MB)for log4net.
No. Its just 7.8MB. This[^] will be the download link. The log4net dll is just 224KB.
|
|
|
|
|
I guess you have to resort to using memory mapped files. And when an hour passes, you would have to move up the records except the first one (LIFO). Hope that helps.
|
|
|
|
|
I second log4net. Anything else is just asking for pain.
Cheers,
Vikram. (Proud to have finally cracked a CCC!)
|
|
|
|
|
In the unix world, there's a tool called logrotate, which basically loops over all your log files, removes the older lines, and backs them up in an archive - something like that would be relatively trivial to write in c#, and would keep the complexity out of your logging code. The only thing you'd have to be careful of, would be file lock clashes when both the logging tool and the rotate tool try to access the log at the same time
|
|
|
|
|
Thanks a lot,
I managed to toggle between two files as a logger as workaround. Still i am keen to explore the right and best way to implement this.
I'll post the solution as soon as i am done.
|
|
|
|
|
When you have the C# application compile an application. And Create an installer for it. Can C# Compact all the manafest files and resource file into the exe application file so that the application is self contained as 1 file instead of several files? Except for the xml or config file's?
|
|
|
|