Sign in to follow this  
Followers 0
Chris86

Play X hertz

13 posts in this topic

How can I play the X hertz from my speaker?

I would like to have something that is equally Beep feature, but it must be played back from the speaker

Share this post


Link to post
Share on other sites



Look for MIDI UDF on board, but im not quite sure if it allow to play some frequency (in hz) from the speaker. But I wish you good luck in searching :P


Share this post


Link to post
Share on other sites

Bass.dll has it covered :P

Global Const $BASS_SAMPLE_LOOP = 4  
Global Const $BASS_SAMPLE_OVER_POS = 0x20000

$Hz = 40
$L = 1000
$A = 32760
$SR = 22000
$Pi = 3.1415 
$F = (2 * $Pi * $Hz) / $SR

$bass_dll = DllOpen ("Bass.dll")

$ret = DllCall($bass_dll, "int", "BASS_Init", "int", -1, _
        "int", 44100, _
        "int", 0, _
        "hwnd", 0, _
        "ptr", 0)
If Not $ret[0] Then
    MsgBox(0, "Error", 'Error initializing audio!');
    Exit
EndIf

$HSAMPLE = DllCall ($bass_dll, "dword", "BASS_SampleCreate", "dword", 256, "dword", 28160, "dword", 1, "dword", 1, "dword", $BASS_SAMPLE_LOOP + $BASS_SAMPLE_OVER_POS); // create sample

$struct = DllStructCreate ("char[128]")

For $i = 0 To 128
    $data = ($A * (sin($F * $i)))
    DllStructSetData ($struct, 1, $data, $i)
    $ret = DllCall ($bass_dll, "dword", "BASS_SampleSetData", "dword", $HSAMPLE[0], "ptr", DllStructGetPtr ($struct)); // set the sample's data 
Next
$hchan = DllCall ($bass_dll, "dword", "BASS_SampleGetChannel", "dword", $HSAMPLE[0], "int", 1)

$ret = DllCall($bass_dll, "int", "BASS_ChannelPlay", "dword", $hchan[0], "int", 0)
Sleep (5000)

DllCall ($bass_dll, "int", "BASS_SampleStop", "dword", $HSAMPLE[0])
DllCall ($bass_dll, "int", "BOOL BASS_Free")

Pretty sure my method of calculation was wayyy off, but I'm sure someone can fix that up :P

Share this post


Link to post
Share on other sites

Oh, yeah bass.dll ! I have forgothed about it :P Thx for fixing me :P


Share this post


Link to post
Share on other sites

I posted it because it made some sound, but from the bass.dll example, its audibly different.

For a 440Hz wave, they had:

HSAMPLE sample=BASS_SampleCreate(256, 28160, 1, 1, BASS_SAMPLE_LOOP|BASS_SAMPLE_OVER_POS); // create sample
short data[128]; // data buffer
int a;
for (a=0; a<128; a++)
    data[a]=(short)(32767.0*sin((double)a*6.283185/64)); // sine wave
BASS_SampleSetData(sample, data); // set the sample's data

Different :|

I got my method from http://www.geocities.com/SiliconValley/Cam...8645/synth.html

Share this post


Link to post
Share on other sites

Can anybody make this working? BASS_SampleCreate

and then Play with BASS?

Pleaseeee for working code.......

Share this post


Link to post
Share on other sites

Here you are:

#AutoIt3Wrapper_UseX64=n
#include "Bass.au3"

Global $nFreq = 440 ; Hz
Global Const $pi = ATan(1) * 4

_BASS_Startup()
_BASS_Init(0, -1, 44100, 0, "")

Global $hSample = _Bass_SampleCreate(88200, 44100, 1, 1, $BASS_SAMPLE_LOOP)

Global $tSampleData = DllStructCreate("short[44100]")
Global $fTmp = $pi * 2 / (44100 / $nFreq)
For $i = 1 To 44100
DllStructSetData($tSampleData, 1, 32767 * Sin($i * $fTmp), $i)
Next
_Bass_SampleSetData($hSample, DllStructGetPtr($tSampleData))


Global $hChan = _Bass_SampleGetChannel($hSample, 0)
_BASS_ChannelSetVolume($hChan, 30)
_Bass_ChannelPlay($hChan, 0)

Sleep(5000)

_Bass_SampleStop($hSample)
_BASS_SampleFree($hSample)
_BASS_Free()

Share this post


Link to post
Share on other sites

thanks eukalyptus but: compile and run working OK .....

but on my speakers is silence! :D

?

Share this post


Link to post
Share on other sites

thanks eukalyptus but: compile and run working OK .....

but on my speakers is silence! :D

?

That script worked for me, are you sure it wasn't playing and the volume was just too low, because that frequency is hard to hear at a low volume.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

I try with sox play dos command:

_RunDos("play -n -n synth 1 sine 440 ")

and I hear 440Hz

....

Share this post


Link to post
Share on other sites

I test on 2 PCs (winXP SP3) and on both not working!

Compile and RUN OK ..but nothing hear from speakers....

?

Share this post


Link to post
Share on other sites

You do have the Bass.dll file and it's located in the script directory correct?

BTW, there's no reason to compile the script if all you want to do is test it, you can run it from SciTE by pressing F5 if you're using SciTE.

1 person likes this

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Nice!! working! :D

I copy bass.dll to working directory and all working! Thanks BrewManNH!!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0