Jump to content

Play X hertz


Recommended Posts

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

Link to comment
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

Link to comment
Share on other sites

  • 3 years later...

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()
Link to comment
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

Link to comment
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.

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

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...