sorting and PLF example 2 with modern form.
a lot of C# to be real!
This commit is contained in:
50
simulations/SortVisualizer/Algorithms/BogoSort.cs
Normal file
50
simulations/SortVisualizer/Algorithms/BogoSort.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SortVisualizer;
|
||||
|
||||
public class BogoSort : ISortAlgorithm
|
||||
{
|
||||
public string Name => "Bogo Sort";
|
||||
|
||||
private static readonly Random rand = new();
|
||||
public static BogoSort Instance { get; } = new BogoSort();
|
||||
public BogoSort() { }
|
||||
|
||||
public async Task Sort(int[] array, Action refresh, int delay, Action<int> playSound, CancellationToken token)
|
||||
{
|
||||
try
|
||||
{
|
||||
while (!IsSorted(array))
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
// Shuffle array
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
int j = rand.Next(array.Length);
|
||||
(array[i], array[j]) = (array[j], array[i]);
|
||||
|
||||
playSound?.Invoke(array[i]);
|
||||
refresh?.Invoke();
|
||||
|
||||
// small delay for live updates
|
||||
await Task.Delay(delay, token);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (TaskCanceledException)
|
||||
{
|
||||
// Sorting was stopped; exit gracefully
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsSorted(int[] array)
|
||||
{
|
||||
for (int i = 1; i < array.Length; i++)
|
||||
if (array[i - 1] > array[i])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
29
simulations/SortVisualizer/Algorithms/BubbleSort.cs
Normal file
29
simulations/SortVisualizer/Algorithms/BubbleSort.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SortVisualizer;
|
||||
|
||||
public class BubbleSort : ISortAlgorithm
|
||||
{
|
||||
public string Name => "Bubble Sort";
|
||||
|
||||
public async Task Sort(int[] array, Action refresh, int delay, Action<int> playSound, CancellationToken token)
|
||||
{
|
||||
for (int i = 0; i < array.Length - 1; i++)
|
||||
{
|
||||
for (int j = 0; j < array.Length - i - 1; j++)
|
||||
{
|
||||
if (token.IsCancellationRequested) return;
|
||||
|
||||
playSound(array[j]);
|
||||
if (array[j] > array[j + 1])
|
||||
{
|
||||
(array[j], array[j + 1]) = (array[j + 1], array[j]);
|
||||
refresh();
|
||||
await Task.Delay(delay);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
18
simulations/SortVisualizer/Algorithms/ISortAlgorithm.cs
Normal file
18
simulations/SortVisualizer/Algorithms/ISortAlgorithm.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SortVisualizer;
|
||||
|
||||
public interface ISortAlgorithm
|
||||
{
|
||||
string Name { get; }
|
||||
|
||||
Task Sort(
|
||||
int[] array,
|
||||
Action refresh,
|
||||
int delay,
|
||||
Action<int> playSound,
|
||||
CancellationToken token
|
||||
);
|
||||
}
|
||||
33
simulations/SortVisualizer/Algorithms/InsertionSort.cs
Normal file
33
simulations/SortVisualizer/Algorithms/InsertionSort.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SortVisualizer;
|
||||
|
||||
public class InsertionSort : ISortAlgorithm
|
||||
{
|
||||
public string Name => "Insertion Sort";
|
||||
|
||||
public async Task Sort(int[] array, Action refresh, int delay, Action<int> playSound, CancellationToken token)
|
||||
{
|
||||
for (int i = 1; i < array.Length; i++)
|
||||
{
|
||||
if (token.IsCancellationRequested) return;
|
||||
|
||||
int key = array[i];
|
||||
int j = i - 1;
|
||||
|
||||
while (j >= 0 && array[j] > key)
|
||||
{
|
||||
if (token.IsCancellationRequested) return;
|
||||
|
||||
playSound(array[j]);
|
||||
array[j + 1] = array[j];
|
||||
j--;
|
||||
refresh();
|
||||
await Task.Delay(delay);
|
||||
}
|
||||
array[j + 1] = key;
|
||||
}
|
||||
}
|
||||
}
|
||||
83
simulations/SortVisualizer/Algorithms/MergeSort.cs
Normal file
83
simulations/SortVisualizer/Algorithms/MergeSort.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SortVisualizer;
|
||||
|
||||
public class MergeSort : ISortAlgorithm
|
||||
{
|
||||
public string Name => "Merge Sort";
|
||||
public MergeSort() { }
|
||||
public async Task Sort(int[] array, Action refresh, int delay, Action<int> playSound, CancellationToken token)
|
||||
{
|
||||
await MergeSortRecursive(array, 0, array.Length - 1, refresh, delay, playSound, token);
|
||||
}
|
||||
|
||||
private async Task MergeSortRecursive(int[] array, int left, int right, Action refresh, int delay, Action<int> playSound, CancellationToken token)
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
if (left >= right) return;
|
||||
|
||||
int mid = (left + right) / 2;
|
||||
await MergeSortRecursive(array, left, mid, refresh, delay, playSound, token);
|
||||
await MergeSortRecursive(array, mid + 1, right, refresh, delay, playSound, token);
|
||||
await Merge(array, left, mid, right, refresh, delay, playSound, token);
|
||||
}
|
||||
|
||||
private async Task Merge(int[] array, int left, int mid, int right, Action refresh, int delay, Action<int> playSound, CancellationToken token)
|
||||
{
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
int[] L = new int[n1];
|
||||
int[] R = new int[n2];
|
||||
|
||||
Array.Copy(array, left, L, 0, n1);
|
||||
Array.Copy(array, mid + 1, R, 0, n2);
|
||||
|
||||
int i = 0, j = 0, k = left;
|
||||
|
||||
while (i < n1 && j < n2)
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
if (L[i] <= R[j])
|
||||
{
|
||||
array[k] = L[i];
|
||||
playSound?.Invoke(L[i]);
|
||||
i++;
|
||||
}
|
||||
else
|
||||
{
|
||||
array[k] = R[j];
|
||||
playSound?.Invoke(R[j]);
|
||||
j++;
|
||||
}
|
||||
|
||||
refresh?.Invoke();
|
||||
await Task.Delay(delay, token);
|
||||
k++;
|
||||
}
|
||||
|
||||
while (i < n1)
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
array[k] = L[i];
|
||||
playSound?.Invoke(L[i]);
|
||||
i++; k++;
|
||||
refresh?.Invoke();
|
||||
await Task.Delay(delay, token);
|
||||
}
|
||||
|
||||
while (j < n2)
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
array[k] = R[j];
|
||||
playSound?.Invoke(R[j]);
|
||||
j++; k++;
|
||||
refresh?.Invoke();
|
||||
await Task.Delay(delay, token);
|
||||
}
|
||||
}
|
||||
}
|
||||
58
simulations/SortVisualizer/Algorithms/StoogeSort.cs
Normal file
58
simulations/SortVisualizer/Algorithms/StoogeSort.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SortVisualizer;
|
||||
|
||||
public class StoogeSort : ISortAlgorithm
|
||||
{
|
||||
public string Name => "Stooge Sort";
|
||||
public StoogeSort() { }
|
||||
|
||||
public async Task Sort(int[] array, Action refresh, int delay, Action<int> playSound, CancellationToken token)
|
||||
{
|
||||
try
|
||||
{
|
||||
await StoogeSortRecursive(array, 0, array.Length - 1, refresh, delay, playSound, token);
|
||||
}
|
||||
catch (TaskCanceledException)
|
||||
{
|
||||
// Sorting was cancelled <20> handle gracefully
|
||||
// Optionally, reset UI or log here
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Unexpected exception <20> optional logging
|
||||
Console.WriteLine($"StoogeSort error: {ex}");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task StoogeSortRecursive(int[] array, int i, int j, Action refresh, int delay, Action<int> playSound, CancellationToken token)
|
||||
{
|
||||
if (token.IsCancellationRequested) return; // graceful early exit
|
||||
|
||||
// Swap if first element is greater than last
|
||||
if (array[i] > array[j])
|
||||
{
|
||||
int temp = array[i];
|
||||
array[i] = array[j];
|
||||
array[j] = temp;
|
||||
|
||||
playSound?.Invoke(array[i]);
|
||||
playSound?.Invoke(array[j]);
|
||||
refresh?.Invoke();
|
||||
|
||||
try { await Task.Delay(delay, token); }
|
||||
catch (TaskCanceledException) { return; } // exit silently if cancelled
|
||||
}
|
||||
|
||||
if (j - i + 1 > 2)
|
||||
{
|
||||
int t = (j - i + 1) / 3;
|
||||
|
||||
await StoogeSortRecursive(array, i, j - t, refresh, delay, playSound, token);
|
||||
await StoogeSortRecursive(array, i + t, j, refresh, delay, playSound, token);
|
||||
await StoogeSortRecursive(array, i, j - t, refresh, delay, playSound, token);
|
||||
}
|
||||
}
|
||||
}
|
||||
38
simulations/SortVisualizer/Form1.Designer.cs
generated
Normal file
38
simulations/SortVisualizer/Form1.Designer.cs
generated
Normal file
@@ -0,0 +1,38 @@
|
||||
namespace SortVisualizer;
|
||||
|
||||
partial class Form1
|
||||
{
|
||||
/// <summary>
|
||||
/// Required designer variable.
|
||||
/// </summary>
|
||||
private System.ComponentModel.IContainer components = null;
|
||||
|
||||
/// <summary>
|
||||
/// Clean up any resources being used.
|
||||
/// </summary>
|
||||
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && (components != null))
|
||||
{
|
||||
components.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
#region Windows Form Designer generated code
|
||||
|
||||
/// <summary>
|
||||
/// Required method for Designer support - do not modify
|
||||
/// the contents of this method with the code editor.
|
||||
/// </summary>
|
||||
private void InitializeComponent()
|
||||
{
|
||||
this.components = new System.ComponentModel.Container();
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.ClientSize = new System.Drawing.Size(800, 450);
|
||||
this.Text = "Form1";
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
9
simulations/SortVisualizer/Form1.cs
Normal file
9
simulations/SortVisualizer/Form1.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace SortVisualizer;
|
||||
|
||||
public partial class Form1 : Form
|
||||
{
|
||||
public Form1()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
14
simulations/SortVisualizer/Program.cs
Normal file
14
simulations/SortVisualizer/Program.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace SortVisualizer;
|
||||
|
||||
internal static class Program
|
||||
{
|
||||
[STAThread]
|
||||
static void Main()
|
||||
{
|
||||
ApplicationConfiguration.Initialize();
|
||||
Application.Run(new VisualizerForm());
|
||||
}
|
||||
}
|
||||
11
simulations/SortVisualizer/SortVisualizer.csproj
Normal file
11
simulations/SortVisualizer/SortVisualizer.csproj
Normal file
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net9.0-windows</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<UseWindowsForms>true</UseWindowsForms>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
289
simulations/SortVisualizer/VisualizerForm.cs
Normal file
289
simulations/SortVisualizer/VisualizerForm.cs
Normal file
@@ -0,0 +1,289 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Media;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace SortVisualizer;
|
||||
|
||||
public class VisualizerForm : Form
|
||||
{
|
||||
int[] array = Array.Empty<int>();
|
||||
Random rand = new();
|
||||
int delay = 10;
|
||||
bool sorting = false;
|
||||
|
||||
ComboBox algoSelector;
|
||||
Button startButton, stopButton, shuffleButton;
|
||||
TrackBar speedSlider;
|
||||
CheckBox soundToggle;
|
||||
Label speedLabel, statusLabel;
|
||||
|
||||
List<ISortAlgorithm> algorithms;
|
||||
CancellationTokenSource? cts;
|
||||
|
||||
Dictionary<int, byte[]> toneCache = new();
|
||||
SemaphoreSlim soundSemaphore = new(1, 1); // prevent sound overlap
|
||||
|
||||
int highlightedIndex1 = -1;
|
||||
int highlightedIndex2 = -1;
|
||||
|
||||
public VisualizerForm()
|
||||
{
|
||||
Text = "C# Sorting";
|
||||
DoubleBuffered = true;
|
||||
MinimumSize = new Size(800, 500);
|
||||
Width = 1000;
|
||||
Height = 600;
|
||||
|
||||
algorithms = new() { new BubbleSort(), new InsertionSort(), new BogoSort(),
|
||||
new MergeSort() , new StoogeSort()
|
||||
};
|
||||
|
||||
// --- Top Control Panel ---
|
||||
var topPanel = new FlowLayoutPanel
|
||||
{
|
||||
Dock = DockStyle.Top,
|
||||
AutoSize = true,
|
||||
WrapContents = true,
|
||||
FlowDirection = FlowDirection.LeftToRight,
|
||||
Padding = new Padding(5),
|
||||
AutoScroll = true
|
||||
};
|
||||
|
||||
algoSelector = new ComboBox { Width = 160, DropDownStyle = ComboBoxStyle.DropDownList };
|
||||
algoSelector.DataSource = algorithms;
|
||||
algoSelector.DisplayMember = "Name";
|
||||
|
||||
int buttonHeight = algoSelector.Height;
|
||||
|
||||
startButton = new Button { Width = 100, Height = buttonHeight, Text = "Start" };
|
||||
stopButton = new Button { Width = 100, Height = buttonHeight, Text = "Stop" };
|
||||
shuffleButton = new Button { Width = 100, Height = buttonHeight, Text = "Shuffle" };
|
||||
|
||||
soundToggle = new CheckBox { Width = 100, Height = buttonHeight, Text = "Sound On", Checked = true };
|
||||
|
||||
speedLabel = new Label { Width = 60, Height = buttonHeight, Text = "Speed: 10", TextAlign = ContentAlignment.MiddleCenter };
|
||||
speedSlider = new TrackBar
|
||||
{
|
||||
Width = 250,
|
||||
Minimum = 1,
|
||||
Maximum = 100,
|
||||
Value = 10,
|
||||
TickFrequency = 10,
|
||||
SmallChange = 1,
|
||||
LargeChange = 10,
|
||||
Height = buttonHeight
|
||||
};
|
||||
|
||||
statusLabel = new Label { Width = 150, Height = buttonHeight, Text = "Idle", TextAlign = ContentAlignment.MiddleRight };
|
||||
|
||||
topPanel.Controls.AddRange(new Control[]
|
||||
{
|
||||
algoSelector, startButton, stopButton, shuffleButton,
|
||||
soundToggle, speedLabel, speedSlider, statusLabel
|
||||
});
|
||||
|
||||
Controls.Add(topPanel);
|
||||
|
||||
speedSlider.ValueChanged += (s, e) =>
|
||||
{
|
||||
delay = 101 - speedSlider.Value;
|
||||
speedLabel.Text = $"Speed: {speedSlider.Value}";
|
||||
};
|
||||
|
||||
startButton.Click += async (s, e) => await StartSort();
|
||||
stopButton.Click += (s, e) => StopSort();
|
||||
shuffleButton.Click += (s, e) => Shuffle();
|
||||
|
||||
this.Resize += (s, e) => Invalidate();
|
||||
|
||||
Shuffle();
|
||||
}
|
||||
|
||||
void Shuffle()
|
||||
{
|
||||
if (sorting) return;
|
||||
array = Enumerable.Range(1, 100).OrderBy(_ => rand.Next()).ToArray();
|
||||
highlightedIndex1 = highlightedIndex2 = -1;
|
||||
Invalidate();
|
||||
statusLabel.Text = "Shuffled!";
|
||||
}
|
||||
|
||||
async Task StartSort()
|
||||
{
|
||||
if (sorting) return;
|
||||
sorting = true;
|
||||
cts = new CancellationTokenSource();
|
||||
|
||||
var algo = (ISortAlgorithm)algoSelector.SelectedItem!;
|
||||
statusLabel.Text = $"Sorting ( {algo.Name} )...";
|
||||
|
||||
try
|
||||
{
|
||||
await algo.Sort(
|
||||
array,
|
||||
() => Invalidate(), // keep original signature
|
||||
delay,
|
||||
value => { if (soundToggle.Checked) _ = PlaySoundAsync(value); },
|
||||
cts.Token
|
||||
);
|
||||
|
||||
highlightedIndex1 = highlightedIndex2 = -1;
|
||||
Invalidate();
|
||||
|
||||
if (!cts.Token.IsCancellationRequested)
|
||||
statusLabel.Text = "Done!";
|
||||
}
|
||||
finally
|
||||
{
|
||||
sorting = false;
|
||||
}
|
||||
}
|
||||
|
||||
void StopSort()
|
||||
{
|
||||
if (!sorting) return;
|
||||
cts?.Cancel();
|
||||
sorting = false;
|
||||
highlightedIndex1 = highlightedIndex2 = -1;
|
||||
Invalidate();
|
||||
statusLabel.Text = "Stopped.";
|
||||
}
|
||||
|
||||
protected override void OnPaint(PaintEventArgs e)
|
||||
{
|
||||
base.OnPaint(e);
|
||||
var g = e.Graphics;
|
||||
|
||||
int offsetY = 50;
|
||||
int marginX = 20;
|
||||
|
||||
if (array.Length == 0) return;
|
||||
|
||||
int barWidth = (ClientSize.Width - 2 * marginX) / array.Length;
|
||||
int availableHeight = ClientSize.Height - offsetY;
|
||||
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
int height = (int)(array[i] * (availableHeight / (float)array.Length));
|
||||
Brush brush = Brushes.LightGreen;
|
||||
|
||||
if (sorting)
|
||||
{
|
||||
if (i == highlightedIndex1) brush = Brushes.Red;
|
||||
else if (i == highlightedIndex2) brush = Brushes.Orange;
|
||||
else brush = Brushes.DeepSkyBlue;
|
||||
}
|
||||
|
||||
g.FillRectangle(brush, marginX + i * barWidth, ClientSize.Height - height, barWidth - 1, height);
|
||||
}
|
||||
}
|
||||
|
||||
async Task PlaySoundAsync(int value)
|
||||
{
|
||||
// Use semaphore to prevent overlapping sounds
|
||||
if (!await soundSemaphore.WaitAsync(0))
|
||||
return; // skip if already playing
|
||||
|
||||
try
|
||||
{
|
||||
int freq = 220 + (int)(value * 12.0);
|
||||
int durationMs = 10;
|
||||
|
||||
if (!toneCache.TryGetValue(freq, out var wav))
|
||||
{
|
||||
wav = GenerateToneWav(freq, durationMs, 44100);
|
||||
|
||||
// Limit cache size
|
||||
if (toneCache.Count > 200)
|
||||
toneCache.Clear();
|
||||
|
||||
toneCache[freq] = wav;
|
||||
}
|
||||
|
||||
await Task.Run(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
using var ms = new MemoryStream(wav);
|
||||
using var player = new SoundPlayer(ms);
|
||||
player.PlaySync(); // blocks on background thread
|
||||
}
|
||||
catch
|
||||
{
|
||||
try
|
||||
{
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
Console.Beep(freq, durationMs);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
});
|
||||
}
|
||||
finally
|
||||
{
|
||||
soundSemaphore.Release();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
cts?.Dispose();
|
||||
soundSemaphore?.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
#region Tone Generation
|
||||
|
||||
public static byte[] GenerateToneWav(int frequency, int durationMs, int sampleRate = 44100, double amplitude = 0.25)
|
||||
{
|
||||
int samples = (int)((durationMs / 1000.0) * sampleRate);
|
||||
using var ms = new MemoryStream();
|
||||
using var bw = new BinaryWriter(ms);
|
||||
|
||||
short numChannels = 1;
|
||||
short bitsPerSample = 16;
|
||||
int byteRate = sampleRate * numChannels * bitsPerSample / 8;
|
||||
short blockAlign = (short)(numChannels * bitsPerSample / 8);
|
||||
int dataSize = samples * numChannels * (bitsPerSample / 8);
|
||||
|
||||
bw.Write(System.Text.Encoding.ASCII.GetBytes("RIFF"));
|
||||
bw.Write(36 + dataSize);
|
||||
bw.Write(System.Text.Encoding.ASCII.GetBytes("WAVE"));
|
||||
|
||||
bw.Write(System.Text.Encoding.ASCII.GetBytes("fmt "));
|
||||
bw.Write(16);
|
||||
bw.Write((short)1);
|
||||
bw.Write(numChannels);
|
||||
bw.Write(sampleRate);
|
||||
bw.Write(byteRate);
|
||||
bw.Write(blockAlign);
|
||||
bw.Write(bitsPerSample);
|
||||
|
||||
bw.Write(System.Text.Encoding.ASCII.GetBytes("data"));
|
||||
bw.Write(dataSize);
|
||||
|
||||
double twoPiF = 2 * Math.PI * frequency;
|
||||
for (int n = 0; n < samples; n++)
|
||||
{
|
||||
double t = n / (double)sampleRate;
|
||||
double sample = amplitude * Math.Sin(twoPiF * t);
|
||||
short s = (short)Math.Max(short.MinValue, Math.Min(short.MaxValue, sample * short.MaxValue));
|
||||
bw.Write(s);
|
||||
}
|
||||
|
||||
bw.Flush();
|
||||
return ms.ToArray();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user