Link to home
Start Free TrialLog in
Avatar of BarryTang
BarryTang

asked on

How to play a specific frequency of wave sound out ?

I would like to play specific frequency of sound wave out.
The frequency of the sound is specific by me ( e.g. 445Hz ). I would also like to know how to control the duration of the playback
( i.e. how long it will play ) ?

Would anybody suggest an answer for me, thank you !!
Avatar of dennislw
dennislw

You can use the Beep API call:

Private Declare Function Beep Lib "kernel32" (ByVal dwFreq As Long, ByVal dwDuration As Long) As Long

Private Sub Command1_Click()
Beep 445, 3000 'this will beep at 445hz for 3 seconds
End Sub
Avatar of BarryTang

ASKER

Hi dennislw :

Thanks for information

However, this is a method for Win NT/2000 only.

Since my environment need to be at Win 98, do you know
if there is any method to do the same thing at this
environment ?

Thank you

This can be done with a direct port manipulation, I am searching to found the correct port...

or
You can use the Python library..
http://www.python.org/doc/current/lib/module-winsound.html

I haven't time to try this lib, but they can do that.


Sorry by the grammatical error, I speak spanish. I can talk in English but like Tarzan. Corrections are welcome.
Good Luck.
Déjà-vu
'This code use a dll called NTPort but work fine with Win9X and can be used to send anything to any port on your PC including the Speaker at port addresses $42 through $43.
You can found the dll ver 2.4 at and buy or crack it(your choice):
http://www.zealsoftstudio.com/ntport/download.html

If you want I can email you a "free" version.

If you have some BASM or C+ knowledge you can do it sending the data to this ports...


'Here are the code:

Option Explicit

Declare Sub Outport Lib "ntport.dll" (ByVal portID As Integer, ByVal nByte As Integer)
Declare Sub OutportW Lib "ntport.dll" (ByVal portID As Integer, ByVal nWord As Integer)
Declare Sub OutportD Lib "ntport.dll" (ByVal portID As Integer, ByVal dwDWord As Long)
Declare Function Inport Lib "ntport.dll" (ByVal portID As Integer) As Integer
Declare Function InportW Lib "ntport.dll" (ByVal portID As Integer) As Integer
Declare Function InportD Lib "ntport.dll" (ByVal portID As Integer) As Long

Declare Sub Outp Lib "ntport.dll" (ByVal portID As Integer, ByVal nByte As Integer)
Declare Sub Outpw Lib "ntport.dll" (ByVal portID As Integer, ByVal nWord As Integer)
Declare Sub Outpd Lib "ntport.dll" (ByVal portID As Integer, ByVal dwDWord As Long)
Declare Function Inp Lib "ntport.dll" (ByVal portID As Integer) As Integer
Declare Function Inpw Lib "ntport.dll" (ByVal portID As Integer) As Integer
Declare Function Inpd Lib "ntport.dll" (ByVal portID As Integer) As Long

Declare Function IsWinNT Lib "ntport.dll" () As Boolean
Declare Function GetLastState Lib "ntport.dll" (ByVal sStatus As String) As Long
Declare Function GetNTPortVersion Lib "ntport.dll" () As Integer

Declare Sub LicenseInfo Lib "ntport.dll" (ByVal sUser As String, ByVal lKey As Long)

Declare Sub EnablePorts Lib "ntport.dll" (ByVal portStart As Integer, ByVal portEnd As Integer)
Declare Sub DisablePorts Lib "ntport.dll" (ByVal portStart As Integer, ByVal portEnd As Integer)

Public Const ERROR_NONE = 0
Public Const ERROR_DRIVER = 6

Public Sub Speaker(bOn As Boolean)
    Dim portVal As Integer
   
    portVal = Inport(&H61)
    If bOn Then
        portVal = portVal Or 3
        portVal = &H2F
    Else
        portVal = portVal And (Not 3)
    End If
    Outport &H61, portVal
End Sub

' Speaker freq is set by setting divider in Intel
' 8253/8254 timer chip at port addresses $42 through $43
Public Sub SetFreq(hertz As Integer)
    Dim s As String
     
    If hertz Then
        Dim divisor As Long
        divisor = 1193180 / hertz
       
        Outport &H43, &HB6
   
        Outport &H42, divisor Mod 256
        Outport &H42, divisor \ 256
       
        Speaker True
    Else
        Speaker False
    End If
End Sub
Dear Deja-vu :

Thank very much for your information.

Just two more question I want to ask :

 1. Does your VB code provided also control the duration
    up to 1 millisecond ( as the Beep API )

 2. Can I make the sound card to output instead of using
    computer speaker

Also, it is better for me to have your version of NTPort dll for my testing purpose

Regards


ASKER CERTIFIED SOLUTION
Avatar of Deja-vu
Deja-vu

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Dear Deja-vu :

Actually it is better for me to use a soundcard ( instead
of using speaker ), so I would like to do program by DirectX.

Some of the DirectX command I already understand,
can you show me the DirectX code for generating a sound with specific frequency and control its duration.

Thank you

I will try (I don't understand all) but I haven't enough time right now.. waitme a few days. it's urgent?
Dear Deja-vu :

It is not so urgent, just work on it at your convenience.

But I must say thank you very much for help for the work
you did before and after.

Best Regards from Barry
Dear Deja-vu :

I find some code from the following links for your
reference

http://www.exhedra.com/DirectX4VB/Tutorials/DirectX7/DM_Modify.asp

It use DirectMusic to play single note of specific frequency and duration. But it still cannot play the sound under my Win 98 environment.

Can you figure out the reason ?

Barry
Dear Deja-vu :

Please !!!  Can you send me free version of NTport ??
I think some version without splash window (i hate it.. grrr) .. or license info

Thank you very very much !!!!

<Edited by SpideyMod>

(Sorry for my English .. I am Czech)

Segeer (from WMWMasters)
BarryTang,
You have 17 opened questions as of this date.  
I will be posting in each of these questions.  
Please take care of your opened questions older than 30 days by clicking on your logon name link and looking for UnlockedQ or using the links I am providing below.  
Please finalize these within 72 hours or I will ask administration to look into your account at that point.  If you have questions, please ask and I will provide assistance.  

https://www.experts-exchange.com/questions/20187331/Problem-related-to-Unknown-token-received-from-SQL-Server.html
https://www.experts-exchange.com/questions/20133725/Access-Foxpro-2-6-free-tables-from-SQL-server-2000.html
https://www.experts-exchange.com/questions/20088012/Problem-related-to-insert-a-group-of-record-to-a-data-file.html
https://www.experts-exchange.com/questions/20083509/Connection-Failure-using-local-harddisk.html
https://www.experts-exchange.com/questions/20382035/How-can-I-extract-a-music-track-in-a-midi-file-by-DirectMusic-in-VB.html
https://www.experts-exchange.com/questions/20382294/How-can-I-extract-a-music-track-in-a-midi-file-by-DirectMusic-in-VB.html
https://www.experts-exchange.com/questions/20534626/DLL-Function-can-run-in-exe-but-fail-to-run-in-VB6-environment.html
https://www.experts-exchange.com/questions/20521771/How-can-I-avoid-the-split-of-word.html
https://www.experts-exchange.com/questions/20515842/Can-I-use-this-DLL-in-VB6.html
https://www.experts-exchange.com/questions/20513575/Problem-related-to-shell-wait.html
https://www.experts-exchange.com/questions/20493221/Problem-related-to-the-scroll-value.html
https://www.experts-exchange.com/questions/20436510/How-can-I-made-a-form-on-top-but-will-disappear-when-switch-to-screen-of-other-windows-task.html
https://www.experts-exchange.com/questions/20399960/How-to-use-a-C-dll-in-a-VB6-program.html
https://www.experts-exchange.com/questions/20395868/How-to-play-a-specific-frequency-of-wave-sound-out.html
https://www.experts-exchange.com/questions/20375410/How-can-I-display-special-characters-in-VB-controls.html
https://www.experts-exchange.com/questions/20373966/How-to-display-musical-note-from-midi-file-in-a-VB-program.html
https://www.experts-exchange.com/questions/20372811/How-to-use-DLL-in-my-VB-program.html

Thank you.
BarryTang,
No comment has been added lately (47 days), so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:

RECOMMENDATION: PAQ/No Refund

Please leave any comments here within 7 days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Thanks,

Dabas
EE Cleanup Volunteer
---------------------
If you feel that your question was not properly addressed, or that none of the comments received were appropriate answers, please post a request in Community support (with a link to this page) to refund your points. https://www.experts-exchange.com/Community_Support/
Personally, I believe Deja-vu should get the points on this one.  2 Methods were given, lots of links with information.  It is likely that these links would have solved the problem.

The moderators might want to take a look at the comment by Segeer as well.
Administrative Action - Force Accepted.

SpideyMod
Community Support Moderator @Experts Exchange
Segeer,
Asking for a hack/crack, or means to remove demo versions, Keys, etc is a severe violation of the membership agreement.  I need you to re-read the membership agreement and indicate your understanding and future compliance within 72 hours or I have an obligation to turn your account to site administration for review.

The Membership agreement can be found at:
https://www.experts-exchange.com/jsp/infoMemberAgreement.jsp 


Additionally, I have removed your email address from your coment as off-site handling of questions is not allowed.

twalgrave, thanks for the heads-up on this.

SpideyMod
Community Support Moderator @Experts Exchange