Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.basic.visual.misc > #620

Re: The Beep Function.

From "Mike Williams" <Mike@WhiskyAndCoke.com>
Newsgroups comp.lang.basic.visual.misc
Subject Re: The Beep Function.
Date 2012-01-08 11:19 +0000
Organization A noiseless patient Spider
Message-ID <jebu28$brf$1@dont-email.me> (permalink)
References <491f49f4-a678-4420-b0e4-935b398adc7b@a40g2000vbu.googlegroups.com> <je1tvg$hot$1@dont-email.me> <7b72a5b5-5662-4632-b3b9-9f45a985c9fd@z12g2000yqm.googlegroups.com> <je4b50$jf3$1@dont-email.me> <7b75dfb9-f79d-4fd1-a1de-a966b58cbc36@v13g2000yqc.googlegroups.com>

Show all headers | View raw


"Peter Nolan" <peter.nolan40@gmail.com> wrote in message 
news:7b75dfb9-f79d-4fd1-a1de-a966b58cbc36@v13g2000yqc.googlegroups.com...

> Hello Mike. Both blocks of code you gave me worked
> first time. I will now implement the code and study it till
> I understand what each line actually does.

You're welcome. There is one thing I'd like to point out though and that is 
the fact that both blocks of code were written in such a way that the main 
loop which generates the sample data for the waveform does it a fairly 
simple way (by that I mean the easiest way to understand), and not in the 
fastest way. You won't notice this for most of the wav files you generate 
(your short one second or so wav files) but you will definitely notice it if 
you decide, for example, to generate a wav file lasting one minute, in which 
case even when run as a compiled exe the code would take about a quarter of 
a second (depending on the speed of your machine) to generate the one minute 
wav file. So if you were generating the wav file from scratch (rather than 
playing a previously generated wav file) there would be a noticeable delay 
between pressing the button and hearing the sound. To see what I mean, have 
a look at the main loop in the second example, which is as follows:

    For n = 0 To totalsamples - 1
        d1 = Amplitude * Sin(k)
        wavfile(n + 23) = d1
        k = k + dAngle
    Next n

The value of totalsamples represents the total number of voltage samples in 
the entire wav file being generated, and it uses the relatively slow Sin 
function on variables of the Double data type to calculate each one of them 
(and there are also some relatively slow "under the hood" conversions from 
integer to floating point going on, because d1 is currently a Long and the 
result of Amplitude * Sin(k) is a Double). For a wav file that is one minute 
long and that uses a sample rate of 44100 samples per second the above loop 
would need to generate something approaching three million samples, which is 
why it takes about a quarter of a second to generate the data (on my own 
machine). For the time being you might like to leave it that way, but if you 
ever get to the stage where you need to use it to generate long wav files 
then you might like to look at ways of speeding it up.

There are all sorts ways of performing the task in a faster way. For example 
with your own background you will almost certainly realise that when 
generating a continuous tone where each cycle is mathematically described, 
such as a sine wave, you really only need to generate a block of cycles of 
such length that when you join a large number of those blocks together (in 
order to create the full wav file) there will not be a "glitch" between the 
end of one block and the start of the next block. Since the first block 
starts at a zero voltage level and is rising then you just need to ensure 
the length of the block is such that it ends at a point where the final 
sample of the block is also at (or at least very close to) a rising zero 
crossing point. The cycle length of this block of course will depend on the 
relationship between the sample rate (44100 Hz CD rate in this case) and on 
the frequency of the tone you are generating, but it can readily be 
calculated, and the block will in virtually all cases be extremely small 
compared to the overall length of the entire wav file data. You can then 
write a bit more code to simply "stitch lots of these small blocks together" 
to produce the total wav file. This will be orders of magnitude faster for 
long wav files.

Another way would be to continue to use the existing "generate all the 
possibly millions of samples for the whole wav file" method but to modify 
the code so that it can readily use a lookup table rather than using the Sin 
function, although you would still be using floating point and you would 
still have either explicit or "under the hood" relatively slow floating 
point to integer conversions, so it would probably only be two or three 
times faster. You can carry this technique further though by modifying the 
code so that it both uses a lookup table and also uses only integers in the 
loop (this would require more significant modification). This method, even 
though it still generates all the possibly millions of samples, would be 
very much faster than the existing method, probaby an order of magnitude 
faster. In fact although the first suggestion would be the fastest I think I 
would prefer to use this method myself on the grounds that it would be 
readily suitable for producing wav files in which the waveform required is 
more complex than a simple Sine wave, one with perhaps lots of harmonics.

Don't get me wrong, I am not by any means an expert in wav file generation 
or in musical sounds in general. In fact what I know about thse things can 
be written  on the back of a postage stamp (!) but I do have a passing 
interest in such stuff. I was actually going to address some of the other 
points in the rest of your message at this point, but I think I've waffled 
on enough already!

Mike


Back to comp.lang.basic.visual.misc | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

The Beep Function. Peter Nolan <peter.nolan40@gmail.com> - 2012-01-04 06:15 -0800
  Re: The Beep Function. Peter Nolan <peter.nolan40@gmail.com> - 2012-01-04 07:11 -0800
  Re: The Beep Function. "Mike Williams" <Mike@WhiskyAndCoke.com> - 2012-01-04 15:38 +0000
  Re: The Beep Function. "Mike Williams" <Mike@WhiskyAndCoke.com> - 2012-01-04 16:16 +0000
    Re: The Beep Function. Peter Nolan <peter.nolan40@gmail.com> - 2012-01-04 10:15 -0800
      Re: The Beep Function. Jim Mack <no-uce-ube@mdxi.com> - 2012-01-04 13:46 -0500
        Re: The Beep Function. Peter Nolan <peter.nolan40@gmail.com> - 2012-01-05 08:09 -0800
      Re: The Beep Function. "Mike Williams" <Mike@WhiskyAndCoke.com> - 2012-01-04 22:24 +0000
      Re: The Beep Function. "Mike Williams" <Mike@WhiskyAndCoke.com> - 2012-01-05 08:22 +0000
      Re: The Beep Function. "Mike Williams" <Mike@WhiskyAndCoke.com> - 2012-01-05 09:21 +0000
        Re: The Beep Function. Gordon Levi <gordon@address.invalid> - 2012-01-05 21:54 +1100
          Re: The Beep Function. "Mike Williams" <Mike@WhiskyAndCoke.com> - 2012-01-05 11:33 +0000
            Re: The Beep Function. Gordon Levi <gordon@address.invalid> - 2012-01-06 00:20 +1100
              Re: The Beep Function. "Mike Williams" <Mike@WhiskyAndCoke.com> - 2012-01-05 17:46 +0000
                Re: The Beep Function. "blank" <blank@blankety.blank.com> - 2012-01-06 14:03 +1100
                Re: The Beep Function. Gordon Levi <gordon@address.invalid> - 2012-01-08 00:43 +1100
                Re: The Beep Function. "Mike Williams" <Mike@WhiskyAndCoke.com> - 2012-01-07 20:53 +0000
                Re: The Beep Function. Gordon Levi <gordon@address.invalid> - 2012-01-08 17:27 +1100
                Re: The Beep Function. "Mike Williams" <Mike@WhiskyAndCoke.com> - 2012-01-08 09:49 +0000
                Re: The Beep Function. "blank" <blank@blankety.blank.com> - 2012-01-08 08:00 +1100
                Re: The Beep Function. "Mike Williams" <Mike@WhiskyAndCoke.com> - 2012-01-07 22:43 +0000
      Re: The Beep Function. "Mike Williams" <Mike@WhiskyAndCoke.com> - 2012-01-05 14:13 +0000
        Re: The Beep Function. Peter Nolan <peter.nolan40@gmail.com> - 2012-01-05 08:26 -0800
        Re: The Beep Function. Peter Nolan <peter.nolan40@gmail.com> - 2012-01-05 08:04 -0800
        Re: The Beep Function. Peter Nolan <peter.nolan40@gmail.com> - 2012-01-07 04:41 -0800
          Re: The Beep Function. "Mike Williams" <Mike@WhiskyAndCoke.com> - 2012-01-08 11:19 +0000
        Re: The Beep Function. Peter Nolan <peter.nolan40@gmail.com> - 2012-01-10 04:52 -0800
          Re: The Beep Function. "Thorsten Albers" <gudea@gmx.de> - 2012-01-10 13:01 +0000
          Re: The Beep Function. Jim Mack <no-uce-ube@mdxi.com> - 2012-01-10 11:44 -0500
            Re: The Beep Function. Peter Nolan <peter.nolan40@gmail.com> - 2012-01-11 05:58 -0800
          Re: The Beep Function. "Mike Williams" <Mike@WhiskyAndCoke.com> - 2012-01-10 17:19 +0000
            Re: The Beep Function. Peter Nolan <peter.nolan40@gmail.com> - 2012-01-11 05:54 -0800
              Re: The Beep Function. "DaveO" <djo@dial.pipex.com> - 2012-01-11 15:21 +0000
                Re: The Beep Function. Peter Nolan <peter.nolan40@gmail.com> - 2012-01-12 08:04 -0800
                Re: The Beep Function. "blank" <blank@blankety.blank.com> - 2012-01-13 12:40 +1100
                Re: The Beep Function. ralph <nt_consulting64@yahoo.net> - 2012-01-12 21:28 -0600
                Re: The Beep Function. "blank" <blank@blankety.blank.com> - 2012-01-13 15:34 +1100
                Re: The Beep Function. Peter Nolan <peter.nolan40@gmail.com> - 2012-01-13 05:25 -0800
                Re: The Beep Function. "blank" <blank@blankety.blank.com> - 2012-01-14 07:32 +1100
                Re: The Beep Function. "DaveO" <djo@dial.pipex.com> - 2012-01-13 11:06 +0000
                Re: The Beep Function. Peter Nolan <peter.nolan40@gmail.com> - 2012-01-13 05:48 -0800
                Re: The Beep Function. "DaveO" <djo@dial.pipex.com> - 2012-01-13 15:09 +0000
                Re: The Beep Function. Peter Nolan <peter.nolan40@gmail.com> - 2012-01-14 04:29 -0800
                Re: The Beep Function. Helmut_Meukel <Helmut_Meukel@bn-hof.invalid> - 2012-01-14 14:56 +0100
                Re: The Beep Function. ralph <nt_consulting64@yahoo.net> - 2012-01-14 10:24 -0600
                Re: The Beep Function. Helmut_Meukel <Helmut_Meukel@bn-hof.invalid> - 2012-01-14 18:51 +0100
                Re: The Beep Function. Peter Nolan <peter.nolan40@gmail.com> - 2012-01-15 03:39 -0800
                Re: The Beep Function. Schmidt <sss@online.de> - 2012-01-15 17:39 +0100
                Re: The Beep Function. Peter Nolan <peter.nolan40@gmail.com> - 2012-01-16 05:21 -0800
                Re: The Beep Function. "DaveO" <djo@dial.pipex.com> - 2012-01-16 14:39 +0000
                Re: The Beep Function. Peter Nolan <peter.nolan40@gmail.com> - 2012-01-17 04:13 -0800
                Re: The Beep Function. "DaveO" <djo@dial.pipex.com> - 2012-01-17 14:17 +0000
                Re: The Beep Function. Peter Nolan <peter.nolan40@gmail.com> - 2012-01-17 07:22 -0800
                Re: The Beep Function. "Henning" <computer_hero@coldmail.com> - 2012-01-17 17:47 +0100
                Re: The Beep Function. "DaveO" <djo@dial.pipex.com> - 2012-01-17 16:50 +0000
                Re: The Beep Function. "Henning" <computer_hero@coldmail.com> - 2012-01-17 18:22 +0100
                Re: The Beep Function. Peter Nolan <peter.nolan40@gmail.com> - 2012-01-18 05:26 -0800
                Re: The Beep Function. "DaveO" <djo@dial.pipex.com> - 2012-01-18 14:35 +0000
                Re: The Beep Function. Peter Nolan <peter.nolan40@gmail.com> - 2012-01-18 07:10 -0800
                Re: The Beep Function. ralph <nt_consulting64@yahoo.net> - 2012-01-18 10:38 -0600
                Re: The Beep Function. "blank" <blank@blankety.blank.com> - 2012-01-19 05:42 +1100
                Re: The Beep Function. "DaveO" <djo@dial.pipex.com> - 2012-01-19 11:33 +0000
                Re: The Beep Function. Peter Nolan <peter.nolan40@gmail.com> - 2012-01-21 03:16 -0800
                Re: The Beep Function. Peter Nolan <peter.nolan40@gmail.com> - 2012-01-19 05:17 -0800
                Re: The Beep Function. "blank" <blank@blankety.blank.com> - 2012-01-20 05:17 +1100
                Re: The Beep Function. Peter Nolan <peter.nolan40@gmail.com> - 2012-01-20 03:54 -0800
                Re: The Beep Function. Jason Keats <jkeats@melbpcDeleteThis.org.au> - 2012-01-20 23:40 +1100
                Re: The Beep Function. Peter Nolan <peter.nolan40@gmail.com> - 2012-01-21 03:20 -0800
                Re: The Beep Function. "blank" <blank@blankety.blank.com> - 2012-01-21 15:20 +1100
                Re: The Beep Function. Peter Nolan <peter.nolan40@gmail.com> - 2012-01-21 03:23 -0800
                Re: The Beep Function. "blank" <blank@blankety.blank.com> - 2012-01-20 05:11 +1100
                Re: The Beep Function. Schmidt <sss@online.de> - 2012-01-17 18:58 +0100
                Re: The Beep Function. Helmut_Meukel <Helmut_Meukel@bn-hof.invalid> - 2012-01-17 23:13 +0100
                Re: The Beep Function. "DaveO" <djo@dial.pipex.com> - 2012-01-18 09:12 +0000
                Re: The Beep Function. "DaveO" <djo@dial.pipex.com> - 2012-01-18 09:22 +0000
                Re: The Beep Function. ralph <nt_consulting64@yahoo.net> - 2012-01-18 05:39 -0600
                Re: The Beep Function. Helmut_Meukel <Helmut_Meukel@bn-hof.invalid> - 2012-01-18 13:32 +0100
                Re: The Beep Function. ralph <nt_consulting64@yahoo.net> - 2012-01-15 13:30 -0600
                Re: The Beep Function. "blank" <blank@blankety.blank.com> - 2012-01-16 06:59 +1100
                Re: The Beep Function. "blank" <blank@blankety.blank.com> - 2012-01-16 06:55 +1100
                Re: The Beep Function. Peter Nolan <peter.nolan40@gmail.com> - 2012-01-16 06:06 -0800
                Re: The Beep Function. "Mike Williams" <Mike@WhiskyAndCoke.com> - 2012-01-13 22:34 +0000
                Re: The Beep Function. "blank" <blank@blankety.blank.com> - 2012-01-14 19:50 +1100
                Re: The Beep Function. Peter Nolan <peter.nolan40@gmail.com> - 2012-01-14 05:10 -0800
                Re: The Beep Function. Peter Nolan <peter.nolan40@gmail.com> - 2012-01-14 06:16 -0800
  Re: The Beep Function. "Henning" <computer_hero@coldmail.com> - 2012-01-19 20:25 +0100
    Re: The Beep Function. Peter Nolan <peter.nolan40@gmail.com> - 2012-01-20 03:56 -0800

csiph-web