QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: hanness on August 27, 2019, 01:36:19 am

Title: Problem with creation of high frequency sounds
Post by: hanness on August 27, 2019, 01:36:19 am
Help for the QB64 SOUND command indicates that the syntax looks like this:

SOUND frequency, duration

Valid values for frequency are supposed to be in the range of 37 to 32767. However, I notice that as I increase the frequency, somewhere in the vicinity of 13,300, as I increase the frequency value, the frequency of the sound actually decreases.

I do realize that we are talking about getting up to some very high frequencies here, but I do actually have a very valid reason for wanting to generate such high frequency sounds. My goal is actually to get up to about 20,000 (20KHz).

Here is a super simple code snippet showing the problem. The program will print the frequency about to be played, then play that sound very briefly. It will then increase the frequency by 50 Hz and repeat. Note how somewhere around 13,300 the frequency of the sound being played actually goes down.

FOR x = 12500 TO 20000 STEP 50
    PRINT x
    SOUND x, 20
NEXT x

Title: Re: Problem with creation of high frequency sounds
Post by: johnno56 on August 27, 2019, 04:05:13 am
I modified the program to do the full range (37 to 32767) using a duration of 2.
I am still recovering from the effects... lol
One thing I noticed was, as the pitch increased, there seemed to be some weird harmonics going on at the same time. Difficult to explain. It sounded like random notes 'far away'. Best description I could think of... They, the far away sounds, seemed to be louder running the original program.
I am not making much sense... I don't think this is very helpful... But I tried....
Title: Re: Problem with creation of high frequency sounds
Post by: bplus on August 27, 2019, 11:24:22 am
Yeah I think after a certain point in frequency a second or third or 4th cycle enters and as they increase another cycle enters... odd?

Attempt to print frequency number when next sound is sounded
Code: QB64: [Select]
  1. FOR x = 37 TO 20000 STEP 50
  2.     PRINT x
  3.     SOUND x, 4
  4.     _DELAY 4 / 18
  5.  

Quote
I am not making much sense... I don't think this is very helpful... But I tried....
Not making sense after that ear full makes sense! ;-))

Title: Re: Problem with creation of high frequency sounds
Post by: RhoSigma on August 27, 2019, 01:04:48 pm
What you notice are probably so called aliasing distortions, which reach approx. halfe the volume of the actually played sound on the sum and difference of the sample frequency and the played frequency.

eg. assume a sine wave is sampled (digitized) with a frequency of 5000Hz, then if you reply this sample with a changed period to generate a sound of 10000Hz, then you get those distortions on 5000Hz (10000Hz - 5000Hz) and on 15000Hz (10000Hz + 5000Hz)

That's why most HiFi systems use 44100Hz as sampling (digitizing) frequency, so assumed the highest human hearable frequence is around 16000-18000Hz even the highest replayed frequency in sum/diff with the sample frequencey is far out of the hearable range.

Well, this is a simplyfied view, to go on further watch this video, approx. from time 3:00 the effect is explained.
Title: Re: Problem with creation of high frequency sounds
Post by: hanness on August 27, 2019, 06:33:20 pm
I played with it a bit more and the behavior is that as I continue to increase the frequency parameter the frequency that is played goes up, then goes down to a certain point, then it goes back up again, then down again.....

Just to be sure it was not something about my audio drivers, speakers, etc. I tried this in Visual BASIC and the frequency goes up without any undesired behavior.

It's a shame I can't get this to work because I'm working on something insanely cool that would be easy to whip up in QB64. Already tested and proved to work on Android and iPhone.
Title: Re: Problem with creation of high frequency sounds
Post by: Petr on August 28, 2019, 11:50:31 am
Hi. Try using the _SNDRAW command, which is more demanding than SOUND. Personally, I think it's bad if sound command in the programming language to emit resonant harmonic waves that resonate with the main sound.
The following program works - bad. There is no complete match with SOUND frequencies, but shows how to make sound through _SNDRAW.
I am not 100 percent with this command. The sampling rate of 44100 Hz is for a maximum frequency of 20 Khz (always twice the number of samples compared to the frequency). There is a bug because the program will behave differently with _SNDRAW Left, Right, or with two lines below it as _SNDRAW left and _SNDRAW right below. Unfortunately, I'm sure no one will try to fix this, because it is a very demanding program.


Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 256)
  2. FOR H = 1 TO 10
  3.     READ b, e, s, l: 'read begin, end, step and sound lenght
  4.  
  5.     FOR st = b TO e STEP s
  6.         CLS
  7.         LOCATE 1
  8.         PRINT "Freq:"; st * 2
  9.         SND st, l
  10.         '  SOUND s, .1
  11.         _DISPLAY
  12.         _LIMIT 20
  13.     NEXT
  14.  
  15. DATA 50,75,.5,.1
  16. DATA 150,75,-.5,.2
  17. DATA 75,750,10,.1
  18. DATA 750,7000,50,.3
  19. DATA 7000,70,-100,.2
  20. DATA 70,75,.5,.1
  21. DATA 75,150,3,.5
  22. DATA 150,75,-.5,.5
  23. DATA 75,500,5,.1
  24. DATA 500,50,-.5,.1
  25.  
  26.  
  27.  
  28. SUB SND (freq!, lenght!)
  29.     samples& = 22050 * lenght '22050 because MONO
  30.     do_Zmeny! = 22050 / freq
  31.     amplitude# = 1 / do_Zmeny!
  32.  
  33.     'graphic visualisation:  samples to 800 (to screen width):
  34.     gs = 800 / samples&
  35.  
  36.     FOR sn = 1 TO samples&
  37.         e# = e# + amplitude#
  38.         IF ABS(e#) >= 1 THEN amplitude# = amplitude# * -1
  39.         _SNDRAW SIN(e#)
  40.  
  41.         'visualisation
  42.         oldGs = nGs: oldGy = nGy
  43.         nGs = gs * sn
  44.         nGy = 300 + 100 * SIN(e#)
  45.         IF oldGs THEN LINE (oldGs, oldGy)-(nGs, nGy)
  46.         '-----------------
  47.  
  48.         DO WHILE _SNDRAWLEN > 0: LOOP
  49.     NEXT
  50.  
  51.  
Title: Re: Problem with creation of high frequency sounds
Post by: hanness on August 28, 2019, 07:16:53 pm
Thanks, Petr. I appreciate your response.