Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Continuous tone generation

Tags:

c#

.net

I am creating a small project that eventually will be used in my final year project next year.

So right now I have created a small sensor using USB (virtual COM port) and returning a set a values from 0 to 100. I have a progress bar which basically goes up and down depending the return value from the sensor via the virtual COM port.

Now my problem lies here. When the sensor in pushed hard enough and a value of 100 is returned I want to sound a small blip (this part is done and working). But I also want that when the values of the sensor range from 50 to round 95, a tone is generated and is output via the speakers.

The tone should be continuous, so if the user presses the sensor, and let's say the value is 65, if the user does not remove pressure from the sensor, it should continue to output a flat tone. If the user presses a little harder and let's say a value of 75 is returned, I want to increase the tone frequency.

The most important thing is that it is a continuous tone for values that come from the sensor that are from 50 to 95 where 50 will sound a low frequency and 95 will be output a higher frequency. This should be in real time.

The code is all done via C# and I would like to use a library for C#. I never used DirectX and am not really willing to download a huge library if not really necessary.

I am willing to buy some small library or use an open source library as long as the price of the library is not more than $50 or in that range.

like image 674
Graham Hili Avatar asked Jun 27 '11 19:06

Graham Hili


2 Answers

Well, actually you don't need to buy a library for that. You can use the free NAudio library. It will allow you to create and play WAV files. Have a look at the capabilities. This might also be helpful:

  • Introduction to Using NAudio
  • Creating sine or square wave in C#
  • Playback of Sine Wave in NAudio

Now you can easily do something like:

loop_to_detect_sensor_input()
{ 
   freq = somefunction(SensorValue);
   generate_audio = SomeFunc1(freq);
   Play_the_generated_audio;
}
like image 163
TarunG Avatar answered Sep 21 '22 10:09

TarunG


Starting from C# 2.0 the System.Console has a Beep variant, which takes frequency and duration: http://msdn.microsoft.com/en-us/library/4fe3hdb1.aspx. I haven't ever used it, but you can scale your 0-100 intensity range into a frequency range. You can play the beep asynchronously in a background thread. What maybe hard is to hold the note continuously, that needs precise timing. Another drawback can be from the MSDN page Remarks section: "method is not supported on the 64-bit editions of Windows Vista and Windows XP". Probably this will use much less resources then generating a wave and then playing it.

like image 36
Csaba Toth Avatar answered Sep 21 '22 10:09

Csaba Toth