2009-01-29 00:18:34 +08:00
|
|
|
using System;
|
|
|
|
using System.Threading;
|
|
|
|
using SFML;
|
|
|
|
using SFML.Audio;
|
|
|
|
|
|
|
|
namespace sample_soundcapture
|
|
|
|
{
|
|
|
|
static class Program
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// The main entry point for the application.
|
|
|
|
/// </summary>
|
|
|
|
static void Main(string[] args)
|
|
|
|
{
|
|
|
|
// Check that the device can capture audio
|
2009-11-03 17:04:40 +08:00
|
|
|
if (SoundRecorder.IsAvailable == false)
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
Console.WriteLine("Sorry, audio capture is not supported by your system");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Choose the sample rate
|
|
|
|
Console.WriteLine("Please choose the sample rate for sound capture (44100 is CD quality) : ");
|
2009-07-17 16:11:03 +08:00
|
|
|
uint sampleRate = uint.Parse(Console.ReadLine());
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Wait for user input...
|
|
|
|
Console.WriteLine("Press enter to start recording audio");
|
|
|
|
Console.ReadLine();
|
|
|
|
|
|
|
|
// Here we'll use an integrated custom recorder, which saves the captured data into a SoundBuffer
|
2009-07-17 16:11:03 +08:00
|
|
|
SoundBufferRecorder recorder = new SoundBufferRecorder();
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Audio capture is done in a separate thread, so we can block the main thread while it is capturing
|
2009-07-17 16:11:03 +08:00
|
|
|
recorder.Start(sampleRate);
|
2009-01-29 00:18:34 +08:00
|
|
|
Console.WriteLine("Recording... press enter to stop");
|
|
|
|
Console.ReadLine();
|
2009-07-17 16:11:03 +08:00
|
|
|
recorder.Stop();
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Get the buffer containing the captured data
|
2009-07-17 16:11:03 +08:00
|
|
|
SoundBuffer buffer = recorder.SoundBuffer;
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Display captured sound informations
|
|
|
|
Console.WriteLine("Sound information :");
|
2009-07-17 16:11:03 +08:00
|
|
|
Console.WriteLine(" " + buffer.Duration + " seconds");
|
|
|
|
Console.WriteLine(" " + buffer.SampleRate + " samples / seconds");
|
|
|
|
Console.WriteLine(" " + buffer.ChannelsCount + " channels");
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Choose what to do with the recorded sound data
|
|
|
|
Console.WriteLine("What do you want to do with captured sound (p = play, s = save) ? ");
|
2009-07-17 16:11:03 +08:00
|
|
|
char choice = char.Parse(Console.ReadLine());
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2009-07-17 16:11:03 +08:00
|
|
|
if (choice == 's')
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
// Choose the filename
|
|
|
|
Console.WriteLine("Choose the file to create : ");
|
2009-07-17 16:11:03 +08:00
|
|
|
string filename = Console.ReadLine();
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Save the buffer
|
2009-07-17 16:11:03 +08:00
|
|
|
buffer.SaveToFile(filename);
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Create a sound instance and play it
|
2009-07-17 16:11:03 +08:00
|
|
|
Sound sound = new Sound(buffer);
|
|
|
|
sound.Play();
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Wait until finished
|
2009-07-17 16:11:03 +08:00
|
|
|
while (sound.Status == SoundStatus.Playing)
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
// Display the playing position
|
|
|
|
Console.CursorLeft = 0;
|
2009-07-17 16:11:03 +08:00
|
|
|
Console.Write("Playing... " + sound.PlayingOffset + " sec ");
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Leave some CPU time for other threads
|
|
|
|
Thread.Sleep(100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finished !
|
|
|
|
Console.WriteLine("\nDone !");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|