ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
sound.c
Go to the documentation of this file.
1/*****************************************************************************
2 * ugBASIC - an isomorphic BASIC language compiler for retrocomputers *
3 *****************************************************************************
4 * Copyright 2021-2026 Marco Spedaletti (asimov@mclink.it)
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *----------------------------------------------------------------------------
18 * Concesso in licenza secondo i termini della Licenza Apache, versione 2.0
19 * (la "Licenza"); è proibito usare questo file se non in conformità alla
20 * Licenza. Una copia della Licenza è disponibile all'indirizzo:
21 *
22 * http://www.apache.org/licenses/LICENSE-2.0
23 *
24 * Se non richiesto dalla legislazione vigente o concordato per iscritto,
25 * il software distribuito nei termini della Licenza è distribuito
26 * "COSÌ COM'È", SENZA GARANZIE O CONDIZIONI DI ALCUN TIPO, esplicite o
27 * implicite. Consultare la Licenza per il testo specifico che regola le
28 * autorizzazioni e le limitazioni previste dalla medesima.
29 ****************************************************************************/
30
31/****************************************************************************
32 * INCLUDE SECTION
33 ****************************************************************************/
34
35#include "../../ugbc.h"
36
37/****************************************************************************
38 * CODE SECTION
39 ****************************************************************************/
40
52/* <usermanual>
53@keyword SOUND
54
55@english
56
57The ''SOUND'' command add a touch of interactivity and liveliness to your programs. Simply put,
58the ''SOUND'' command allows you to generate sounds directly from your computer. You can
59give just the frequency (to start an infinite sound) or a frequency followed by a duration.
60You can play multiple sounds by concatenating frequencies or frequencies and durations.
61
62By combining several ''SOUND'' commands with different frequencies and durations, you
63can compose short melodies. For example, you can make a sound play when the user presses
64a button or reaches a goal in the game. You can simulate the sounds of explosions,
65gunshots, or any other effect you want.
66
67This command allows you to handle multiple audio channels at the same time, if the
68target has them, allowing you to create more complex sounds. The waveform of the sound
69generated can vary depending on the available hardware.
70
71@italian
72
73Il comando ''SOUND'' aggiunge un tocco di interattività e vivacità ai tuoi programmi.
74In parole povere, il comando ''SOUND'' ti consente di generare suoni direttamente
75dal tuo computer. Puoi fornire solo la frequenza (per avviare un suono infinito) o
76una frequenza seguita da una durata. Puoi riprodurre più suoni concatenando
77frequenze o frequenze e durate.
78
79Combinando più comandi ''SOUND'' con frequenze e durate diverse, puoi comporre brevi
80melodie. Ad esempio, puoi far riprodurre un suono quando l'utente preme un pulsante
81o raggiunge un obiettivo nel gioco. Puoi simulare i suoni di esplosioni, spari
82o qualsiasi altro effetto desideri.
83
84Questo comando ti consente di gestire più canali audio contemporaneamente, se
85il bersaglio li ha, consentendoti di creare suoni più complessi. La forma d'onda
86del suono generato può variare a seconda dell'hardware disponibile.
87
88@syntax SOUND f1[,d1][;f1[,d2][;...]] [ON channels]
89
90@example SOUND #440
91@example SOUND la;do;mi;sol
92@example SOUND #440, #250 ON #%001
93
94@target c128
95</usermanual> */
96void sound( Environment * _environment, int _freq, int _delay, int _channels ) {
97
98 sid_start( _environment, _channels );
99 sid_set_program( _environment, _channels, IMF_INSTRUMENT_GLOCKENSPIEL );
100 sid_set_frequency( _environment, _channels, _freq );
101 if ( _delay ) {
102 sid_set_duration( _environment, _channels, _delay / 20 /* approx! */ );
103 sid_wait_duration( _environment, _channels );
104 }
105
106}
107
119void sound_vars( Environment * _environment, char * _freq, char * _delay, char * _channels ) {
120
121 Variable * freq = variable_retrieve_or_define( _environment, _freq, VT_WORD, 440 );
122 if ( _channels ) {
123 Variable * channels = variable_retrieve_or_define( _environment, _channels, VT_WORD, 0x07 );
124 sid_start_var( _environment, channels->realName );
126 sid_set_frequency_vars( _environment, channels->realName, freq->realName );
127 if ( _delay ) {
128 Variable * delay = variable_retrieve_or_define( _environment, _delay, VT_WORD, 0 );
129 Variable * durationInTicks = variable_div_const( _environment, delay->name, 20, NULL );
130 sid_set_duration_vars( _environment, channels->realName, durationInTicks->realName );
131 sid_wait_duration_vars( _environment, channels->realName );
132 }
133 } else {
134 sid_start_var( _environment, NULL );
135 sid_set_program_semi_var( _environment, _channels, IMF_INSTRUMENT_GLOCKENSPIEL );
136 sid_set_frequency_vars( _environment, NULL, freq->realName );
137 if ( _delay ) {
138 Variable * delay = variable_retrieve_or_define( _environment, _delay, VT_WORD, 0 );
139 Variable * durationInTicks = variable_div_const( _environment, delay->name, 20, NULL );
140 sid_set_duration_vars( _environment, NULL, durationInTicks->realName );
141 sid_wait_duration_vars( _environment, NULL );
142 }
143 }
144
145}
Variable * variable_retrieve_or_define(Environment *_environment, char *_name, VariableType _type, int _value)
Variable * variable_div_const(Environment *_environment, char *_source, int _destination, char *_remainder)
void sound_vars(Environment *_environment, char *_freq, char *_delay, char *_channels)
Emit ASM code for SOUND ....
Definition sound.c:79
void sound(Environment *_environment, int _freq, int _delay, int _channels)
Emit ASM code for SOUND ....
Definition sound.c:57
void sid_set_frequency(Environment *_environment, int _channels, int _frequency)
Definition sid.c:473
void sid_wait_duration(Environment *_environment, int _channels)
Definition sid.c:855
void sid_set_program(Environment *_environment, int _channels, int _program)
Definition sid.c:281
void sid_start_var(Environment *_environment, char *_channels)
Definition sid.c:506
void sid_set_program_semi_var(Environment *_environment, char *_channels, int _program)
Definition sid.c:545
void sid_start(Environment *_environment, int _channels)
Definition sid.c:65
void sid_wait_duration_vars(Environment *_environment, char *_channels)
Definition sid.c:884
void sid_set_frequency_vars(Environment *_environment, char *_channels, char *_frequency)
Definition sid.c:747
void sid_set_duration_vars(Environment *_environment, char *_channels, char *_duration)
Definition sid.c:864
void sid_set_duration(Environment *_environment, int _channels, int _duration)
Definition sid.c:846
char * name
Definition ugbc.h:979
char * realName
Definition ugbc.h:982
#define IMF_INSTRUMENT_GLOCKENSPIEL
Definition ugbc.h:4583
struct _Variable Variable
Structure of a single variable.
struct _Environment Environment
Structure of compilation environment.
@ VT_WORD
Definition ugbc.h:455