6

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

2 Answers2

2

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:

Now you can easily do something like:

loop_to_detect_sensor_input()
{ 
   freq = somefunction(SensorValue);
   generate_audio = SomeFunc1(freq);
   Play_the_generated_audio;
}
Community
  • 1
  • 1
TarunG
  • 602
  • 5
  • 21
1

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.

Csaba Toth
  • 10,021
  • 5
  • 75
  • 121
  • "Plays the sound of a beep of a specified frequency and duration through the console speaker." I think from memory - could be wrong - that plays through the speaker built into the computer/attached to the motherboard rather than through the Windows audio system, and not all modern PCs have such a speaker. This may or may not affect the OP but thought I should mention it. – Stephen Kennedy Aug 28 '13 at 16:26
  • @StephenKennedy Good point. He has to decide if it's fine with him, there a trade off. Most of the devices have some kind of speaker at least for BIOS POST messages. The concern could be rather quality of the sound, or if he wants to use headphones. – Csaba Toth Aug 28 '13 at 16:40
  • +1. The tone will not be 100% continuous, but this is by far the simplest solution. – Peter Mortensen Nov 15 '13 at 14:55
  • However, [it does not work on Windows XP 64-bit and Windows Vista (32-bit and 64-bit)](http://stackoverflow.com/questions/14042630/how-to-generate-sounds-according-to-frequency/14042786#14042786) (I just verified it empirically on Windows 64-bit). – Peter Mortensen Nov 15 '13 at 17:14
  • @PeterMortensen Yes, the MSDN article's Remarks mention this "method is not supported on the 64-bit editions of Windows Vista and Windows XP". The .NET 2.0 version doesn't mention it, I guess because the function appeared earlier then Vista. – Csaba Toth Nov 18 '13 at 18:29