Solved

Creating sound in Delphi

Posted on 1998-10-02
11
447 Views
Last Modified: 2010-04-06
I would like to create sounds in Delphi by being able to specify the frequency of the sound and the length it is made
Example: procedure PlaySound(Freq: Word; Length: Word);

I need code on how to do this please.
0
Comment
Question by:Gerhard100198
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +2
11 Comments
 
LVL 5

Expert Comment

by:scrapdog
ID: 1341477
Since frequency and duration are your only parameters, you could do this through the PC Speaker.  Use this procedure (duration is in milliseconds).
-------------------------------------

procedure PlaySound(Freq: Word; Duration :Word);
var
   b,c : Byte;
   FirstTickCount :Longint;
begin
   if Freq > 18 then begin
      Freq := Word(1193181 div LongInt(Freq));
      asm
        mov DX, $61
        in  AL, DX
        mov B, AL
      end;
      c := b or 3;
      if (B and 3) = 0 then begin
         asm
           mov DX, $61
           mov AL, C
           out DX, AL
           mov DX, $43
           mov AL, $B6
           out DX, AL
         end
      end;
      {FreqLo := Lo(Freq);
      FreqHi := Hi(Freq);}
      asm
        mov DX, $42
        mov AX, Freq
        out DX, AL
        xchg AL, AH
        out DX, AL
      end;
      FirstTickCount:=GetTickCount;
      repeat
        Application.ProcessMessages; {allowing access to other controls, etc.}
      until ((GetTickCount-FirstTickCount) >= LongInt(Duration));
      asm
        mov DX, $61
        in  AL, DX
        and AL, $fc
        out DX, AL
      end;

   end;
end;

----------------------------------------

Example:  to play an A for one second

PlaySound(440, 1000);


This would be the simplest way to make sounds that vary only in frequency and duration.  To use something other than the PC speaker would require much more extensive programming.

Scrappy
0
 
LVL 2

Expert Comment

by:kjteng
ID: 1341478
0
 
LVL 1

Author Comment

by:Gerhard100198
ID: 1341479
I already have the above code.

Maybe I should have mentioned that I'm using D4 on an NT workstation.

When running the code I get a "privilaged instruction" error on the following line

procedure TForm1.PlaySound(Freq : Word; Duration : Word);
var
  b, c : Byte;
  FirstTickCount : Longint;
begin
  if Freq > 18 then begin
    Freq := Word(1193181 div LongInt(Freq));
    asm
             mov DX, $61
----->             in  AL, DX   <------------
             mov B, AL
    end;
    c := b or 3;

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:erajoj
ID: 1341480
Whats wrong with Windows.Beep(...)?

/// John
0
 
LVL 1

Author Comment

by:Gerhard100198
ID: 1341481
Erajoj,

If you're referring to the same Beep method available in Delphi:

It plays a default WAV file. I need to make a sound on every key press. A carp load full of "dinging" becomes very irritating. I need something where I can control the pitch and the duration of the sound.
0
 
LVL 1

Author Comment

by:Gerhard100198
ID: 1341482
I've just solved my own problem so I guess I get to keep the points. ;)

Thanks to all the input and all those that tried to help. I appreciate the effort.

I've located a freeware component with source on the Delphi Super page:
http://sunsite.icm.edu.pl/delphi/

It's pretty good and well worth using.

Thanks to the author of this component.
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1341483
No I meant the Beep function in the Windows Unit. Look it up!
If you don't, things will be unnecessarily complicated. It uses the Win32 API to generate a SQwave sound at a certain frequency and duration.
The ordinary Beep function isn't worth the dust beneath my soles.

And please, don't tell me I'm telling you something I am not telling you, because when I tell you something it's because I have something worth listening to to tell you.

/// John
0
 
LVL 1

Author Comment

by:Gerhard100198
ID: 1341484
Erajoj,

I found the function you're referring to. It looks perfect. I spoke to a friend of mine and he claims to have read that the said function only works under NT and not under Win95. Since I don't work on Win95 at all there is no way to test it.

BTW the point are yours. Post your last comment as an answer and I'll accept it. Thanks for the input.
0
 

Accepted Solution

by:
Tazi earned 10 total points
ID: 1341485
There is a component which I use to play a sound in Delphi..I haven't got the address of where to find it, if you could give me your email address, i will mail the component to you..

The code is really easy and plays a variety of sounds at different frequencies.

Speaker1.Play(200,200);

Bye..
Tazi
0
 
LVL 1

Author Comment

by:Gerhard100198
ID: 1341486
Thanks Tazi,

I've found a component called BTBeepere (with full source code). It works in much the sameway but has some nifty property and methods.

I will however accept you answer because it sounds like the first answer which will allow the PC speaker to be used in Delphi.
0
 

Expert Comment

by:Tazi
ID: 1341487
Gerhard, I am glad that I could have been of some assitance to u...
if u have further difficulties, send me your email address and I will mail the component that i am using to u.

Regards
Tazi....
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi TcxGrid group footer summary 3 358
error 1.1 400 Bad request idhttp delphi 18 208
delphi parse string to params 3 157
Need Help Delphi 2010 CheckBox1 Stored value in memo 13 82
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question