how to make a short beep

I want to make a short (fast) high pitched beep noise to help with debugging.

How do I play a sine wave at a certain frequency for a fraction of a second in Firemonkey? 

I'm okay with making one version for MSWINDOWS and another for MACINTOSH if necessary.
0
Michael
9/17/2014 9:00:09 PM
embarcadero.delphi.firemonkey 4901 articles. 4 followers. Follow

11 Replies
3022 Views

Similar Articles

[PageSpeed] 50

Michael Leahy wrote:

> I want to make a short (fast) high pitched beep noise to help with debugging.
> 
> How do I play a sine wave at a certain frequency for a fraction of a second in Firemonkey? 
> 
> I'm okay with making one version for MSWINDOWS and another for MACINTOSH if necessary.

Michael,
I can't help you with MAC, but for Win you could take a look at this:
http://www.swissdelphicenter.ch/en/showcode.php?id=1324
It creates a sound in WAVE format in a memory stream of needed frequency, duration and volume and
then uses sndPlaySound to output the sound. I have not used this code but I don't see any reason
why it wouldn't work under FMX.

-- 
Tom Brunberg
firstname.lastname@welho.com
0
Tom
9/18/2014 8:24:11 PM
I drop a Tmediaplayer on the form. Download a commercial free beep.mp3. and use the code below.

    TMediaPlayer1.FileName := TPath.GetDocumentsPath + PathDelim + 'Sounds' + PathDelim +  beep.mp3;
    TMediaPlayer1.Play;
0
Chris
9/18/2014 8:36:43 PM
> {quote:title=Chris Dunn wrote:}{quote}
> I drop a Tmediaplayer on the form. Download a commercial free beep.mp3. and use the code below.
> 
>     TMediaPlayer1.FileName := TPath.GetDocumentsPath + PathDelim + 'Sounds' + PathDelim +  beep.mp3;
>     TMediaPlayer1.Play;

Playing a short MP3 with the MediaPlayer seems very code heavy for what I'm up to.

I'm hoping for something like the old Beep(800, 500) calls that will very quickly play a short tone so that I can listen to them in a high speed process to help debug it.
0
Michael
9/18/2014 9:06:04 PM
Michael Leahy wrote:

> > {quote:title=Chris Dunn wrote:}{quote}
> > I drop a Tmediaplayer on the form. Download a commercial free beep.mp3. and use the code below.
> > 
> >     TMediaPlayer1.FileName := TPath.GetDocumentsPath + PathDelim + 'Sounds' + PathDelim +
> > beep.mp3;     TMediaPlayer1.Play;
> 
> Playing a short MP3 with the MediaPlayer seems very code heavy for what I'm up to.
> 
> I'm hoping for something like the old Beep(800, 500) calls that will very quickly play a short
> tone so that I can listen to them in a high speed process to help debug it.

Did you try the code at the link I gave you in my previous post?
I did, and it didn't work. It's obviously written before D2009 and a change is necessary:
The following, original string constants need to be changed to AnsiString:
{code}
   RiffId: ansistring = 'RIFF';
   WaveId: ansistring = 'WAVE';
   FmtId: ansistring = 'fmt ';
   DataId: ansistring = 'data';
{code}

I also rearranged the code, to create the sound data only once and then just reuse it, and made a
class of it.
{code}
uses
  ...  
  windows, mmsystem;

type
  TVolumeLevel = 0..127;

  TNoise = class
  private
    ms: TMemoryStream;
  public
    constructor Create(Frequency{Hz}, Duration{mSec}: Integer; Volume: TVolumeLevel);
    destructor Destroy; override;
    procedure Play;
  end;

implementation
  ...
{ TNoise }

constructor TNoise.Create(Frequency, Duration: Integer; Volume: TVolumeLevel);
var
   WaveFormatEx: TWaveFormatEx;
   i, TempInt, DataCount, RiffCount: integer;
   SoundValue: byte;
   w: double; // omega ( 2 * pi * frequency)
const
   Mono: Word = $0001;
   SampleRate: Integer = 11025; // 8000, 11025, 22050, or 44100
   RiffId: ansistring = 'RIFF';
   WaveId: ansistring = 'WAVE';
   FmtId: ansistring = 'fmt ';
   DataId: ansistring = 'data';
begin
   if Frequency > (0.6 * SampleRate) then
   begin
     ShowMessage(Format('Sample rate of %d is too Low to play a tone of %dHz',
       [SampleRate, Frequency]));
     Exit;
   end;
   with WaveFormatEx do
   begin
     wFormatTag := WAVE_FORMAT_PCM;
     nChannels := Mono;
     nSamplesPerSec := SampleRate;
     wBitsPerSample := $0008;
     nBlockAlign := (nChannels * wBitsPerSample) div 8;
     nAvgBytesPerSec := nSamplesPerSec * nBlockAlign;
     cbSize := 0;
   end;
   MS := TMemoryStream.Create;
   with MS do
   begin
     {Calculate length of sound data and of file data}
     DataCount := (Duration * SampleRate) div 1000; // sound data
     RiffCount := Length(WaveId) + Length(FmtId) + SizeOf(DWORD) +
       SizeOf(TWaveFormatEx) + Length(DataId) + SizeOf(DWORD) + DataCount; // file data
     {write out the wave header}
     Write(RiffId[1], 4); // 'RIFF'
     Write(RiffCount, SizeOf(DWORD)); // file data size
     Write(WaveId[1], Length(WaveId)); // 'WAVE'
     Write(FmtId[1], Length(FmtId)); // 'fmt '
     TempInt := SizeOf(TWaveFormatEx);
     Write(TempInt, SizeOf(DWORD)); // TWaveFormat data size
     Write(WaveFormatEx, SizeOf(TWaveFormatEx)); // WaveFormatEx record
     Write(DataId[1], Length(DataId)); // 'data'
     Write(DataCount, SizeOf(DWORD)); // sound data size
     {calculate and write out the tone signal} // now the data values
     w := 2 * Pi * Frequency; // omega
     for i := 0 to DataCount - 1 do
     begin
       SoundValue := 127 + trunc(Volume * sin(i * w / SampleRate)); // wt = w * i / SampleRate
       Write(SoundValue, SizeOf(Byte));
     end;
   end;
end;

destructor TNoise.Destroy;
begin
  ms.Free;
  inherited;
end;

procedure TNoise.Play;
begin
  sndPlaySound(MS.Memory, SND_MEMORY or SND_ASYNC);
end;

{code}
Note that I also changed to Async mode, not to stop the program for the sound duration.
Usage:
{code}
procedure TForm4.Button1Click(Sender: TObject);
begin
  myBeep := TNoise.Create(3800, 100, 120);
end;

procedure TForm4.Button2Click(Sender: TObject);
begin
  myBeep.Play;
end;
{code}


-- 
Tom Brunberg
firstname.lastname@welho.com
0
Tom
9/19/2014 1:01:01 AM
> {quote:title=Tom Brunberg wrote:}{quote}
> Michael Leahy wrote:
> 
> > > {quote:title=Chris Dunn wrote:}{quote}
> > > I drop a Tmediaplayer on the form. Download a commercial free beep.mp3. and use the code below.
> > > 
> > >     TMediaPlayer1.FileName := TPath.GetDocumentsPath + PathDelim + 'Sounds' + PathDelim +
> > > beep.mp3;     TMediaPlayer1.Play;
> > 
> > Playing a short MP3 with the MediaPlayer seems very code heavy for what I'm up to.
> > 
> > I'm hoping for something like the old Beep(800, 500) calls that will very quickly play a short
> > tone so that I can listen to them in a high speed process to help debug it.
> 
> Did you try the code at the link I gave you in my previous post?
> I did, and it didn't work. It's obviously written before D2009 and a change is necessary:
> The following, original string constants need to be changed to AnsiString:
> {code}
>    RiffId: ansistring = 'RIFF';
>    WaveId: ansistring = 'WAVE';
>    FmtId: ansistring = 'fmt ';
>    DataId: ansistring = 'data';
> {code}
> 
> I also rearranged the code, to create the sound data only once and then just reuse it, and made a
> class of it.
> {code}
> uses
>   ...  
>   windows, mmsystem;
> 
> type
>   TVolumeLevel = 0..127;
> 
>   TNoise = class
>   private
>     ms: TMemoryStream;
>   public
>     constructor Create(Frequency{Hz}, Duration{mSec}: Integer; Volume: TVolumeLevel);
>     destructor Destroy; override;
>     procedure Play;
>   end;
> 
> implementation
>   ...
> { TNoise }
> 
> constructor TNoise.Create(Frequency, Duration: Integer; Volume: TVolumeLevel);
> var
>    WaveFormatEx: TWaveFormatEx;
>    i, TempInt, DataCount, RiffCount: integer;
>    SoundValue: byte;
>    w: double; // omega ( 2 * pi * frequency)
> const
>    Mono: Word = $0001;
>    SampleRate: Integer = 11025; // 8000, 11025, 22050, or 44100
>    RiffId: ansistring = 'RIFF';
>    WaveId: ansistring = 'WAVE';
>    FmtId: ansistring = 'fmt ';
>    DataId: ansistring = 'data';
> begin
>    if Frequency > (0.6 * SampleRate) then
>    begin
>      ShowMessage(Format('Sample rate of %d is too Low to play a tone of %dHz',
>        [SampleRate, Frequency]));
>      Exit;
>    end;
>    with WaveFormatEx do
>    begin
>      wFormatTag := WAVE_FORMAT_PCM;
>      nChannels := Mono;
>      nSamplesPerSec := SampleRate;
>      wBitsPerSample := $0008;
>      nBlockAlign := (nChannels * wBitsPerSample) div 8;
>      nAvgBytesPerSec := nSamplesPerSec * nBlockAlign;
>      cbSize := 0;
>    end;
>    MS := TMemoryStream.Create;
>    with MS do
>    begin
>      {Calculate length of sound data and of file data}
>      DataCount := (Duration * SampleRate) div 1000; // sound data
>      RiffCount := Length(WaveId) + Length(FmtId) + SizeOf(DWORD) +
>        SizeOf(TWaveFormatEx) + Length(DataId) + SizeOf(DWORD) + DataCount; // file data
>      {write out the wave header}
>      Write(RiffId[1], 4); // 'RIFF'
>      Write(RiffCount, SizeOf(DWORD)); // file data size
>      Write(WaveId[1], Length(WaveId)); // 'WAVE'
>      Write(FmtId[1], Length(FmtId)); // 'fmt '
>      TempInt := SizeOf(TWaveFormatEx);
>      Write(TempInt, SizeOf(DWORD)); // TWaveFormat data size
>      Write(WaveFormatEx, SizeOf(TWaveFormatEx)); // WaveFormatEx record
>      Write(DataId[1], Length(DataId)); // 'data'
>      Write(DataCount, SizeOf(DWORD)); // sound data size
>      {calculate and write out the tone signal} // now the data values
>      w := 2 * Pi * Frequency; // omega
>      for i := 0 to DataCount - 1 do
>      begin
>        SoundValue := 127 + trunc(Volume * sin(i * w / SampleRate)); // wt = w * i / SampleRate
>        Write(SoundValue, SizeOf(Byte));
>      end;
>    end;
> end;
> 
> destructor TNoise.Destroy;
> begin
>   ms.Free;
>   inherited;
> end;
> 
> procedure TNoise.Play;
> begin
>   sndPlaySound(MS.Memory, SND_MEMORY or SND_ASYNC);
> end;
> 
> {code}
> Note that I also changed to Async mode, not to stop the program for the sound duration.
> Usage:
> {code}
> procedure TForm4.Button1Click(Sender: TObject);
> begin
>   myBeep := TNoise.Create(3800, 100, 120);
> end;
> 
> procedure TForm4.Button2Click(Sender: TObject);
> begin
>   myBeep.Play;
> end;
> {code}
> 
> 
> -- 
> Tom Brunberg
> firstname.lastname@welho.com

Thanks, Tom.

I added this to XE6.

When I click the button, it delays a half second or sometimes a full second or more before playing the sound. I need something that will play nearly instantly every time (<100ms delay) and reset to play again almost immediately (<50ms). 

I tried removing SND_ASYNC but it behaved the same way.
0
Michael
9/23/2014 1:01:01 AM
> {quote:title=Michael Leahy wrote:}{quote}
> I want to make a short (fast) high pitched beep noise to help with debugging.
> 
> How do I play a sine wave at a certain frequency for a fraction of a second in Firemonkey? 
> 
> I'm okay with making one version for MSWINDOWS and another for MACINTOSH if necessary.

Reminds me of old school debugging on the Apple ][ with ASCII chr(7) [aka CTRL-G] :)

http://en.wikipedia.org/wiki/Bell_character

Since you are looking for custom tones, you have found you have to work a bit more for that.  One thing I spotted was these libs and might have something useful for you. http://www.mitov.com/products/audiolab#overview
--
John Frazier (Embarcadero Newsgroup Admin)
0
John
9/23/2014 1:01:01 AM
> {quote:title=Tom Brunberg wrote:}{quote}
> Did you try the code at the link I gave you in my previous post?
> I also rearranged the code, to create the sound data only once and then just reuse it, and made a
> class of it.

I tried this with XE7 and an actual Windows machine and it is much more responsive.

Perhaps my problem was with VM on the Mac slowing down the sound.
0
Michael
9/24/2014 1:01:01 AM
Michael Leahy wrote:

> > {quote:title=Tom Brunberg wrote:}{quote}
> > Did you try the code at the link I gave you in my previous post?
> > I also rearranged the code, to create the sound data only once and then just reuse it, and made
> > a class of it.
> 
> I tried this with XE7 and an actual Windows machine and it is much more responsive.
> 
> Perhaps my problem was with VM on the Mac slowing down the sound.

Yeah Michael, that wouldn't surprise me at all.

Cheers

-- 
Tom Brunberg
firstname.lastname@welho.com
0
Tom
9/24/2014 1:01:01 AM
> {quote:title=John Frazier wrote:}{quote}
> > {quote:title=Michael Leahy wrote:}{quote}
> > I want to make a short (fast) high pitched beep noise to help with debugging.
> > 
> > How do I play a sine wave at a certain frequency for a fraction of a second in Firemonkey? 
> > 
> > I'm okay with making one version for MSWINDOWS and another for MACINTOSH if necessary.
> 
> Reminds me of old school debugging on the Apple ][ with ASCII chr(7) [aka CTRL-G] :)
> 
> http://en.wikipedia.org/wiki/Bell_character
> 
> Since you are looking for custom tones, you have found you have to work a bit more for that.  One thing I spotted was these libs and might have something useful for you. http://www.mitov.com/products/audiolab#overview
> --
> John Frazier (Embarcadero Newsgroup Admin)

My legacy VCL code uses Mitov's libraries for multimedia.

I am still looking for a way to make a short fast beep (tone) that works in both MSWINDOWS and MACOS. I may have to repost the question.
0
Michael
9/24/2014 1:01:01 AM
> {quote:title=Michael Leahy wrote:}{quote}
> My legacy VCL code uses Mitov's libraries for multimedia.
> 
> I am still looking for a way to make a short fast beep (tone) that works in both MSWINDOWS and MACOS. I may have to repost the question.

I noticed Boian mentions FM here http://www.mitov.com/products/audiolab#downloads though it is not clear if it works on platforms other than Windows.  I saw that he has introduced some Android support, might be worth asking if he has anything to test :) I looked briefly and this seemed promising http://mitov.com/installer/104
--
John Frazier (Embarcadero Newsgroup Admin)
0
John
9/24/2014 11:54:46 PM
> {quote:title=John Frazier wrote:}{quote}
> > {quote:title=Michael Leahy wrote:}{quote}
> > My legacy VCL code uses Mitov's libraries for multimedia.
> > 
> > I am still looking for a way to make a short fast beep (tone) that works in both MSWINDOWS and MACOS. I may have to repost the question.
> 
> I noticed Boian mentions FM here http://www.mitov.com/products/audiolab#downloads though it is not clear if it works on platforms other than Windows.  I saw that he has introduced some Android support, might be worth asking if he has anything to test :) I looked briefly and this seemed promising http://mitov.com/installer/104
> --
> John Frazier (Embarcadero Newsgroup Admin)

I did ask Boian. The multimedia components are not yet ready for MACOS.
0
Michael
9/25/2014 2:14:22 PM
Reply:

Similar Artilces:

Making Firemonkey more compatible with Delphi
I have programmed in Delphi since 1995 Delphi1 I am now evaluating Delphi XE4. It boggles my mind, why embarcadero has lost the most important aspects of Delphi. Simplicity and Fast User Interface creation. I can easily see how Firemonkey can be improved to bring it in line with traditional Delphi Techniques by doing the following. 1. All Firemonkey controls need a data aware counterpart. 2. Firemonkey Styles need to be converted to TControl descendants. 3. ie. TLayout goes to TPanel ( TFMXPanel ) 4. ie. TRectangle does to TFrame TFMXFrame ) 5. LiveBindings gets placed un...

BEEP! BEEP!
Server #2 is unable to communicate with Server #1. Shows a DS error. BEEP EVERY 5 SECONDS! "I am going nuts." It is impossible to bring up server #1 again. How do I stop this BEEP?? SERVER#2: NetWare 5.1, holds Master Replica, rooter rooter wrote: > Server #2 is unable to communicate with Server #1. Shows a DS error. > > BEEP EVERY 5 SECONDS! > > "I am going nuts." > > It is impossible to bring up server #1 again. > > How do I stop this BEEP?? > > SERVER#2: > NetWare 5.1, holds Master Replica,...

Making should be make
Name: Scott Melton Email: scott_rides_againatyahoodotcom Product: Firefox Summary: Making should be make Comments: The opening page for Bugzilla is poorly worded. http://www.mozilla.org/projects/seamonkey/start/ The second line: If you want to help making this application even better, we would encourage you to take part in the should be MAKE, not MAKING Browser Details: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2) Gecko/20070222 SeaMonkey/1.1.1 ...

Ann: FastReport FMX for Delphi XE3 (FireMonkey 2) and XE2 (FireMonkey) released
Dear friends, Visual report designer is available now on both MS Windows and MAC OS X! Full sources. We can write here full list of possibilities, but it will so long. Let see FastReport VCL and FastReport FMX comparison table: http://www.fast-report.com/en/products/comparison-fast-report-firemonkey-vcl.html And all of this - just now in your hands for business-applications for Mac. Report generator FastReport FMX is the first multiplatform solution for including Business Intelligence into software based on Embarcadero FireMonkey IDE (Delphi for MS Windows and Apple Mac OS X)...

Ann: X-platform report generator for Delphi XE3 (FireMonkey 2) and XE2 (FireMonkey) released
Dear friends, Visual report designer is available now on both MS Windows and MAC OS X! Full sources. We can write here full list of possibilities, but it will so long. Let see FastReport VCL and FastReport FMX comparison table: http://www.fast-report.com/en/products/comparison-fast-report-firemonkey-vcl.html And all of this - just now in your hands for business-applications for Mac. Report generator FastReport FMX is the first multiplatform solution for including Business Intelligence into software based on Embarcadero FireMonkey IDE (Delphi for MS Windows and Apple Mac OS X)...

short, Short, SHORT coverage of security
Hi; When we ship our portal to customers, we will have a "suggested" Web.Config for their use but I am guessing most will need to tweak it. My concern is that most people administering this will not invest hours to get up to speed on security. Yes they should - but they won't. Nor will they hire someone who knows this. Is there something that will give a sys-admin type what they need to know for settings (ASP.NET users/roles, ActiveDirectory, and/or integrated) they might want to make that can be read in 5 minutes, 10 at most. I figure that's the longest it can be and they will still read ...

How to make a backup from Delphi
Does anybody have a sample how to run a backup from within Delphi? (Environment: Netware, SQL Anywhere 5.5) Juergen Loewner ...

Beep noise
Windows and Macintosh each have their signature "bad beep" noises. How do I play them in Firemonkey? > {quote:title=Michael Leahy wrote:}{quote} > Windows and Macintosh each have their signature "bad beep" noises. How do I play them in Firemonkey? It seems the Beep() call does the trick. I'll ask about shorter faster beeps in another question. > {quote:title=Michael Leahy wrote:}{quote} > Windows and Macintosh each have their signature "bad beep" noises. How do I play them in Firemonkey? Hi the Beep() is working when I select win 32 ...

How to make description short ...
 Hello, I'm working on an app which displays some large body of text in adatalist. The text can be up to 500 characters long which is long consideringI have records to bind possibly over 20. So what I want to do ismake the description shorter such as "This is a very long description ... the end" or something along those lineswhereas the user clicks on the ellipse (...) and the rest of the description shows up. Can someone help with this? Thanks in advance,Programor   hi,u should use hyperlink control to display discription.in ItemDataBound Event ...

beep R beep C beep 3
Here lies the third and final release candidate: ftp://ftp.linux.activestate.com/pub/staff/gsar/APC/5.6.0/perl-5.6.0-RC3.patch.gz ftp://ftp.linux.activestate.com/pub/staff/gsar/APC/5.6.0/perl-5.6.0-RC3.tar.gz And yes, the patch is against 5.5.670, but this here summary is against RC2. Bug fixes: + open($fh[0],...) autovivifies properly + closures inherit attributes correctly + "\N{...}" used without "use charnames ..." produces better diagnostic + "\N{...}" enables utf8-ness of string correctly + return values fr...

beep R beep C beep 3
Here lies the third and final release candidate: ftp://ftp.linux.activestate.com/pub/staff/gsar/APC/5.6.0/perl-5.6.0-RC3.patch.gz ftp://ftp.linux.activestate.com/pub/staff/gsar/APC/5.6.0/perl-5.6.0-RC3.tar.gz And yes, the patch is against 5.5.670, but this here summary is against RC2. Bug fixes: + open($fh[0],...) autovivifies properly + closures inherit attributes correctly + "\N{...}" used without "use charnames ..." produces better diagnostic + "\N{...}" enables utf8-ness of string correctly + return ...

Delphi MAKE conflicting with other compilers
I get the error, ERROR: You are using a non-Cygwin compatible Make program. Currently using: C:/ProgramFiles(x86)/Embarcadero/RADStudio/9.0/bin/make does anyone has a suggestion avoid the conflicting programs? Gilbert Padilla wrote: > I get the error, > > ERROR: You are using a non-Cygwin compatible Make program. > Currently using: C:/ProgramFiles(x86)/Embarcadero/RADStudio/9.0/bin/make > > does anyone has a suggestion avoid the conflicting programs? The rad studio bin directory is added to the path, when you need to use a different make use the full p...

How to make a procedure in Delphi? [Edit]
I want to know how to make a procedure in Delphi. I will be calling this procedure whenever value of EditBox, ComboBox and RadioButton(s) change. {code} procedure GetName(); begin if TForm1.edbName.Text <> '' then if TForm1.TComboBox.Text <> '' then if TForm1.rdbPersonal.Checked then TForm1.edbName.Text := Name1 + 'Dummy' else if TForm1.rdbCommercial.Checked then TForm1.edbName.Text := Name2 + 'Dummy' ...

Delphi XE2, OSX and FireMonkey?
1. Does anyone know if developing with FireMonkey will be the only way to develop for OSX with XE2? 2. Does anyone know if you will need to compile on a Mac for OSX, or will XE2 cross compile? I know this will all come out during the world tour, but I'm just trying to get some idea so I can start planning. 1. Yes, I think for now it will be the only way. But you can probably build a native UI for OSX by directly using the API, like you can also develop Win32 applications without using the VCL. 2. XE2 will only be available for Windows, but it will compile for other platforms. &l...