<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>AB-WebLog.com&#187; sine</title>
	<atom:link href="http://www.ab-weblog.com/en/tag/sine/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ab-weblog.com/en</link>
	<description>Andreas Breitschopp</description>
	<lastBuildDate>Wed, 18 Mar 2015 09:47:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Simple Sine Generator for .NET</title>
		<link>http://www.ab-weblog.com/en/simple-sine-generator-for-net/</link>
		<comments>http://www.ab-weblog.com/en/simple-sine-generator-for-net/#comments</comments>
		<pubDate>Mon, 18 Jul 2011 12:08:54 +0000</pubDate>
		<dc:creator>Andreas Breitschopp</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Audio]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[sine]]></category>
		<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://www.ab-weblog.com/en/?p=133</guid>
		<description><![CDATA[In this post I want to show how to create an easy sine generator in .NET. Let&#8217;s start with a code listing and then I&#8217;ll explain what I&#8217;m doing here: const double frequency = 1000; const double amplitude = 20000; &#8230; <a href="http://www.ab-weblog.com/en/simple-sine-generator-for-net/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In this post I want to show how to create an easy sine generator in .NET.</p>
<p>Let&#8217;s start with a code listing and then I&#8217;ll explain what I&#8217;m doing here:</p>
<pre class="brush: csharp; gutter: true">const double frequency	= 1000;
const double amplitude	= 20000;
const long sampleRate	= 44100;
const int durationSec	= 5;

long sampleCount = sampleRate * durationSec;

double timeStep = 1.0 / (double)sampleRate;

double time = 0;
int[] values = new int[sampleCount];
for (long i = 0; i &lt; sampleCount; i++) {
	values[i] = (int)(amplitude * Math.Sin(2 * Math.PI * frequency * time));
	time = time + timeStep;
}</pre>
<p>OK, here are some explanations:</p>
<ul>
<li>lines 1-4: some constants you can change to e. g. adjust the frequency.<br />
<em>Note:</em> the frequency cannot be more than half the sampling rate.</li>
<li>line 6: calculating the number of sampling points.</li>
<li>line 8: calculating the time between two sampling points.</li>
<li>lines 10-11: some variable initializations.</li>
<li>lines 12-15: here finally the value of each sampling point is calculated.</li>
</ul>
<p>And here is the corresponding code for Visual Basic .NET:</p>
<pre class="brush: vbnet; gutter: true">const frequency as double		= 1000
const amplitude as double		= 20000
const sampleRate As Long		= 44100
const durationSec As Integer	= 5

Dim sampleCount As Long
sampleCount = sampleRate * durationSec

Dim timeStep As Double
timeStep = 1.0 / sampleRate

Dim time As Double = 0
Dim values(0 To sampleCount - 1) As Integer
For i As Long = 0 To sampleCount - 1
	values(i) = amplitude * Math.Sin(2 * Math.PI * frequency * time)
	time = time + timeStep
Next i</pre>
<p>For sound playback you can now use either an API call like <em>PlaySound</em> (of <em>winmm.dll</em>) or the solution of <a title="Article about .NET sound class" href="http://www.codeproject.com/KB/audio-video/CPIAudio.aspx" target="_blank">this great article</a>.</p>
<p>Did you also need to generate a sine in .NET yourself already?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ab-weblog.com/en/simple-sine-generator-for-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->