Click here to Skip to main content
15,887,318 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#

So I enjoy working on college assignments (to keep some of my skills sharp) and I've decided to tackle this one:

COS 126 Programming Assignment: Digital Signal Processing Assignment[^]

I'm running MSVS2015 C#/Console app along with the SharpDX package that gives me access to some underlying DirectSound capabilities. I'm just trying to create and play a 2 second note 'A' as they go over in the first example. When I run the following code it plays the 2 seconds but it is very static-y. I'm assuming there is something off with my calculations but I can't figure out what exactly. Does anyone have experience writing their own digital sound buffers?

Thanks,
- Jeff

What I have tried:

C#
public class Execution : IDisposable
{
	IntPtr Handle;
	DirectSound Device;
	SecondarySoundBuffer Buffer;

	public Execution()
	{
		Handle = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;

		Device = new DirectSound();
		Device.SetCooperativeLevel(Handle, CooperativeLevel.Priority);

		var rate = 44100;
		var bits = 16;
		var channels = 1;
		var waveFormat = new WaveFormat(rate, bits, channels);

		// Create a buffer with 2 seconds of sample data
		var seconds = 2;

		var bufferDescription = new SoundBufferDescription() { Format = waveFormat, BufferBytes = waveFormat.AverageBytesPerSecond * seconds };
		Buffer = new SecondarySoundBuffer(Device, bufferDescription);

		var noteFrequency = 440f;       // A
		var bufferData = new float[bufferDescription.BufferBytes];

		var count = 0;
		for (var sample = 0; sample < bufferDescription.BufferBytes; sample++)
		{
			var sampleInSeconds = (float)sample / (float)bufferDescription.BufferBytes * (float)seconds;
			var value = (float)Math.Sin(2f * Math.PI * noteFrequency * sampleInSeconds );
			bufferData[sample] = value;
		}

		Buffer.Write(bufferData, 0, LockFlags.EntireBuffer);
	}

	public void Execute()
	{
		Buffer.Play(0, 0);
	}

	public void Dispose()
	{
		Buffer.Dispose();
		Device.Dispose();
	}
}
Posted
Updated 2-Dec-16 5:35am

1 solution

Well, I would think that you are writing the data in an incorrect format. A typical 16 bit wave file uses integers and not floating points.

Also, obviously a float is 32 bit so it cannot be the proper data for a 16 bit wave file.

Given that both the wide and the type of data are different than the expected one, how would you expect to get good result?

A good starting point would be to start by analysing a wave file that contain such wave form and compare the data with your data. It should not be hard to find either application or code to generated a wave.

Also, by using a decent sound editor, it should be obvious it the data is correct if you zoom enough on data to the point you see the wave itself.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900