From: Svjatoslav Agejenko Date: Wed, 2 Jul 2025 18:12:10 +0000 (+0300) Subject: Better code readability X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=HEAD;p=qbasicapps.git Better code readability --- diff --git a/Networking/Digital data over analog audio channel/xi2msg.bas b/Networking/Digital data over analog audio channel/xi2msg.bas index 5b2bfaa..7c85c82 100644 --- a/Networking/Digital data over analog audio channel/xi2msg.bas +++ b/Networking/Digital data over analog audio channel/xi2msg.bas @@ -5,7 +5,7 @@ ' ' Changelog: ' 2001, Initial version -' 2024, Improved program readability using AI +' 2024 - 2025, Improved program readability ' ' Data is encoded in the audio by using frequency modulation. ' When decoding data, program locates peaks between waveforms. @@ -35,7 +35,9 @@ DECLARE SUB byt (a) DIM SHARED file1$ DIM SHARED file2$ -DIM SHARED buf(-100 TO 10000) + +' Contains the actual audio waveform samples +DIM SHARED audioBuffer(-100 TO 10000) DIM SHARED bus AS STRING * 1000 DIM SHARED bufi DIM SHARED bg @@ -43,7 +45,9 @@ DIM SHARED sm DIM SHARED beg DIM SHARED wai DIM SHARED old2 -DIM SHARED stat(1 TO 10) + +' Statistical Analysis Array (contains measured peak distances) +DIM SHARED statisticalArray(1 TO 10) DIM SHARED statl DIM SHARED aver DIM SHARED byte AS STRING * 1 @@ -67,7 +71,7 @@ FOR a = 1 TO 1000 bufi = bufi + 1 c = ASC(b$) IF c > 127 THEN c = c - 255 - buf(bufi) = c + audioBuffer(bufi) = c NEXT a IF (EOF(2) = 0) AND (bufi < 8000) THEN GOTO 2 anal @@ -90,8 +94,8 @@ SUB anal ' Plot the buffer data points FOR b = 0 TO 200 - PSET (b, buf(b + a - 101) + 300), 0 - PSET (b, buf(b + a - 100) + 300), 14 + PSET (b, audioBuffer(b + a - 101) + 300), 0 + PSET (b, audioBuffer(b + a - 100) + 300), 14 NEXT b LINE (old2 - a + 100, 170)-(old2 - a + 100, 430), 0 @@ -99,7 +103,7 @@ SUB anal ' Calculate the average value of the buffer segment c = 0 FOR b = a TO a + (avv - 1) - c = c + buf(b) + c = c + audioBuffer(b) NEXT b c = c / (avv / 2) @@ -127,7 +131,7 @@ SUB anal ' Shift the buffer data to the left FOR a = bufi - (avv - 2) TO bufi - buf(a - (bufi - (avv - 2)) + 1) = buf(a) + audioBuffer(a - (bufi - (avv - 2)) + 1) = audioBuffer(a) NEXT a ' Update the starting index of the buffer @@ -171,14 +175,14 @@ SUB byt (a) IF statl > 8 THEN statl = 1 b = 0 - IF stat(1) = 1 THEN b = b + 128 - IF stat(2) = 1 THEN b = b + 64 - IF stat(3) = 1 THEN b = b + 32 - IF stat(4) = 1 THEN b = b + 16 - IF stat(5) = 1 THEN b = b + 8 - IF stat(6) = 1 THEN b = b + 4 - IF stat(7) = 1 THEN b = b + 2 - IF stat(8) = 1 THEN b = b + 1 + IF statisticalArray(1) = 1 THEN b = b + 128 + IF statisticalArray(2) = 1 THEN b = b + 64 + IF statisticalArray(3) = 1 THEN b = b + 32 + IF statisticalArray(4) = 1 THEN b = b + 16 + IF statisticalArray(5) = 1 THEN b = b + 8 + IF statisticalArray(6) = 1 THEN b = b + 4 + IF statisticalArray(7) = 1 THEN b = b + 2 + IF statisticalArray(8) = 1 THEN b = b + 1 ' Print the byte value to the screen LOCATE 10, 69 @@ -208,7 +212,7 @@ SUB byt (a) ' Print the bit value to the screen LOCATE 10, 50 + (statl * 2) - stat(statl) = a + statisticalArray(statl) = a PRINT a @@ -254,7 +258,7 @@ SUB pfo (f, t, it) IF statl > 10 THEN FOR a = 1 TO 10 - aver = aver + stat(a) + aver = aver + statisticalArray(a) NEXT a aver = aver * 1.5 / 10 @@ -267,7 +271,7 @@ SUB pfo (f, t, it) END IF ' Store the current frame index - stat(statl) = f - old2 + statisticalArray(statl) = f - old2 END IF ' Decode the bits if we are in the decoding state @@ -278,7 +282,7 @@ SUB pfo (f, t, it) statl = 0 FOR a = 1 TO 8 - stat(a) = 0 + statisticalArray(a) = 0 NEXT a GOTO 4 diff --git a/QBasic tutorial/Group 2/02.bas b/QBasic tutorial/Group 2/02.bas index 39b2c4e..4495c79 100755 --- a/QBasic tutorial/Group 2/02.bas +++ b/QBasic tutorial/Group 2/02.bas @@ -1,14 +1,26 @@ -1 -s = s + 1 ' speed -PRINT s; "x" +' Simple Sound Sweep Program -FOR a = 100 TO 1000 STEP s - SOUND a, .1 -NEXT a +' Start label for infinite loop +beginLoop: -FOR a = 1000 TO 100 STEP -s - SOUND a, .1 -NEXT a +' Increase speed on each pass - this controls how quickly frequencies change +speed = speed + 1 -GOTO 1 +' Show current speed multiplier to user +PRINT speed; "x" +' Forward frequency sweep from 100Hz to 1000Hz +FOR currentFrequency = 100 TO 1000 STEP speed + ' Create sound with current frequency for 0.1 seconds + ' SOUND format: SOUND frequency, duration + SOUND currentFrequency, .1 +NEXT currentFrequency + +' Reverse frequency sweep from 1000Hz to 100Hz +FOR currentFrequency = 1000 TO 100 STEP -speed + ' Create sound with current frequency for 0.1 seconds + SOUND currentFrequency, .1 +NEXT currentFrequency + +' Jump back to beginning for continuous effect +GOTO beginLoop diff --git a/QBasic tutorial/Group 2/04.bas b/QBasic tutorial/Group 2/04.bas index a428f1e..2ef6a57 100755 --- a/QBasic tutorial/Group 2/04.bas +++ b/QBasic tutorial/Group 2/04.bas @@ -1,11 +1,13 @@ CLS -a$ = "-two-" -PRINT a$ +' Initialize the string variable with "-two-" +stringValue$ = "-two-" +PRINT stringValue$ -a$ = a$ + "three" -PRINT a$ - -a$ = "one" + a$ -PRINT a$ +' Append "three" to the existing string +stringValue$ = stringValue$ + "three" +PRINT stringValue$ +' Prepend "one" to the string to form a complete sequence +stringValue$ = "one" + stringValue$ +PRINT stringValue$ diff --git a/QBasic tutorial/Group 2/11.bas b/QBasic tutorial/Group 2/11.bas index 0831a84..ef910ae 100755 --- a/QBasic tutorial/Group 2/11.bas +++ b/QBasic tutorial/Group 2/11.bas @@ -1,10 +1,19 @@ +' Simple Character Extractor Program +' This program takes user input and prints each character individually + CLS -INPUT "Enter some text:", t$ +' Get user input +INPUT "Enter some text:", userInput$ + +' Process each character in the input string +FOR charPosition = 1 TO LEN(userInput$) + ' Get the left portion of the string up to current position + leftPortion$ = LEFT$(userInput$, charPosition) -FOR i = 1 TO LEN(t$) - a$ = LEFT$(t$, i) - b$ = RIGHT$(a$, 1) - PRINT "letter:"; b$ -NEXT i + ' Extract the current character (rightmost character of left portion) + currentChar$ = RIGHT$(leftPortion$, 1) + ' Print the current character with label + PRINT "letter:"; currentChar$ +NEXT charPosition diff --git a/Simulation/Explosion/explode.bas b/Simulation/Explosion/explode.bas index feb1258..ad6c53d 100755 --- a/Simulation/Explosion/explode.bas +++ b/Simulation/Explosion/explode.bas @@ -130,26 +130,22 @@ FOR y = 2 TO 99 FOR x = 2 TO 99 ' Update pressure based on speed in the x-direction. IF spdx(x, y) > 0 THEN - spdxp(x - 1, y) = ((press(x, y) * spdx(x - 1, y)) + (spdx(x, y) * spdx(x, y))) / - (press(x, y) + spdx(x, y)) - spdx(x - 1, y) + spdxp(x - 1, y) = ((press(x, y) * spdx(x - 1, y)) + (spdx(x, y) * spdx(x, y))) / (press(x, y) + spdx(x, y)) - spdx(x - 1, y) END IF ' Update pressure based on speed in the y-direction. IF spdy(x, y) > 0 THEN - spdyp(x, y - 1) = ((press(x, y) * spdy(x, y - 1)) + (spdy(x, y) * spdy(x, y))) / - (press(x, y) + spdy(x, y)) - spdy(x, y - 1) + spdyp(x, y - 1) = ((press(x, y) * spdy(x, y - 1)) + (spdy(x, y) * spdy(x, y))) / (press(x, y) + spdy(x, y)) - spdy(x, y - 1) END IF ' Handle negative speeds in the x-direction. IF spdx(x - 1, y) < 0 THEN - spdxp(x, y) = ((press(x, y) * spdx(x, y)) - (spdx(x - 1, y) * spdx(x - 1, y))) / - (press(x, y) - spdx(x - 1, y)) - spdx(x, y) + spdxp(x, y) = ((press(x, y) * spdx(x, y)) - (spdx(x - 1, y) * spdx(x - 1, y))) / (press(x, y) - spdx(x - 1, y)) - spdx(x, y) END IF ' Handle negative speeds in the y-direction. IF spdy(x, y - 1) < 0 THEN - spdyp(x, y) = ((press(x, y) * spdy(x, y)) - (spdy(x, y - 1) * spdy(x, y - 1))) / - (press(x, y) - spdy(x, y - 1)) - spdy(x, y) + spdyp(x, y) = ((press(x, y) * spdy(x, y)) - (spdy(x, y - 1) * spdy(x, y - 1))) / (press(x, y) - spdy(x, y - 1)) - spdy(x, y) END IF NEXT x NEXT y