Skip to content

Instantly share code, notes, and snippets.

@Noofbiz
Created June 16, 2016 13:12
Show Gist options
  • Save Noofbiz/58410ebadd7971ebc57f62d63ff8f0d2 to your computer and use it in GitHub Desktop.
Save Noofbiz/58410ebadd7971ebc57f62d63ff8f0d2 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
namespace MakingWaves
{
internal class Program
{
private static void Main(string[] args)
{
WaveForm a = new WaveForm(8000, 300, "ABCDEFG_GFEDCBA", @"c:\waveyWave.wav");
}
}
/// <summary>
/// creates a .wav file using the sampleRate, duration of note, a string of notes, at the file path given.
/// </summary>
public class WaveForm
{
public WaveForm(double dblSampleRate, double dblDuration, string strNotes, string strFilePAth)
{
char[] chrArrNotes = strNotes.ToCharArray();
int bytesPerNote = (int)Math.Floor(dblSampleRate * dblDuration / 1000.0);
List<byte> bytsWaveForm = new List<byte>();
foreach (char note in chrArrNotes)
{
byte byteCurrentAmp = 128;
for (int i = 0; i < bytesPerNote; i++)
{
byteCurrentAmp = (GetFrequencyFromNote(note) == 0.01) ? (byte)128 : (byte)Math.Round(Math.Sin(2 * Math.PI * i * GetFrequencyFromNote(note) / dblSampleRate));
bytsWaveForm.Add(byteCurrentAmp);
}
}
int time = (int)Math.Ceiling(dblDuration * strNotes.Length / 1000.0);
GetWAVFileFromByteString(bytsWaveForm, dblSampleRate, time, strFilePAth);
}
private double GetFrequencyFromNote(char chNote)
{
double freqReturn = 0.0;
switch (chNote)
{
case 'A':
freqReturn = 220.00;
break;
case 'B':
freqReturn = 246.94;
break;
case 'C':
freqReturn = 261.63;
break;
case 'D':
freqReturn = 293.66;
break;
case 'E':
freqReturn = 329.63;
break;
case 'F':
freqReturn = 349.23;
break;
case 'G':
freqReturn = 392.00;
break;
default:
freqReturn = 0.01;
break;
}
return freqReturn;
}
private void GetWAVFileFromByteString(List<byte> bytsWaveForm, double dblSampleRate, int intSeconds, string strFilePAth)
{
int intBitSize = 16;
short intWaveType = 1;
short intChannels = 1;
int fileSize = (bytsWaveForm.Count) + 44;
short intBitPerSample = 8;
int intSampleRate = (int)Math.Floor(dblSampleRate);
int intSampleSize = intSampleRate * intBitPerSample * intChannels / 8;
short intBitChannels = (short)(intBitPerSample * intChannels / 8);
using (System.IO.BinaryWriter w = new System.IO.BinaryWriter(System.IO.File.Open(strFilePAth, System.IO.FileMode.Create)))
{
byte[] arr = System.Text.Encoding.ASCII.GetBytes("RIFF");
w.Write(arr);
w.Write(fileSize - 8);
arr = System.Text.Encoding.ASCII.GetBytes("WAVE");
w.Write(arr);
arr = System.Text.Encoding.ASCII.GetBytes("fmt ");
w.Write(arr);
w.Write(intBitSize);
w.Write(intWaveType);
w.Write(intChannels);
w.Write(intSampleRate);
w.Write(intSampleSize);
w.Write(intBitChannels);
w.Write(intBitPerSample);
arr = System.Text.Encoding.ASCII.GetBytes("data");
w.Write(arr);
w.Write(fileSize - 44);
for (int i = 0; i < bytsWaveForm.Count - 1; i++)
{
w.Write(bytsWaveForm[i]);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment