Click here to Skip to main content
15,886,519 members
Articles / Programming Languages / C#

nBASS: A sound libary for .NET

Rate me:
Please Sign up or sign in to vote.
4.74/5 (30 votes)
10 Nov 2002BSD5 min read 478.9K   7.1K   134  
nBASS provides .NET users with sound functionality
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using nBASS;

namespace RecordTest
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.ComponentModel.IContainer components;

		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.ComboBox comboBox1;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox textBox2;
		private System.Threading.Thread thread;

		private WMAEncoder enc;
		private BASS bass;
		private WMAStream wmastream;
		private System.Windows.Forms.ProgressBar progressBar1;
		private System.Windows.Forms.ProgressBar progressBar2;
		private System.Windows.Forms.Timer timer1;
		private Record record;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			bass = new BASS(-1, 44100, DeviceSetupFlags.Default, this.Handle);
			bass.Start();

			int[] rates = WMAEncoder.GetRates(44100, RateFlags.Default); 
			foreach (int rate in rates)
			{
				this.comboBox1.Items.Add(rate);
			}

		}

		private int Progress(IntPtr buffer, int length, int user)
		{
			try 
			{
				enc.Write(buffer, length);
			}
			catch (WMAException)
			{
				timer1.Stop();
				enc.Close();
				return 0;
			}
			return 1;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (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.textBox1 = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.comboBox1 = new System.Windows.Forms.ComboBox();
			this.button3 = new System.Windows.Forms.Button();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.progressBar1 = new System.Windows.Forms.ProgressBar();
			this.progressBar2 = new System.Windows.Forms.ProgressBar();
			this.timer1 = new System.Windows.Forms.Timer(this.components);
			this.SuspendLayout();
			// 
			// textBox1
			// 
			this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.textBox1.Location = new System.Drawing.Point(96, 8);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(312, 20);
			this.textBox1.TabIndex = 0;
			this.textBox1.Text = "";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 6);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(88, 23);
			this.label2.TabIndex = 3;
			this.label2.Text = "Destination URL";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// button1
			// 
			this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.button1.Location = new System.Drawing.Point(96, 64);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(96, 23);
			this.button1.TabIndex = 4;
			this.button1.Text = "Source On";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button2
			// 
			this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.button2.Location = new System.Drawing.Point(200, 64);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(96, 23);
			this.button2.TabIndex = 5;
			this.button2.Text = "Connect";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// comboBox1
			// 
			this.comboBox1.Location = new System.Drawing.Point(96, 34);
			this.comboBox1.Name = "comboBox1";
			this.comboBox1.Size = new System.Drawing.Size(88, 21);
			this.comboBox1.TabIndex = 6;
			// 
			// button3
			// 
			this.button3.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.button3.Location = new System.Drawing.Point(304, 64);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(104, 23);
			this.button3.TabIndex = 8;
			this.button3.Text = "Stop";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// textBox3
			// 
			this.textBox3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.textBox3.Location = new System.Drawing.Point(368, 34);
			this.textBox3.Name = "textBox3";
			this.textBox3.Size = new System.Drawing.Size(40, 20);
			this.textBox3.TabIndex = 10;
			this.textBox3.Text = "42042";
			this.textBox3.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(312, 32);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(56, 23);
			this.label3.TabIndex = 11;
			this.label3.Text = "Send Port";
			this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(192, 32);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(72, 23);
			this.label1.TabIndex = 13;
			this.label1.Text = "Connect Port";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// textBox2
			// 
			this.textBox2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.textBox2.Location = new System.Drawing.Point(264, 34);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(40, 20);
			this.textBox2.TabIndex = 12;
			this.textBox2.Text = "42041";
			this.textBox2.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// progressBar1
			// 
			this.progressBar1.Location = new System.Drawing.Point(3, 34);
			this.progressBar1.Maximum = 128;
			this.progressBar1.Name = "progressBar1";
			this.progressBar1.Size = new System.Drawing.Size(88, 23);
			this.progressBar1.Step = 1;
			this.progressBar1.TabIndex = 14;
			// 
			// progressBar2
			// 
			this.progressBar2.Location = new System.Drawing.Point(3, 64);
			this.progressBar2.Maximum = 128;
			this.progressBar2.Name = "progressBar2";
			this.progressBar2.Size = new System.Drawing.Size(88, 23);
			this.progressBar2.Step = 1;
			this.progressBar2.TabIndex = 15;
			// 
			// timer1
			// 
			this.timer1.Interval = 50;
			this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(416, 93);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																							 this.progressBar2,
																							 this.progressBar1,
																							 this.label1,
																							 this.textBox2,
																							 this.label3,
																							 this.textBox3,
																							 this.button3,
																							 this.comboBox1,
																							 this.button2,
																							 this.button1,
																							 this.label2,
																							 this.textBox1});
			this.Name = "Form1";
			this.Text = "RecordTest";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			try 
			{
				enc = WMAEncoder.OpenEncoderNetwork(44100, 
						  EncoderFlags.Tags, 
						  (int) this.comboBox1.SelectedItem, 
						  Convert.ToInt32(this.textBox3.Text),
						  2);

				enc.SetTag(WMATag.Author, "leppie");
				enc.SetTagDone();

				record = new Record(-1);
				record.SetInput(4, InputFlags.ON); //mic is 4, on mine anyways :)
				record.Start(44100, RecordFlags.Default, new GetRecordCallBack(Progress), 0);
				timer1.Start();
			}
			catch (WMAException ex)
			{
				Console.WriteLine(ex.ErrorDescription);
			}
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			wmastream = bass.LoadWMAStream(false,
				"mms://" + this.textBox1.Text + ":" + Convert.ToInt32(this.textBox2.Text), 0, 0, WMAStreamFlags.Default);
			wmastream.Play(false, false);
		}

		private void button3_Click(object sender, System.EventArgs e)
		{
			enc.Close();
			wmastream.Stop();
		}

		private void timer1_Tick(object sender, System.EventArgs e)
		{
			if (record.ActivityState != State.Stopped)
			{
				this.progressBar1.Value = record.LeftLevel; //not allowed in callback
				this.progressBar2.Value = record.RightLevel; //not allowed in callback
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License


Written By
Software Developer
South Africa South Africa
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions