Hack Forums
[.NET] Beep manually done function - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] Beep manually done function (/showthread.php?tid=4913912)



Beep manually done function - Menalix - Jul 19, 2015

removed content do to leeching.


* - Only3 - Jul 19, 2015

have this generate wave code for wave class i made ... its much easy (with win os) to work with wav files then mp3


* - Menalix - Jul 19, 2015

(Jul 19, 2015 09:32 PM)Only3 Wrote:  have this generate wave code for wave class i made ... its much easy (with win os) to work with wav files then mp3

Idk about that, I only know I had to read way to much shit about wav files to make this haha :P but I wouldn't wonder if mp3 is more complicated.


* - Kinepak - Jul 19, 2015

Nice work, you can change the volume of the beep function in "system sound".


* - Menalix - Jul 19, 2015

(Jul 19, 2015 10:09 PM)Kinepak Wrote:  Nice work, you can change the volume of the beep function in "system sound".
Thanks, yea I know, like I wrote
Quote:and doesn't let you adjust the volume but just uses what your system sound volume is set at.

Adjusting the system sounds volume programatical is quite nasty since there's no direct API for it, and it also affect other software playing sounds using the system sounds volume.


* - Jordan "ESEA" Belfort - Jul 20, 2015

I cannot seem to get this working, what are some example values I should use. I am using 100 for volume, 250 for frequency and 60 for duration.


* - Menalix - Jul 20, 2015

(Jul 20, 2015 12:14 AM)A Y Y Y L M A O Wrote:  I cannot seem to get this working, what are some example values I should use. I am using 100 for volume, 250 for frequency and 60 for duration.

wierd, cause it works perfect for me with those vals:
Beep(100.0F, 250.0F, 60.0F)


* - Hiiro - Jul 20, 2015

C# version if you want to add it to OP,
I get the feeling I have mistaken something so feel free to scan over it and fix.

PHP Code

<?php 
private static bool Beep(double Volume, double Frequency, double Duration)
{
      try
      {
      
double Amplitude = Volume * 1.27;
      
double A = ((Amplitude * (System.Math.Pow(2, 15))) / 1000) - 1;
      
double DeltaFT = 2 * System.Math.PI * Frequency / 8000;

      
double Samples = 441 * (Duration / 100);
      
int Bytes = System.Convert.ToInt32(Samples) * 4;
      
int[] Hdr = new int[] { 0x46464952, 36 + Bytes, 0x45564157, 0x20746D66, 16, 0x20001, 8000, 176400, 0x100004, 0x61746164, Bytes };

      
using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(44 + Bytes))
      {
            
using (System.IO.BinaryWriter binaryWriter = new System.IO.BinaryWriter(memoryStream))
            {
            for (
int i = 0; i < Hdr.Length; i++)
            
binaryWriter.Write(Hdr[i]);

            for (
int T = 0; T < System.Convert.ToInt32(Samples); T++)
            {
            
short Sample = System.Convert.ToInt16(A * System.Math.Sin(DeltaFT * T));
            
binaryWriter.Write(Sample);
            
binaryWriter.Write(Sample);
            }
            
binaryWriter.Flush();
            
memoryStream.Seek(0, System.IO.SeekOrigin.Begin);

            
using (System.Media.SoundPlayer SP = new System.Media.SoundPlayer(memoryStream))
            
SP.PlaySync();
             }
      }
      }
      catch
      {
      return 
false;
      }
      return 
true;
} 



* - Lorenzon - Jul 20, 2015

Haha wow I'm impressed man, I would never have the patience to do something like this! I'll save this snippet if I'll ever need anything like it! Thanks!


* - Jordan "ESEA" Belfort - Jul 20, 2015

(Jul 20, 2015 12:44 AM)Menalix Wrote:  wierd, cause it works perfect for me with those vals:
Beep(100.0F, 250.0F, 60.0F)

I have it working correctly now, I had an error with my thread.