On Jan 5, 2:13 pm, "Mike Williams" <***@WhiskyAndCoke.com> wrote:
> "Peter Nolan" <***@gmail.com> wrote in message
>
> news:7b72a5b5-5662-4632-b3b9-***@z12g2000yqm.googlegroups.com...
>
> > If you can help out a little more that would be great . . .
>
> Here's another slightly different example in which I've used an Integer
> array instead of a String and where I am generating the entire required
> length of wav file data rather than generating a small 0.1 second wav and
> playing it in a timed loop as did my previous example. This version will of
> course allow you to then save the full length wav file to disk if you wish
> to do so (see remarks in the code). Paste the code into a VB 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_ASYNC As Long = &H1
> Private Const SND_MEMORY As Long = &H4
> Private Const SND_NODEFAULT As Long = &H2
> Private Const SND_SYNC As Long = &H0
> Private Const SND_PURGE = &H40
>
> Private Sub WavBeep(Frequency As Long, _
> Duration As Double, Optional wait As Boolean = False)
> Static wavfile() As Integer, Flags As Long
> 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 = 44100
> Amplitude = 32600 ' just under full volume
> Duration = Duration / 1000 ' change to seconds
> cycles = Round(Frequency * Duration)
> If cycles < 1 Then cycles = 1
> 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
> 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
> ' Use the following commented out block of code
> ' 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\testwav1.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 400, 500 ' 400 Hz for 500 milliseconds
> 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_PURGE Or SND_NODEFAULT
> End Sub
>
> Private Sub Form_Unload(Cancel As Integer)
> PlaySoundFromBytes ByVal 0&, ByVal 0&, _
> SND_PURGE Or SND_NODEFAULT
> End Sub
Hello Mike,
I want to thank you for the two fandabadozi blocks of code you have
sent me. I'm indebted to you. I will now implement the code and study
it till I understand what each line actually does. I'm a physicist by
profession so the technical or mathematical aspects of the code to do
with sine functions is readily understandable to me in addition to my
being able to understand A/D to conversion and it's converse D/A
conversion so you will be happy to know that while I'm not an ace
coder like you are I am not totally clueless about what is going on
here either. :)
I want to tell you what I'm trying to do. I have mild depression as
distinct from clinical depression and while mild depression isn't too
serious it is nevertheless debilitating enough and I earnestly wish
that I had an overall brighter outlook on life. This is a bit of a
long story but because I've probably lost you already having just told
you what I have and so I won't go into all the ins and outs of how I
imagine I thought I have might have a possible treatment for mild
depression especially and probably only in the case of reactive
depression as distinct from indigenous depression that is caused by
real physical upset to the brain/body system because it's always so
important to factor in that component that is the body part or role
played by the whole body.
Many years ago, me thinking I don't want to lose you in all this, I
heard that depression is anger turned inwards. I think this is a
brilliant insight. Now, Mike, imagine looking at the computer screen
as you are doing now but imagine never being able to change that
perspective. Imagine having that fixed outlook that is staring forever
at a computer screen and never be able to switch off the screen and
oneself and say to then enjoy everything else that is going on around
one in life noting the phrase "around one". That is a sort of away to
think about depression that is to be forever fixed in one's outlook
peering straight ahead never being able to take the more panoramic
viewpoint that any normal person should be able to hold during the
very varied day that it's possible for many to live.
Now take all that into account and consider the possibility that it
might be possible to induce in someone suffering from depression that
feeling of being able to look around one three hundred and sixty
degree style.
So this is my idea. Years ago 2001 I designed and built a little
pacemaker for runners that simply emits a beep at some settable rate
according to how fast the runner wants to run. Don't get the wrong
idea that I am a whizz electronic design engineer like the fellas in
the newsgroup sci.design.electronics Some of those guys are just
amazing just like you are at coding. Just amazing! I had high hopes to
make some money here but it all came to nothing when it turns out a
company called Garmin make such a device using GPS and simply worn on
the wrist. No way could I compete with them.
However during the last week or two because this project of mine is
only in it's infancy I would sit there comfortably and place the
beeping pacemaker behind my back behind where I'm sitting in the chair
close my eyes and try to focus entirely on the beep beeping at a rate
of about one beep per second. I gotta say I really have noticed a
difference in how I feel even after only say six sessions with each
session lasting say half an hour. You see now immediately why I wanted
the PC to do all this that that little hardware based beeper does. I
got really excited about being able to just choose at will any
frequency for the beep in the PC's speakers and so on and I'll bet you
can relate to how disappointed I felt when it didn't work on my PC.
So I'm trying to train my mind to have a broader outlook by for
example just sitting there for half an hour each day listening to a
beep being emitted right there, not in front of me where my focus
always is, but behind my back. I don't blame you if this doesn't make
the slightest bit of sense to you as a guy who is exceedingly bright
but maybe I'm onto something here that might just give relief. When I
implement the code on my PC I will be able to see much more clearly
any potential this very novel approach might have. I think at least
that this is worth exploring and if it doesn't work and I think it
might already have done just that I will only have lost a bit of time
and I will have made contact with a fantastic coder called Mike
Williams and that's a good thing. That's a great thing.
It will take me a little time to implement the code so please bear
with me. I will let you know how things pan out through
comp.lang.basic.visual.misc.
With regard to posting to NG's I can tell you I use a Mac iBook and I
was able to use MT-Newswatcher on this machine. However there was a
hiccup of some sort when I tried to post to this NG this time round
and so I had to go through google. If spam starts coming in I
discovered I can just delete the gmail address and so long as no one
knows my other email address from my ISP all should be well.
Best Wishes,
Peter Nolan. Ph.D.
33 Templeville Rd.
Templeogue
Dublin 6W