Skip to content

Commit

Permalink
Merge pull request #28 from Mijo-Software/mjohne-patch-1
Browse files Browse the repository at this point in the history
Add files via upload
  • Loading branch information
mjohne authored Dec 3, 2019
2 parents 8308202 + da34969 commit db042fd
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 66 deletions.
8 changes: 5 additions & 3 deletions Beep/Beep.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
<IsWebBootstrapper>false</IsWebBootstrapper>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
Expand All @@ -29,11 +32,8 @@
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -48,6 +48,7 @@
<DocumentationFile>bin\Debug\Beep.xml</DocumentationFile>
<RunCodeAnalysis>true</RunCodeAnalysis>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
<LangVersion>8.0</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -61,6 +62,7 @@
<DocumentationFile>bin\Release\Beep.xml</DocumentationFile>
<RunCodeAnalysis>true</RunCodeAnalysis>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
<LangVersion>8.0</LangVersion>
</PropertyGroup>
<PropertyGroup>
<StartupObject>Beep.Program</StartupObject>
Expand Down
28 changes: 19 additions & 9 deletions Beep/BeepForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

94 changes: 46 additions & 48 deletions Beep/BeepForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ namespace Beep
/// </summary>
public partial class BeepForm : Form
{
/// <summary>
/// Culture info
/// </summary>
private readonly CultureInfo culture = new CultureInfo(name: "en");

/// <summary>
Expand All @@ -26,57 +29,52 @@ public partial class BeepForm : Form
/// <param name="volume">volume</param>
public static void PlayBeep(ushort frequency = 2400, int msDuration = 100, ushort volume = 16383)
{
using (MemoryStream memoryStream = new MemoryStream())
using MemoryStream memoryStream = new MemoryStream();
using (BinaryWriter writer = new BinaryWriter(output: memoryStream))
{
using (BinaryWriter writer = new BinaryWriter(output: memoryStream))
const double TAU = 2 * Math.PI;
const int formatChunkSize = 16;
const int headerSize = 8;
const short formatType = 1;
const short tracks = 1;
const int samplesPerSecond = 44100;
const short bitsPerSample = 16;
const short frameSize = tracks * ((bitsPerSample + 7) / 8);
const int bytesPerSecond = samplesPerSecond * frameSize;
const int waveSize = 4;
int samples = (int)((decimal)samplesPerSecond * msDuration / 1000);
int dataChunkSize = samples * frameSize;
int fileSize = waveSize + headerSize + formatChunkSize + headerSize + dataChunkSize;
// var encoding = new System.Text.UTF8Encoding();
writer.Write(value: 0x46464952); // = encoding.GetBytes("RIFF")
writer.Write(value: fileSize);
writer.Write(value: 0x45564157); // = encoding.GetBytes("WAVE")
writer.Write(value: 0x20746D66); // = encoding.GetBytes("fmt ")
writer.Write(value: formatChunkSize);
writer.Write(value: formatType);
writer.Write(value: tracks);
writer.Write(value: samplesPerSecond);
writer.Write(value: bytesPerSecond);
writer.Write(value: frameSize);
writer.Write(value: bitsPerSample);
writer.Write(value: 0x61746164); // = encoding.GetBytes("data")
writer.Write(value: dataChunkSize);
{
const double TAU = 2 * Math.PI;
int formatChunkSize = 16;
int headerSize = 8;
short formatType = 1;
short tracks = 1;
int samplesPerSecond = 44100;
short bitsPerSample = 16;
short frameSize = (short)(tracks * ((bitsPerSample + 7) / 8));
int bytesPerSecond = samplesPerSecond * frameSize;
int waveSize = 4;
int samples = (int)((decimal)samplesPerSecond * msDuration / 1000);
int dataChunkSize = samples * frameSize;
int fileSize = waveSize + headerSize + formatChunkSize + headerSize + dataChunkSize;
// var encoding = new System.Text.UTF8Encoding();
writer.Write(value: 0x46464952); // = encoding.GetBytes("RIFF")
writer.Write(value: fileSize);
writer.Write(value: 0x45564157); // = encoding.GetBytes("WAVE")
writer.Write(value: 0x20746D66); // = encoding.GetBytes("fmt ")
writer.Write(value: formatChunkSize);
writer.Write(value: formatType);
writer.Write(value: tracks);
writer.Write(value: samplesPerSecond);
writer.Write(value: bytesPerSecond);
writer.Write(value: frameSize);
writer.Write(value: bitsPerSample);
writer.Write(value: 0x61746164); // = encoding.GetBytes("data")
writer.Write(value: dataChunkSize);
{
double theta = frequency * TAU / samplesPerSecond;
// 'volume' is UInt16 with range 0 thru Uint16.MaxValue ( = 65 535)
// we need 'amp' to have the range of 0 thru Int16.MaxValue ( = 32 767)
double amp = volume >> 2; // so we simply set amp = volume / 2
for (int step = 0; step < samples; step++)
{
short s = (short)(amp * Math.Sin(a: theta * step));
writer.Write(value: s);
}
}
memoryStream.Seek(offset: 0, loc: SeekOrigin.Begin);
using (SoundPlayer soundPlayer = new SoundPlayer(stream: memoryStream))
double theta = frequency * TAU / samplesPerSecond;
// 'volume' is UInt16 with range 0 thru Uint16.MaxValue ( = 65 535)
// we need 'amp' to have the range of 0 thru Int16.MaxValue ( = 32 767)
double amp = volume >> 2; // so we simply set amp = volume / 2
for (int step = 0; step < samples; step++)
{
soundPlayer.Play();
short s = (short)(amp * Math.Sin(a: theta * step));
writer.Write(value: s);
}
writer.Close();
}
memoryStream.Close();
memoryStream.Seek(offset: 0, loc: SeekOrigin.Begin);
using SoundPlayer soundPlayer = new SoundPlayer(stream: memoryStream);
soundPlayer.Play();
}
memoryStream.Close();
} // public static void PlayBeep(UInt16 frequency, int msDuration, UInt16 volume = 16383)

/// <summary>
Expand All @@ -85,22 +83,22 @@ public static void PlayBeep(ushort frequency = 2400, int msDuration = 100, ushor
/// <param name="sender">object sender</param>
/// <param name="e">event arguments</param>
/// <remarks>The parameters <paramref name="e"/> and <paramref name="sender"/> are not needed, but must be indicated.</remarks>
private void ChangeBeepText_Scroll(object sender, EventArgs e) => buttonBeep.Text = "BEEP = " + ((ushort)trackBarFrequency.Value).ToString(provider: culture) + " hz / " + trackBarDuration.Value.ToString(provider: culture) + " ms / " + ((ushort)trackBarVolume.Value).ToString(provider: culture);
private void ChangeBeepText_Scroll(object sender, EventArgs e) => buttonBeep.Text = "&BEEP = " + ((ushort)trackBarFrequency.Value).ToString(provider: culture) + " hz / " + trackBarDuration.Value.ToString(provider: culture) + " ms / " + ((ushort)trackBarVolume.Value).ToString(provider: culture) + " %";

/// <summary>
/// Indicate to play a beep sound
/// </summary>
/// <param name="sender">object sender</param>
/// <param name="e">event arguments</param>
/// <remarks>The parameters <paramref name="e"/> and <paramref name="sender"/> are not needed, but must be indicated.</remarks>
private void ButtonBeep_Click(object sender, EventArgs e) => PlayBeep(frequency: (ushort)trackBarFrequency.Value, msDuration: trackBarDuration.Value, volume: (ushort)trackBarVolume.Value);
private void ButtonBeep_Click(object sender, EventArgs e) => PlayBeep(frequency: (ushort)trackBarFrequency.Value, msDuration: trackBarDuration.Value, volume: (ushort)(trackBarVolume.Value * 32767 / 200));

/// <summary>
/// Load the main window and indicate to play a beep sound
/// </summary>
/// <param name="sender">object sender</param>
/// <param name="e">event arguments</param>
/// <remarks>The parameters <paramref name="e"/> and <paramref name="sender"/> are not needed, but must be indicated.</remarks>
private void BeepForm_Load(object sender, EventArgs e) => PlayBeep(frequency: (ushort)trackBarFrequency.Value, msDuration: trackBarDuration.Value, volume: (ushort)trackBarVolume.Value);
private void BeepForm_Load(object sender, EventArgs e) => PlayBeep(frequency: (ushort)trackBarFrequency.Value, msDuration: trackBarDuration.Value, volume: (ushort)(trackBarVolume.Value * 32767 / 200));
}
}
3 changes: 3 additions & 0 deletions Beep/BeepForm.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,7 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="toolTip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
</root>
6 changes: 2 additions & 4 deletions Beep/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ private static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(defaultValue: false);
using (BeepForm mainForm = new BeepForm())
{
Application.Run(mainForm: mainForm);
}
using BeepForm mainForm = new BeepForm();
Application.Run(mainForm: mainForm);
}
}
}
4 changes: 2 additions & 2 deletions Beep/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden,
// übernehmen, indem Sie "*" eingeben:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.1.2")]
[assembly: AssemblyFileVersion("1.0.1.2")]
[assembly: AssemblyVersion("1.0.2.3")]
[assembly: AssemblyFileVersion("1.0.2.3")]
[assembly: NeutralResourcesLanguage("en")]

0 comments on commit db042fd

Please sign in to comment.