|
|
|
|
|
Making Sounds with BASIC ProgramsUsing the SOUND and ENVELOPE keywords to generate music
An article describing the way that BASIC can be used to produce sounds of varying complexities - from single notes to chords, and envelopes allowing instrument simulation
IntroductionThis article is part of the Learn to Program with BBC BASIC series, and covers the use of sound, music and noise in BBC BASIC programming. It is useful prequel to reading about video game programming in BBC BASIC, and other BASIC languages. Sounds are useful in programs for a number of reasons, from alerting the user to playing a tune, and there are some excellent facilities for making sounds in the BBC BASIC language. Before reading this article, it is advantageous to ensure that:
The reader will take away a good knowledge of the two important keywords SOUND and ENVELOPE, as well as an appreciation of using channels. Some of these facilities may not be accurately emulated on every platform, however, BBC BASIC for Windows, referred to in the Learn to Program with BBC BASIC article implements these features flawlessly. Sounds and ChannelsIn BBC BASIC, there are 4 channels upon which a sound can be played. Channels 1-3 are reserved for actual musical notes, and channel 0 is the 'noise' channel. For now, we shall concentrate on the three note channels. The SOUND command takes the following form: SOUND <channel>, <volume>, <pitch>, <length> The channel refers to the channel through which to play the sound. The channels can play at the same time. The volume can be negative, representing the actual volume from -15 to 0 (loud to silent), or positive. If it is positive then it represents one of the 16 sound envelopes that can be defined by the programmer (see The Envelope Keyword, below). The third parameter can be a value between 0 and 255, representing the pitch of the note in quarters of a semitone. For the musically minded, steps of one octave are represented by an increase or decrease in pitch of 48 units, or 28 for a step of a perfect 5th. Middle C has the easily-remembered value 100. Finally, the length parameter can be between -1 (continuous) and 254. A non-negative number represents the length of the note in 20ths of a second. The note can be interrupted if it has a length of -1 by sending another note down the same channel. To play a scale starting from Middle C, and moving up in single tones, we could use the following code: 10 FOR nPitch% = 100 TO 192 STEP 4 20 SOUND 1, -15, nPitch%, 20 30 NEXT nPitch% Each note will sound for 1 second. The reader might like to consider how to program a scale that avoids the black notes on a piano, given the following: C 100 C# 104 D 108 D# 112 E 116 F 120 F# 124 G 128 G# 132 A 136 A# 140 B 144 (Hint : the sharps have to be missed out; not played) The Envelope KeywordMostly used for special effects, the sound envelope can be manipulated in two ways:
The Envelope keyword takes 14 parameters, each of which has a special meaning in terms of either the Pitch or Volume envelope of the sound. It is quite easy to become completely baffled by this function of BBC BASIC, but mastering the ENVELOPE keyword is very rewarding, and worth the effort it takes to understand how it works. The volume envelope has four stages:
If one imagines striking a note on a piano, the volume initially rises from zero to a peak as the string is struck. This is the attack phase. Afterwards, the volume will decay slightly (past the peak of the attack phase), before continuing at a more or less constant volume (the sustain) before fading out during the release phase. The pitch envelope has three stages, each consisting of up to 255 steps, with a pitch that can vary between -128 and 127 units (quarter of a semitone). The length of time over which the envelope will be applied is therefore the step length multiplied by the total number of steps in phases 1, 2 and 3. The volume envelope is then applied indirectly throughout the duration of the envelope, which is driven by the sounding of a note via the SOUND keyword. This SOUND statement (the reader will recall) specifies the Envelope to use, and the starting note (pitch). So, the generic form of the ENVELOPE keyword is as follows: ENVELOPE n, t, p1, p2, p3, n1, n2, n3, v1, v2, v3, v4, peak, final The value 'n' is the envelope number, and 't' is the length of each step of the pitch envelope. 'n1' to 'n3' give the number of steps of each phase in the pitch envelope, with 'p1' to 'p3' giving the rate of change (-128 to 127). Finally, 'v1' to 'v4' give the rate of change of volume during the attack, decay, sustain, and release phases. The 'peak' is the required peak once the attack phase has finished, and the 'final' volume is the desired end volume once the decay phase has stopped. The reader might like to try the following: 10 ENVELOPE 1, 5, 20, -5, 1, 7, 7, 7, 126, 0, 0, -126, 126, 0 20 SOUND 1, 1, 100, 20 The Noise ChannelFinally, the noise channel can be used to produce white noise or buzzing noises useful for creating explosion or drum effects (amongst others). It is worthwhile experimenting with various values to find those that work for a specific purpose, but the amount of variety that is available is somewhat limited. Links
The copyright of the article Making Sounds with BASIC Programs in Computer Programming Tutorials is owned by Guy Lecky-Thompson. Permission to republish Making Sounds with BASIC Programs in print or online must be granted by the author in writing.
|
|
|
|