Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Beep Beep

1 view
Skip to first unread message

Jim Mack

unread,
Mar 21, 2009, 2:38:24 PM3/21/09
to
Lorin wrote:
> What can I do to replace the BEEP Api?
> VB6 on Vista PC.
> Want the same kind of frequency control, etc.

Gotta wonder why, but it's fairly easy.

You can generate WAV images on the fly (in byte arrays) and use the
PlaySound API with them. If you can decide in advance what you need,
you can place WAV images into a resource and play them from there.

Our Stamina library (www.mdxi.com) offers sound functions (e.g.
DxWaveMulti) that create these ad hoc WAVs in microseconds. Pure VB
code can do almost as well.

--
Jim Mack
Twisted tees at http://www.cafepress.com/2050inc
"We sew confusion"

Abhishek

unread,
Mar 21, 2009, 4:27:37 PM3/21/09
to
can you wav lib generated sound of any frequency ? I need sound/wav files of
particular frequency (non hearable by humans) to use in a mosquito repellent
software :)


Jim Mack

unread,
Mar 21, 2009, 6:06:40 PM3/21/09
to

Yes, up to the Nyquist limit -- roughly half the sampling frequency.
So at 48000 samples/sec, the highest frequency would be about 24kHz,
which is above what (most) humans can hear.

I don't have a RIFF/WAVE header spec in front of me, but I'm pretty
sure that sampling can be set to higher values than 48K. Whether MS
software will play it, or sounds cards and transducers will reproduce
it, is another matter.

Actually, now that I look at our spec... the published routines are
all preset to stereo 11025Hz, making the highest frequency about 5kHz.
But if you need it, I'd raise the limits to whatever you require. It's
just constants in the ASM source code.

--
Jim

Abhishek

unread,
Mar 21, 2009, 7:09:56 PM3/21/09
to
I want to produce sound between range of 16000Hz to 20000Hz. can your dll do
it, how much your dll costs or it can be done thought the Win API directly ?

produce sound between 16000Hz to 20000Hz using WinAPI and output to
speakers. (though humans wont be able to hear it)
or
record wav of this frequency and play then in standard way.

can anyone help me out on this?

thanks.


Michael Williams

unread,
Mar 21, 2009, 7:16:08 PM3/21/09
to
"Jim Mack" <jm...@mdxi.nospam.com> wrote in message
news:uMPQRFnq...@TK2MSFTNGP03.phx.gbl...

> I don't have a RIFF/WAVE header spec in front of me,
> but I'm pretty sure that sampling can be set to higher
> values than 48K. Whether MS software will play it,
> or sounds cards and transducers will reproduce
> it, is another matter.

I'm not sure what the limit is but out of curiosity I've just created a 16
bit mono 1 KHz sine wave at a sample rate of one million samples per second
and it plays just fine (using sndPlaySound) on my system which has a
standard cheap motherboard sound chip, although that's about as high as I
can go with it. I was actually quite surprised that such a high sample rate
played okay and of course it is massively more than the OP would require.

Mike


Jim Mack

unread,
Mar 21, 2009, 8:10:55 PM3/21/09
to

Right -- the test of the whole chain would be to create a sound sample
at (say) 96K, of a 40khZ tone. If the sound makes it through the
system (as seen on a scope), there's the answer. But I suspect every
system is different -- most sound cards are probably spec'd with a
20kHz top end.

--
Jim

Jim Mack

unread,
Mar 21, 2009, 8:15:12 PM3/21/09
to
Abhishek wrote:
> I want to produce sound between range of 16000Hz to 20000Hz. can
> your dll do it, how much your dll costs or it can be done thought
> the Win API directly ?

Let's keep commerce out of the group. Drop me a note at the obvious
address and we'll discuss it offline.

> produce sound between 16000Hz to 20000Hz using WinAPI and output to
> speakers. (though humans wont be able to hear it)
> or
> record wav of this frequency and play then in standard way.
>
> can anyone help me out on this?

I'm sure there are examples of how to create a WAV file in VB floating
around. If you don't find one, ask again and I'll send some sample
code. It's a bit wordy for a posting.

Abhishek

unread,
Mar 22, 2009, 3:21:33 AM3/22/09
to
Declare Function Beep Lib "kernel32" Alias "Beep" (ByVal dwFreq As Long,
ByVal dwDuration As Long) As Long

· dwFreq
Windows NT:
Specifies the frequency, in hertz, of the sound. This parameter must be in
the range 37 through 32,767 (0x25 through 0x7FFF).
Windows 95:
The parameter is ignored.

· dwDuration
Windows NT:
Specifies the duration, in milliseconds, of the sound.
Windows 95:
The parameter is ignored.


"Lorin" <Lo...@discussions.microsoft.com> wrote in message
news:C7250338-683C-4139...@microsoft.com...

Michael Williams

unread,
Mar 22, 2009, 5:29:48 AM3/22/09
to
"Jim Mack" <jm...@mdxi.nospam.com> wrote in message
news:%23ly8sKo...@TK2MSFTNGP06.phx.gbl...

> Right -- the test of the whole chain would be to create a sound sample
> at (say) 96K, of a 40khZ tone. If the sound makes it through the
> system (as seen on a scope), there's the answer. But I suspect every
> system is different -- most sound cards are probably spec'd with a
> 20kHz top end.

Quite probably, and most typical speakers as well, so he should be okay for
the frequencies he is talking about (I think he mentioned 16 to 20 KHz). And
of course since the OP only wants the high frequencies for scaring
mosquitoes, who won't particularly be interested in hi fi reproduction (!),
he will probably be able to use considerably more than 20 KHz before the
response drops off to an unusable level. As you've said, he would need to
conduct a proper test using proper measuring equipment to be certain. It's
at times like this that I wish I had kept all the equipment I had many years
ago when my hobby was electronics, but sadly that's all gone now :-( Out of
interest I've just carried out a few tests in which I created and played
sounds at various frequencies (using the typical CD 44.1 KHz sample rate)
and it showed that the frequency response of my own hearing is quite poor. I
knew it would be of course because I am 66 years of age and at that age the
frequency response will have dropped off considerably from my younger days,
but I was surprised that one of my ears was so bad that I could not hear any
sound at all above about 7 KHz (with the other ear being about 11 KHz). I'm
sure most people here will fare better than that? Youngsters of course will
be way above those levels, with some of them possibly even becoming annoyed
by the 16 KHz frequency he mentioned, and mosquitoes will be dancing in the
aisles ;-)

Mike


mayayana

unread,
Mar 23, 2009, 1:17:34 AM3/23/09
to
You really should use a sound. People who won't
hear a sound played will not want to hear a beep.
If you force them to hear it then you're overriding
their choice not to have sounds. (There is a setting
in the Registry to disable beeps, but I don't know
of any accessible setting in the Sounds applet to
control that setting.)


Claus Centrino

unread,
Mar 23, 2009, 8:09:33 AM3/23/09
to
> Youngsters of course will be way above those levels, with
> some of them possibly even becoming annoyed by the 16 KHz
> frequency he mentioned, and mosquitoes will be dancing in
> the aisles ;-)

In GB they use such high frequencies at shopping centers
after closing time to avoid accumulations of youngsters,
other people which don't hear that are not affected and can
still have an undisturbed look into the shop window. So
hardness of hearing sometimes can be advantageous.

Abhishek

unread,
Mar 23, 2009, 10:18:11 AM3/23/09
to
I tried to use this API to generate sound, but it outputs to internal
motherboard speaker, not to the soundcard speaker.


"Abhishek" <m...@server.com> wrote in message
news:OzOyX7rq...@TK2MSFTNGP03.phx.gbl...

Schmidt

unread,
Mar 23, 2009, 12:04:59 PM3/23/09
to

"Abhishek" <m...@server.com> schrieb im Newsbeitrag
news:%239lFvon...@TK2MSFTNGP02.phx.gbl...

> I want to produce sound between range of 16000Hz to 20000Hz.

If you want to take a look at a DirectX8-based example -
here is one:
<http://groups.google.de/group/microsoft.public.vb.winapi/browse_frm/thread/
dcaa418f98906a10/6f57197320819663?q=vb+sss+directsound#6f57197320819663

Just play around with it - and then rip of the pieces you
want (and adapt the frequencies to your needs).

Olaf


Michael Williams

unread,
Mar 23, 2009, 3:53:57 PM3/23/09
to
"Abhishek" <m...@server.com> wrote in message
news:e9ZT0I8q...@TK2MSFTNGP05.phx.gbl...

> I tried to use this API to generate sound [Declare Function
> Beep Lib "kernel32"], but it outputs to internal motherboard


> speaker, not to the soundcard speaker.

Beep is a nuisance and it always seems to go where you do not want it to go!
I seem to recall it going to the motherboard speaker if one is installed and
enabled and to the sound card if it is not, although I'm not entirely sure
on that and I imagine it varies from system to system. It's a "tinny" little
sound anyway, and virtually useless above about 4KHz (as far as I recall).
The best thing to do would be to follow the advice you've already been given
and generate a wav file in code at whatever frequency and duration and
waveform you require and play it using one of thewinmm.dll PlaySound
functions.

Here is some code which will generate and play a sine wave wav file for you
at any desired frequency and duration, subject of course to the limit
imposed by whatever sample rate you use. It took me a while on and off over
the last day or two to write it and I haven't got time at the moment to make
it into a more general purpose function, but you should be able to do that
yourself once you examine the code for yourself. At the moment the routine
is hard coded so as to generate a mono wav at 16 bits per sample and at a
sample rate of 48000 samples per second, which should be good enough for the
up to 20K frequencies you asked for in your earlier posts. There are enough
comments in the code to enable you to fairly easily modify those settings if
you wish to. The calling code just has to specify the desired frequency and
duration. At the moment the code generates a waveform containing sufficient
data to play for the entire duration you have specified, but for repeating
continuous wavefoms such as this (a constant amplitude and frequency sine
wave in this example) you can generate a very much smaller sample if you
wish and use the SND_LOOP flag in the PlaySound function to cause the short
wav to continually repeat itself, adding some extra code to turn it off
after the desired total period has elapsed. I haven't had time to add such
code though, so if you wish to change things so that it works in that way
you will need to add the extra code yourself. With the current settings the
code generates data of just under 100KB per second of sound duration, so if
you use it to produce tones of any greater length than about five seconds
you should begin to think about the looping method I've mentioned.

Anyway, here's the code as it stands so far. Paste it into a VB Form
containing a Command Button. Run the code and click the button. It's "work
in progress" at the moment so if you spot any problems with it then let me
know.

Mike

Option Explicit
Private Declare Function PlaySoundFromBytes _
Lib "winmm.dll" Alias "PlaySoundA" _
(lpBytes As Any, ByVal hModule As Long, _
ByVal dwFlags As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" (Destination As Any, _
Source As Any, ByVal Length As Long)
Private Const SND_APPLICATION As Long = &H80
Private Const SND_ALIAS As Long = &H10000
Private Const SND_ASYNC As Long = &H1
Private Const SND_FILENAME As Long = &H20000
Private Const SND_LOOP As Long = &H8
Private Const SND_MEMORY As Long = &H4
Private Const SND_NODEFAULT As Long = &H2
Private Const SND_NOSTOP As Long = &H10
Private Const SND_PURGE As Long = &H40
Private Const SND_SYNC As Long = &H0
Private wavFile() As Integer

Private Sub Make16BitMonoWav(Frequency As Long, _
Duration As Double)
' wavfile() is a Form level array of Integers (because
' an array inside the Sub would be ditched by VB as
' soon as the Sub ended and whilst the wav was still
' playing). The values here are at the moment hard
' coded to produce a 16 bits per sample wav file
' with a sample rate of 48000 samples per second and
' at a volume level of nearly full volume
Dim SamplesPerSec As Double, samplerate As Double
Dim cycles As Double, pi As Double, k As Double
Dim n As Long, totalsamples As Long, d1 As Long
Dim Amplitude As Integer, dAngle As Double
SamplesPerSec = 48000 ' slightly higher than CD quality
Amplitude = 32600 ' just under full volume
cycles = Round(Frequency * Duration)
totalsamples = Round(SamplesPerSec / Frequency * cycles)
' Note: we may need to add padding bytes to make
' actual wav data a multiple of 4, which means for
' this 16 bit (2 byte) wav we may need and extra
' 2 bytes if totalsamples is an odd number
' (check this later, but make sure that it we do
' need to add pad bytes that we do NOT alter the
' value of the TotalSamples variable because it
' is used in the following code
ReDim wavFile(1 To totalsamples + 22) ' 44 Bytes for header
wavFile(1) = &H4952 ' ) "RIFF"
wavFile(2) = &H4646 ' )
' calculate byte len of total wav file minus 8 bytes
d1 = (totalsamples + 22) * 2 - 8
' and copy the Long result into the two Integers
CopyMemory wavFile(3), d1, 4
wavFile(5) = &H4157 ' ) "WAVE"
wavFile(6) = &H4556 ' )
wavFile(7) = &H6D66 ' ) "fmt "
wavFile(8) = &H2074
d1 = 16 ' chunk size (Long)
CopyMemory wavFile(9), d1, 4 ' write Long into two Integers
wavFile(11) = 1 ' 1 = not compressed
wavFile(12) = 1 ' number of channels (1 for mono)
d1 = SamplesPerSec
CopyMemory wavFile(13), d1, 4
' calculate average bytes per second
' (16 bit mono = samplespersec * 2) (Long)
d1 = SamplesPerSec * 2
CopyMemory wavFile(15), d1, 4
' blockalign (bytes per sample frame)
' (16 bit mono = 2) (Integer)
wavFile(17) = 2
wavFile(18) = 16 ' 16 bits per sample
wavFile(19) = &H6164 ' ) "DATA"
wavFile(20) = &H6174 ' )
d1 = totalsamples * 2
' actual wav data bytes not counting header or any padding
CopyMemory wavFile(21), d1, 4
' generate the actual sine wave data
pi = Atn(1) * 4
dAngle = (cycles * 2 * pi) / totalsamples
For n = 0 To totalsamples - 1
d1 = Amplitude * Sin(k)
wavFile(n + 23) = d1
k = k + dAngle
Next n
' Use the following as an example if you wish
' to save the wav file to disk as a standard
' .wav file
'Dim s1 As String, fn As Long
's1 = "c:\temp\checkwav.wav"
'fn = FreeFile
'Open s1 For Output As fn
'Close fn
'Open s1 For Binary As fn
'Put fn, 1, wavFile()
'Close 1
End Sub

Private Sub Command1_Click()
Make16BitMonoWav 1000, 1 ' 1000 Hz, 1 second
PlaySoundFromBytes wavFile(1), ByVal 0&, SND_MEMORY Or _
SND_ASYNC Or SND_NODEFAULT
End Sub

Private Sub Command2_Click()
' stop wav playing (most useful for stopping
' a wav that is being played using SND_LOOP
PlaySoundFromBytes ByVal 0&, ByVal 0&, _
SND_NODEFAULT Or SND_PURGE
End Sub


Jim Mack

unread,
Mar 23, 2009, 8:20:20 PM3/23/09
to

I decided to polish up a class I've been using internally for a while,
and make it available for free use.

So there's an AX DLL at http://www.mdxi.com/other/ToneGen.zip along
with an example / testbed project.

I've done tons of this kind of stuff, including square waves, triangle
waves, quadrature (90/180/270), cosine-ramp frequency sweeps,
table-driven video-locked generators, etc. This DLL just does sine
waves.

Let me know if you can hear 20 kHz. (-:

Michael Williams

unread,
Mar 24, 2009, 4:18:27 AM3/24/09
to
"Abhishek" <m...@server.com> wrote in message
news:%239lFvon...@TK2MSFTNGP02.phx.gbl...

> I want to produce sound between range of
> 16000Hz to 20000Hz.

I posted two responses yesterday, one from my main machine and one from my
laptop, and neither of them seem to have made it into the group, at least
not as I see it in Windoze Mail, and yet I can see one of them on Google
groups. Weird. Anyway, just in case, here it is again but just the code this
time. Paste it into a Form containing a Command Button:

Mike

Option Explicit
Private Declare Function PlaySoundFromBytes _
Lib "winmm.dll" Alias "PlaySoundA" _
(lpBytes As Any, ByVal hModule As Long, _
ByVal dwFlags As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" (Destination As Any, _
Source As Any, ByVal Length As Long)
Private Const SND_APPLICATION As Long = &H80
Private Const SND_ALIAS As Long = &H10000
Private Const SND_ASYNC As Long = &H1
Private Const SND_FILENAME As Long = &H20000
Private Const SND_LOOP As Long = &H8
Private Const SND_MEMORY As Long = &H4
Private Const SND_NODEFAULT As Long = &H2
Private Const SND_NOSTOP As Long = &H10
Private Const SND_PURGE As Long = &H40
Private Const SND_SYNC As Long = &H0

Private Sub WavBeep(Frequency As Long, _
Duration As Double)
' The values here are at the moment hard
' coded to produce a 16 bits per sample mono
' wav file with a sample rate of 48000 samples
' per second and at nearly full volume level
Dim wavfile() As Integer


Dim SamplesPerSec As Double, samplerate As Double
Dim cycles As Double, pi As Double, k As Double
Dim n As Long, totalsamples As Long, d1 As Long
Dim Amplitude As Integer, dAngle As Double
SamplesPerSec = 48000 ' slightly higher than CD quality
Amplitude = 32600 ' just under full volume
cycles = Round(Frequency * Duration)
totalsamples = Round(SamplesPerSec / Frequency * cycles)
' Note: we may need to add padding bytes to make
' actual wav data a multiple of 4, which means for

' some 16 bit (2 byte) wav we may need and extra


' 2 bytes if totalsamples is an odd number

' (Actually on subsequent checking it appears this
' is not necessary as long as the data is a whole
' number of Integers, which of course it always will
' be at 16 bits per sample, but I may need to check
' this out further).
ReDim wavfile(1 To totalsamples + 22) ' 44 Bytes for header
wavfile(1) = &H4952 ' ) "RIFF"
wavfile(2) = &H4646 ' )


' calculate byte len of total wav file minus 8 bytes
d1 = (totalsamples + 22) * 2 - 8
' and copy the Long result into the two Integers

CopyMemory wavfile(3), d1, 4
wavfile(5) = &H4157 ' ) "WAVE"
wavfile(6) = &H4556 ' )
wavfile(7) = &H6D66 ' ) "fmt "
wavfile(8) = &H2074


d1 = 16 ' chunk size (Long)

CopyMemory wavfile(9), d1, 4 ' write Long to 2 Integers
wavfile(11) = 1 ' 1 = not compressed
wavfile(12) = 1 ' number of channels (1 for mono)
d1 = SamplesPerSec
CopyMemory wavfile(13), d1, 4


' calculate average bytes per second
' (16 bit mono = samplespersec * 2) (Long)
d1 = SamplesPerSec * 2

CopyMemory wavfile(15), d1, 4


' blockalign (bytes per sample frame)
' (16 bit mono = 2) (Integer)

wavfile(17) = 2
wavfile(18) = 16 ' 16 bits per sample
wavfile(19) = &H6164 ' ) "DATA"
wavfile(20) = &H6174 ' )


d1 = totalsamples * 2

' actual wav data bytes not counting header or padding
CopyMemory wavfile(21), d1, 4


' generate the actual sine wave data
pi = Atn(1) * 4
dAngle = (cycles * 2 * pi) / totalsamples
For n = 0 To totalsamples - 1
d1 = Amplitude * Sin(k)

wavfile(n + 23) = d1


k = k + dAngle
Next n

PlaySoundFromBytes wavfile(1), ByVal 0&, _
SND_MEMORY Or SND_ASYNC Or SND_NODEFAULT
DoEvents ' necessary if wavfile() is local to Sub


' Use the following as an example if you wish
' to save the wav file to disk as a standard
' .wav file
'Dim s1 As String, fn As Long
's1 = "c:\temp\checkwav.wav"
'fn = FreeFile
'Open s1 For Output As fn
'Close fn
'Open s1 For Binary As fn
'Put fn, 1, wavFile()
'Close 1
End Sub

Private Sub Command1_Click()
WavBeep 1000, 1
End Sub

Michael Williams

unread,
Mar 24, 2009, 8:45:24 AM3/24/09
to
"Abhishek" <m...@server.com> wrote in message
news:%239lFvon...@TK2MSFTNGP02.phx.gbl...

> I want to produce sound between range of 16000Hz
> to 20000Hz . . .

OK, since my small test message got through recently I'll have one last go
at posting the response I've already posted three times since yesterday in
the hope that this one will get through :-( Here it is again, just the
code. For comments regarding looping a small wav file when a very long
continuous waveform is needed see my previous posts, if they ever get
through. In the meantime the following code will produce and play a sine
wave at whatever frequency and for whatever duration you require, up to a
maximum usable frequency of 20000 Hz. If you want a higher frequency than
that, and if your sound card and speakers will handle a higher frequency,
then just change the SamplesPerSec = 48000 in the following code to
something else, perhaps SamplesPerSec = 96000. Anyway, here's the code.

Paste it into a Form containing a Command Button:

Option Explicit

Rick Raisley

unread,
Mar 24, 2009, 8:54:15 AM3/24/09
to
"Jim Mack" <jm...@mdxi.nospam.com> wrote in message
news:uZCbSZBr...@TK2MSFTNGP05.phx.gbl...

And if we can't, then that means it's working, right? ;-)

I can't hear that, and a 30-ish girl around me at work can hear it, but very
softly. But I annoyed the hell out of her with 16 kHz, which basically I
can't hear. lol!

Anyhow, very nice. I like the different frequencies to the left and right,
so you can get a "throbbing" when they're close, but not the same. I can use
this, I'm sure. Thanks again! ;-)

--
Regards,

Rick Raisley
heavymetal-A-T-bellsouth-D-O-T-net


Rick Raisley

unread,
Mar 24, 2009, 9:38:26 AM3/24/09
to
Jim, I notice when using your code that calling the sound does not wait
until it is done playing. I looped together some rising sounds, each 200 ms
long, expecting to hear a steadily increasing frequency. But it kind of
muddled together. Apparently, calling a 200 ms (or 1000 ms, whatever) sound
returns to the program immediately, while the sound continues to play. Now,
that's not unexpected, but it means you can't string several play commands
together, and have it sound like a "song".

I could add a Sleep command, to delay the duration of the note, but my guess
is that it will result in no sound playing for a brief period. Still, that
might be fine.

Is there anything within the DLL that could optionally keep code from
returning to the calling routine until the sound is done playing?

--
Regards,

Rick Raisley
heavymetal-A-T-bellsouth-D-O-T-net

"Jim Mack" <jm...@mdxi.nospam.com> wrote in message
news:uZCbSZBr...@TK2MSFTNGP05.phx.gbl...

Jim Mack

unread,
Mar 24, 2009, 11:11:43 AM3/24/09
to
Rick Raisley wrote:
> Jim, I notice when using your code that calling the sound does not
> wait until it is done playing. I looped together some rising
> sounds, each 200 ms long, expecting to hear a steadily increasing
> frequency. But it kind of muddled together. Apparently, calling a
> 200 ms (or 1000 ms, whatever) sound returns to the program
> immediately, while the sound continues to play. Now, that's not
> unexpected, but it means you can't string several play commands
> together, and have it sound like a "song".
>
> I could add a Sleep command, to delay the duration of the note, but
> my guess is that it will result in no sound playing for a brief
> period. Still, that might be fine.
>
> Is there anything within the DLL that could optionally keep code
> from returning to the calling routine until the sound is done
> playing?

Internally, the DLL uses PlaySound with the SND_ASNYC flag, which is
normally what you want. I could add an option to play sync or async,
but it's just as easy for you to do it. The .Play method is mainly a
fillip - the real power is in the .Generate method.

First, Declare PlaySound yourself, and the SND_MEMORY flag.

Then, use the DLL to .Generate each sound, and save the returned
buffers in local byte arrays. See the file save example code for
inspiration.

When you want to play a series of sounds, make your own calls to
PlaySound using the stored buffers in any sequence. You can use a
Variant array to hold a series of byte arrays, BTW.

Also, I believe PlaySound works just as well with a series of
concatenated WAV images. If so you could just string out a set of
.SoundBuffers into one big array.

Private Const SND_ASYNC As Long = 1 ' leave this out
Private Const SND_MEMORY As Long = 4

Private Declare Function PlaySound Lib "winmm" _
Alias "PlaySoundA" (ByRef snd As Byte, _
ByVal hFil As Long, ByVal flags As Long) As Long

PlaySound bytBuffer(0), 0, SND_MEMORY 'Or SND_ASYNC

Larry Serflaten

unread,
Mar 24, 2009, 12:21:45 PM3/24/09
to

"Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote

> "Jim Mack" wrote:
> > I decided to polish up a class I've been using internally for a while,
> > and make it available for free use.

> Is there anything within the DLL that could optionally keep code from


> returning to the calling routine until the sound is done playing?

It also would have been nice to have Object Browser comments for
the different properties. Limits, acceptable values, explainations,
that sort of thing, right there in the Object Browser....

:-)
LFS


Abhishek

unread,
Mar 24, 2009, 12:38:34 PM3/24/09
to
thanks a lot for the code :P

and as far as my tests are concerned. I can hear 16 kHz but not 20 kHz. I am
24 yrs old.

any possibility to get source code of that dll ?

thanks,


"Jim Mack" <jm...@mdxi.nospam.com> wrote in message
news:uZCbSZBr...@TK2MSFTNGP05.phx.gbl...

Jim Mack

unread,
Mar 24, 2009, 1:04:10 PM3/24/09
to

Yeah, and some chocolate would be nice. (-:

OK, v1.0.0.1 coming up later today, with TLB adds.

--
Jim

Jim Mack

unread,
Mar 24, 2009, 1:37:34 PM3/24/09
to
Larry Serflaten wrote:

> It also would have been nice to have Object Browser comments for
> the different properties. Limits, acceptable values, explainations,
> that sort of thing, right there in the Object Browser....

Done, revision in place. Let me know if it's enough. (-:

--
Jim

Larry Serflaten

unread,
Mar 24, 2009, 7:00:12 PM3/24/09
to

"Jim Mack" <jm...@mdxi.nospam.com> wrote

Yes it is a big help to have info right there at your fingertips....

Thanks.
LFS


Rick Raisley

unread,
Mar 25, 2009, 10:55:16 AM3/25/09
to
Thanks very much, Jim. I'll mention a cute experience I had using your
program yesterday:

I was running various frequencies, seeing (okay, hearing) just how well this
old man can hear. I was find through 13 kHz, but then the volume started
going down. By turning the volume way up, I could just barely hear 15 kHz,
so was playing with that and 16 kHz, which I could NOT hear. Just then, a
young girl in our office walked by with both her hands covering her ears,
exclaiming "WHAT'S THAT NOISE!!??" ;-)

Guess my ears aren't the only thing that's old. lol!

--
Regards,

Rick Raisley
heavymetal-A-T-bellsouth-D-O-T-net

"Jim Mack" <jm...@mdxi.nospam.com> wrote in message

news:%23yQu4cK...@TK2MSFTNGP04.phx.gbl...

Nobody

unread,
Mar 25, 2009, 12:18:28 PM3/25/09
to
"Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
news:%23UKA3mV...@TK2MSFTNGP05.phx.gbl...

> Thanks very much, Jim. I'll mention a cute experience I had using your
> program yesterday:
>
> I was running various frequencies, seeing (okay, hearing) just how well
> this old man can hear. I was find through 13 kHz, but then the volume
> started going down. By turning the volume way up, I could just barely hear
> 15 kHz, so was playing with that and 16 kHz, which I could NOT hear. Just
> then, a young girl in our office walked by with both her hands covering
> her ears, exclaiming "WHAT'S THAT NOISE!!??" ;-)

You could use Audacity which is a free audio editor. It has a Generate menu
to generate any frequency in various shapes.

http://en.wikipedia.org/wiki/Audacity
http://audacity.sourceforge.net/


Michael Williams

unread,
Mar 25, 2009, 2:25:39 PM3/25/09
to
"Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
news:%23UKA3mV...@TK2MSFTNGP05.phx.gbl...

> I was running various frequencies, seeing (okay, hearing) just
> how well this old man can hear. I was fine through 13 kHz,


> but then the volume started going down. By turning the volume
> way up, I could just barely hear 15 kHz, so was playing with
> that and 16 kHz, which I could NOT hear. Just then, a young
> girl in our office walked by with both her hands covering her
> ears, exclaiming "WHAT'S THAT NOISE!!??" ;-)
> Guess my ears aren't the only thing that's old. lol!

You think that's bad? I can hear up to 11 kHz in one ear and only 8 kHz in
the other!

Mike


Rick Raisley

unread,
Mar 31, 2009, 11:27:38 AM3/31/09
to
Jim, is there a limit to the files that can be saved, as to sampling
frequency or sound frequency? I made a 16,000/16,500 sound using a high
sampling rate, which sounded extremely annoying to the local young female,
then saved it as a WAV file. But all I get is basically static, when playing
the WAV file.

Any idea why?

--
Regards,

Rick Raisley
heavymetal-A-T-bellsouth-D-O-T-net

"Jim Mack" <jm...@mdxi.nospam.com> wrote in message

news:eBsoYLJr...@TK2MSFTNGP06.phx.gbl...

Michael Williams

unread,
Mar 31, 2009, 12:46:59 PM3/31/09
to
"Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
news:u9215Uhs...@TK2MSFTNGP02.phx.gbl...

> Jim, is there a limit to the files that can be saved, as to sampling
> frequency or sound frequency? I made a 16,000/16,500 sound
> using a high sampling rate, which sounded extremely annoying to
> the local young female, then saved it as a WAV file. But all I get
> is basically static, when playing the WAV file. Any idea why?

I haven't tried Jim's code but I don't expect him to have come up against
any limits. Are you sure that you are not saving just the raw wav data
instead of a proper wav file with the appropriate header? That's what it
sounds like to me (no pun intended!).

In the meantime, did you ever try the code I posted early on in this thread,
maybe a couple of weeks ago now? That code included an example of how to
save the wav file it produced. It may be that you never saw my response
because I tried to post it about a dozen times over a number of days and it
kept getting rejected by the Micro$oft news servers. The problem was
eventually pinned down to a specific variable name I had used, which for
some reason was triggering some automatic message filter mechanism. Out of
interest, the variable name was a very innocent sounding
S_a_m_p_l_e_s_P_e_r_S_e_c(without the underscores of course, which I have
used here otherwise this post would be rejected as well!).

Anyway here is the code again, this time with that specific variable named
replaced by something else. To try it out just paste it into a VB Form
containing a Command Button. Change the hard coded .wav file path
("c:\temp\testwav.wav") to whatever file name you require.

Mike

Option Explicit
Private Declare Function PlaySoundFromBytes _
Lib "winmm.dll" Alias "PlaySoundA" _
(lpBytes As Any, ByVal hModule As Long, _
ByVal dwFlags As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" (Destination As Any, _
Source As Any, ByVal Length As Long)

Private Declare Sub Sleep Lib "kernel32" _
(ByVal dwMilliseconds As Long)


Private Const SND_APPLICATION As Long = &H80
Private Const SND_ALIAS As Long = &H10000
Private Const SND_ASYNC As Long = &H1
Private Const SND_FILENAME As Long = &H20000
Private Const SND_LOOP As Long = &H8
Private Const SND_MEMORY As Long = &H4
Private Const SND_NODEFAULT As Long = &H2
Private Const SND_NOSTOP As Long = &H10
Private Const SND_PURGE As Long = &H40
Private Const SND_SYNC As Long = &H0

Private Sub WavBeep(Frequency As Long, _

Duration As Double, Optional wait As Boolean = False)


' The values here are at the moment hard
' coded to produce a 16 bits per sample mono
' wav file with a sample rate of 48000 samples
' per second and at nearly full volume level

Static wavfile() As Integer, Flags As Long
Dim SampleFrequency As Double, samplerate As Double


Dim cycles As Double, pi As Double, k As Double
Dim n As Long, totalsamples As Long, d1 As Long
Dim Amplitude As Integer, dAngle As Double

SampleFrequency = 48000 ' slightly higher than CD quality


Amplitude = 32600 ' just under full volume
cycles = Round(Frequency * Duration)

totalsamples = Round(SampleFrequency / Frequency * cycles)


' Note: we may need to add padding bytes to make
' actual wav data a multiple of 4, which means for
' some 16 bit (2 byte) wav we may need and extra
' 2 bytes if totalsamples is an odd number
' (Actually on subsequent checking it appears this
' is not necessary as long as the data is a whole
' number of Integers, which of course it always will
' be at 16 bits per sample, but I may need to check
' this out further).
ReDim wavfile(1 To totalsamples + 22) ' 44 Bytes for header
wavfile(1) = &H4952 ' ) "RIFF"
wavfile(2) = &H4646 ' )
' calculate byte len of total wav file minus 8 bytes
d1 = (totalsamples + 22) * 2 - 8
' and copy the Long result into the two Integers
CopyMemory wavfile(3), d1, 4
wavfile(5) = &H4157 ' ) "WAVE"
wavfile(6) = &H4556 ' )
wavfile(7) = &H6D66 ' ) "fmt "
wavfile(8) = &H2074
d1 = 16 ' chunk size (Long)
CopyMemory wavfile(9), d1, 4 ' write Long to 2 Integers
wavfile(11) = 1 ' 1 = not compressed
wavfile(12) = 1 ' number of channels (1 for mono)

d1 = SampleFrequency


CopyMemory wavfile(13), d1, 4
' calculate average bytes per second

' (16 bit mono = SampleFrequency * 2) (Long)
d1 = SampleFrequency * 2


CopyMemory wavfile(15), d1, 4
' blockalign (bytes per sample frame)
' (16 bit mono = 2) (Integer)
wavfile(17) = 2
wavfile(18) = 16 ' 16 bits per sample
wavfile(19) = &H6164 ' ) "DATA"
wavfile(20) = &H6174 ' )
d1 = totalsamples * 2
' actual wav data bytes not counting header or padding
CopyMemory wavfile(21), d1, 4
' generate the actual sine wave data
pi = Atn(1) * 4
dAngle = (cycles * 2 * pi) / totalsamples
For n = 0 To totalsamples - 1
d1 = Amplitude * Sin(k)
wavfile(n + 23) = d1
k = k + dAngle
Next n

Flags = SND_MEMORY Or SND_NODEFAULT
If wait = True Then
Flags = Flags Or SND_SYNC
Else
Flags = Flags Or SND_ASYNC
End If
PlaySoundFromBytes wavfile(1), ByVal 0&, Flags
Sleep 10


' Use the following as an example if you wish
' to save the wav file to disk as a standard
' .wav file
Dim s1 As String, fn As Long

s1 = "c:\temp\testwav.wav"


fn = FreeFile
Open s1 For Output As fn
Close fn
Open s1 For Binary As fn

Put fn, 1, wavfile()
Close 1
'
End Sub

Private Sub Command1_Click()
' frequency (Long)
' duration (Double)
' Wait until finished (Boolean)
WavBeep 1000, 1, False
End Sub


Jim Mack

unread,
Mar 31, 2009, 1:03:01 PM3/31/09
to
Rick Raisley wrote:
> Jim, is there a limit to the files that can be saved, as to sampling
> frequency or sound frequency? I made a 16,000/16,500 sound using a
> high sampling rate, which sounded extremely annoying to the local
> young female, then saved it as a WAV file. But all I get is
> basically static, when playing the WAV file.
>
> Any idea why?

None. If it plays with PlaySound, it should save correctly as a WAV
file -- it's exactly the same image.

What sample rate? I'd use 44100 in this case -- anything higher is
wasted.

Are you using any EQ in the WAV player? EQ can really mess with
digitized sound.

If you like, put the file in a ZIP / RAR and email it to the obvious
address.

--
Jim

Rick Raisley

unread,
Apr 1, 2009, 8:00:34 AM4/1/09
to
"Jim Mack" <jm...@mdxi.nospam.com> wrote in message
news:u3fOOKis...@TK2MSFTNGP06.phx.gbl...

> Rick Raisley wrote:
>> Jim, is there a limit to the files that can be saved, as to sampling
>> frequency or sound frequency? I made a 16,000/16,500 sound using a
>> high sampling rate, which sounded extremely annoying to the local
>> young female, then saved it as a WAV file. But all I get is
>> basically static, when playing the WAV file.
>>
>> Any idea why?
>
> None. If it plays with PlaySound, it should save correctly as a WAV
> file -- it's exactly the same image.
>
> What sample rate? I'd use 44100 in this case -- anything higher is
> wasted.
>
> Are you using any EQ in the WAV player? EQ can really mess with
> digitized sound.
>

Yeah, turns out while the EQ was not turned on, the SRS WOW (???) effects
were. Turning them off eliminated the static, and seems to play correctly.

Especially since after playing the 15kHz sound, the 27 year old girl nearby
yelled "Rick, stop pressing the button!" ;-)

Larry Serflaten

unread,
Apr 13, 2009, 10:35:18 AM4/13/09
to

"Jim Mack" <jm...@mdxi.nospam.com> wrote

> I decided to polish up a class I've been using internally for a while,
> and make it available for free use.
>
> So there's an AX DLL at http://www.mdxi.com/other/ToneGen.zip along
> with an example / testbed project.
>
> I've done tons of this kind of stuff, including square waves, triangle
> waves, quadrature (90/180/270), cosine-ramp frequency sweeps,
> table-driven video-locked generators, etc. This DLL just does sine
> waves.


I haven't done that sort of stuff before, so it sounded like a fun project.

Generating an oscillating value sounded easy enough, but what if you
wanted to vary the amplitude (modulation), or the frequency? Lots of
sounds use that sort of thing so I added that capability and found I
could do Lissajou patterns! A demo is available free for download:
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=71979&lngWId=1

Have fun!
LFS


Jim Mack

unread,
Apr 13, 2009, 3:57:18 PM4/13/09
to
Larry Serflaten wrote:
> "Jim Mack" wrote...

>>
>> I've done tons of this kind of stuff, including square waves,
>> triangle waves, quadrature (90/180/270), cosine-ramp frequency
>> sweeps, table-driven video-locked generators, etc. This DLL just
>> does sine waves.
>
>
> I haven't done that sort of stuff before, so it sounded like a fun
> project.
>
> Generating an oscillating value sounded easy enough, but what if you
> wanted to vary the amplitude (modulation), or the frequency? Lots
> of
> sounds use that sort of thing so I added that capability and found I
> could do Lissajou patterns! A demo is available free for download:
>
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=71979&lngWId=1

Wow, it looks like you had a lot of fun. Nice going.

0 new messages