QB64 Phoenix Edition
experimenting with _SndRaw - Printable Version

+- QB64 Phoenix Edition (https://staging.qb64phoenix.com)
+-- Forum: QB64 Rising (https://staging.qb64phoenix.com/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://staging.qb64phoenix.com/forumdisplay.php?fid=3)
+---- Forum: Works in Progress (https://staging.qb64phoenix.com/forumdisplay.php?fid=9)
+---- Thread: experimenting with _SndRaw (/showthread.php?tid=658)



experimenting with _SndRaw - madscijr - 07-21-2022

In looking for a way to make a cool rumbling sound for my Lunar Lander game 
(I want it to sound like the 1979 Atari Lunar Lander & Asteroids games) 
I came across some code at a Japanese site (thank the maker for Google translate)
which made a sound, so I just made a loop that changes some parameters 
to see what kind of sounds come from different numbers. 

Here is the code in case anyone is curious. 

Code: (Select All)
' A _SndRaw experiment
' based on code by Senji Niban, 2/4/2016, at senjiniban-hatenablog.com
' Modified by madscijr, 7/21/2022
'
' --------------------------------------------------------------------------------
' "Keep it simple, Stupid!" Again (2016-02-02) (senjiniban-hatenablog.com)
' https://senjiniban-hatenablog-com.translate.goog/?page=1454854659&_x_tr_sl=ja&_x_tr_tl=en&_x_tr_hl=en&_x_tr_pto=sc
'
' From Senji Niban, 2/4/2016
'
' Tags: FORTH QB64 QuickBasic
'
' I'm always saying that I'm just doing it, so I'd like you to listen to it in
' half the story, but I've become quite fond of QB64, and I may make an app with
' this.
' Even if I used to do Baisc programs in earnest, I think that QuickBasic
' compatibility is quite unreasonable to work on programs in earnest nowadays,
' and in fact it is a premise to do Python etc. in parallel, but something this
' source I feel that the condition that comes into my head is very irresistible.
' You may have read this article before.
' postd.cc
' When I looked at the program below, I thought that there was nothing I couldn't
' understand compared to other languages. It's as simple as trying to make a
' sound with a microcomputer , and it feels like you can see the operation of the
' CPU rather than calling a sound-related class in an object language.
' --------------------------------------------------------------------------------

' NOTE: There were no Dim statements so I'm not sure what types these should be.
'       I tried dimming the variables based on what docs I could find
'       and it stopped working, so for now no variables are explicitly declared.

FREQ = 400 ' any frequency desired from 36 to 10,000 Pi2
Pi2 = 8 * Atn(1) ' 2 * pi
Amplitude = .3 ' amplitude of the signal from --1.0 to 1.0
Amplitude = -1
iSeconds% = 5
iSeconds% = 2

Do
    FREQ = FREQ + 200
    Amplitude = Amplitude + 0.3 ' amplitude of the signal from --1.0 to 1.0
    SampleRate = _SndRate ' sets the sample rate
    FRate = FREQ / SampleRate

    Cls
    Print "_SndRaw experiment"
    Print
    Print "FREQ       =" + _Trim$(Str$(FREQ))
    Print "Amplitude  =" + _Trim$(Str$(Amplitude))
    Print "SampleRate = " + _Trim$(Str$(SampleRate)) + " = _SndRate"
    Print "FRate      = " + _Trim$(Str$(FRate)) + " = FREQ / SampleRate"
    Print
    Print "Press any key to quit"

    For Duration = 0 To iSeconds% * SampleRate ' play 5 seconds
        _SndRaw Amplitude * Sin(Pi2 * Duration * FRate) ' sine wave
    Next Duration

    Do: Loop While _SndRawLen
Loop Until InKey$ <> ""
End

' Speaking of seeing the operation of the CPU, with QB64, PEEK and POKE could
' also be used to directly access the memory. Actually, although it is said to be
' simple, if the execution environment is dropped, it is about 100M, and as I
' wrote last time, it is compatible with OpenGL and networks, so it may not be so
' simple, but after all it is Basic in terms of language specifications. I don't
' think it's suitable for writing huge programs. However, the low hurdle due to
' this simplicity is very big. As Chuck Moore, who made Forth, is proud of the
' size of the sauce, it is nonsense, and a compact sauce for a single purpose
' would be good. Moore says "1 mega is enough". However, the exe file that QB64
' spits out is a little larger than one floppy disk. Lately, I wanted to make an
' app with a program, but I couldn't imagine what it would be like, and I was
' sick of it, but the "SHOOTING SIXTEEN" made with QuickBasic (although it
' doesn't move) is quite tiny . Looking at Zebius , I'm wondering if I should
' make a very simple tool for doing what I want to do.