namespace WindowsFormsApplication1
{
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.numFreq = new System.Windows.Forms.NumericUpDown();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.numVol = new System.Windows.Forms.NumericUpDown();
((System.ComponentModel.ISupportInitialize)(this.numFreq)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.numVol)).BeginInit();
this.SuspendLayout();
//
// numFreq
//
this.numFreq.Location = new System.Drawing.Point(152, 12);
this.numFreq.Maximum = new decimal(new int[] {
24000,
0,
0,
0});
this.numFreq.Name = "numFreq";
this.numFreq.Size = new System.Drawing.Size(120, 20);
this.numFreq.TabIndex = 0;
this.numFreq.ValueChanged += new System.EventHandler(this.numFreq_ValueChanged);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 14);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(49, 13);
this.label1.TabIndex = 1;
this.label1.Text = "Частота";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 48);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(62, 13);
this.label2.TabIndex = 2;
this.label2.Text = "Громкость";
//
// numVol
//
this.numVol.DecimalPlaces = 2;
this.numVol.Increment = new decimal(new int[] {
1,
0,
0,
131072});
this.numVol.Location = new System.Drawing.Point(152, 41);
this.numVol.Maximum = new decimal(new int[] {
1,
0,
0,
0});
this.numVol.Name = "numVol";
this.numVol.Size = new System.Drawing.Size(120, 20);
this.numVol.TabIndex = 3;
this.numVol.ValueChanged += new System.EventHandler(this.numVol_ValueChanged);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(870, 413);
this.Controls.Add(this.numVol);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.numFreq);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.numFreq)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.numVol)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.NumericUpDown numFreq;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.NumericUpDown numVol;
}
}
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Media;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
WavBuffer PlayBuffer, WriteBuffer;
int Frequency;
float Volume;
Semaphore WriterSem, PlayerSem;
private void Form1_Load(object sender, EventArgs e)
{
numFreq.Value = 500;
numVol.Value = 0.75M;
PlayBuffer = WriteBuffer = new WavBuffer();
PlayBuffer.Next = new WavBuffer() { Next = new WavBuffer() { Next = new WavBuffer() { Next = PlayBuffer } } };
WriterSem = new Semaphore(2, 4);
PlayerSem = new Semaphore(0, 4);
new Thread(new ThreadStart(WriteThread)) { IsBackground = true }.Start();
new Thread(new ThreadStart(PlayThread)) { IsBackground = true }.Start();
}
private void WriteThread()
{
double x = 0;
while (true)
{
WriterSem.WaitOne();
int Max = WriteBuffer.File.Length;
byte[] B = WriteBuffer.File;
double Amp = Volume * 32700;
double delta = Frequency * 2 * Math.PI / (44100);
for (int i = WriteBuffer.StartOffset; i < Max; i += 2)
{
double d = Amp * Math.Sin(x);
x += delta;
short s = (short)d;
B[i] = (byte)s;
B[i + 1] = (byte)(s >> 8);
}
WriteBuffer = WriteBuffer.Next;
PlayerSem.Release();
}
}
private void PlayThread()
{
SoundPlayer Player = new SoundPlayer();
while (true)
{
PlayerSem.WaitOne();
PlayBuffer.MemStream.Seek(0, SeekOrigin.Begin);
Player.Stream = PlayBuffer.MemStream;
Player.PlaySync();
WriterSem.Release();
}
}
private void numFreq_ValueChanged(object sender, EventArgs e)
{
Frequency = (int)numFreq.Value;
}
private void numVol_ValueChanged(object sender, EventArgs e)
{
Volume = (float)numVol.Value;
}
class WavBuffer
{
internal WavBuffer()
{
short channr = 1;
int Hz = 44100;
short bitpersample = 16;
short blockalign = (short)(channr * bitpersample / 8);
int bytesps = Hz * blockalign;
int DataLen = bytesps * 2;
int frmLen = 16;
int FileLen = 4 + (8 + frmLen) + (8 + DataLen);
BinaryWriter BW = new BinaryWriter(new MemoryStream(), System.Text.Encoding.ASCII);
BW.Write("RIFF".ToCharArray());//File Header
BW.Write(FileLen);
BW.Write("WAVE".ToCharArray());
BW.Write("fmt ".ToCharArray());//Format chunk
BW.Write(frmLen);
BW.Write((short)1); //PCM
BW.Write(channr); //mono
BW.Write(Hz); //Hz
BW.Write(88200); //byte / s
BW.Write(blockalign); //Block alighn
BW.Write(bitpersample);//bit/sample
BW.Write("data".ToCharArray());//Data chunk
BW.Write(DataLen);
BW.Write(new char[DataLen]);
BW.Flush();
File = (BW.BaseStream as MemoryStream).GetBuffer();
StartOffset = File.Length - DataLen;
MemStream = new MemoryStream(File);
}
public byte[] File;
public int StartOffset;
public WavBuffer Next;
public MemoryStream MemStream;
}
}
}
Если Вы уже зарегистрированы на Портале - войдите в систему, если Вы еще не регистрировались - пройдите простую процедуру регистрации.