Using ‘random’ numbers Some ways the standard UNIX ‘rand()’

advertisement
Using ‘random’ numbers
Some ways the standard UNIX ‘rand()’
library-function can be deployed to
generate graphics and sound
Automating pattern creation
• Use these standard runtime functions;
– #include <stdlib.h>
– int rand( void );
– void srand( unsigned int seed );
• Make a new 8x8 bitmap pattern like this:
unsigned char
pat[ 8 ];
for (k = 0; k < 8; k++) pat[ k ] = rand();
fgcolor = rand(); bgcolor = rand();
Esthetics
• Use a ‘brighter’ color in the foreground
• Use a ‘darker’ color in the background
• To implement this discipline we need to
know how the ‘color-table’ is arranged
• In mode 19 there are 256 default colors
• Among these are 24 color-groups:
– 3 intensity-levels plus 3 saturation-levels
The ‘default’ colors (mode 19)
•
•
•
•
•
•
•
Range for the 72 brightest colors: 32–103
Range for the 72 midlevel colors: 104-175
Range for the 72 darkest colors: 176-247
Colors 0-15 are the standard EGA colors
Colors 16-31 are sixteen grayscale colors
Colors 248-255 are solid black (default)
(But all of these are fully ‘programmable’)
Choosing a random color-pair
• foreground color (from the ‘bright’ range):
fgcolor = ( ( rand() & 0xFF ) % 72 ) + 32;
• Background color (from the ‘dark’ range):
bgcolor = ( ( rand() & 0xFF ) % 72 ) + 104;
Using patterns with more colors
•
•
•
•
Same concepts can be extended
But need a larger pattern-bitmap
Example: use 2 bits-per-pixel (4 colors)
An 8x8 pattern that using 4 colors:
unsigned short
pat2bpp[ 8 ];
unsigned char
palette4[ 4 ];
for (r = 0; r < 8; r++) pat2bpp[ r ] = rand();
for (c = 0; c < 4; c++) palette4[ c ] = rand();
Tiling with a 4-color bitmap
for (y = 0; y < hres; y++)
{
unsigned short bits = pat2bpp[ y % 8 ];
for (x = 0; x < hres; x++)
{
int
i = ( bits >> ( x % 8 )&3;
int
color = palette4[ i ];
vram[ y*hres + x ] = color;
}
}
Automating an ‘art show’
• Can use a standard C runtime function:
#include <unistd.h>
void sleep( int seconds );
• User views your screen for fixed duration:
while ( !done )
{
draw_next_scene(); sleep(1);
}
In-class exercise #1
• Can you use the UNIX random-number
generator function to create an art show
(i.e., a succession of esthetically pleasing
patterns that tile the display screen for a
timed duration)?
Generating ‘white noise’
• We can use the ‘rand()’ function to create
pulse-code data for a Waveform Audio file
• When we play that .wav file what we will
hear is a sound known as ‘white noise’
• Let’s look at the details for IBM/Microsoft
Waveform Audio file-format (.wav) – their
original ‘simple’ version (Version 1.0)
• It’s contents is organized into ‘chunks’
The Waveform Audio-File Format
RIFF Chunk (12 bytes)
FORMAT Chunk (24 bytes)
DATA Chunk (size varies)
Version 1.0
Developed jointly in 1991 by IBM and Microsoft Corporation
for the Windows 3.1 operating system
The RIFF chunk
RIFF = “Resource Interchange File Format”
12 bytes
“RIFF”
Total number of
the file’s bytes
that follow
“WAVE”
chunkName
(ascii characters)
chunkSize
(unsigned int)
chunkType
(ascii characters)
Remainder of the RIFF chunk
holds all the file’s other chunks
The FORMAT chunk
“fmt “
(ascii characters)
formatTag
(i.e., 0 or 1)
nChannels
(mono=1 or
stereo=2)
avBytesPerSec
Number of chunk’s
bytes that follow
(i.e., 16)
samplesPerSec
(e.g., 44100)
frameAlignmt bitsPerSample
(i.e., 1, 2, 4)
(e.g., 8 or 16)
Originally was a fixed size: header (8 bytes) plus parameters (16 bytes)
TOTAL SIZE = 24 bytes
The DATA chunk
“data“
(ascii characters)
Number of chunk’s
bytes that follow
(i.e., 16)
The PCM data goes here
(Pulse Code Modulation)
size is variable
8-Bit Sample Formats
• 8-bit monoaural: Each sample is one byte
– Value is an ‘unsigned char’ in range 0..255
– The ‘neutral’ value is 128 (i.e., silence)
• 8-bit stereo: Each sample is a byte-pair
– First sample-value is for left-hand channel,
second sample-value is right-hand channel
16-Bit Sample Formats
• 16-bit monoaural: sample-size is two bytes
– Value is a ‘short’ in range -32768..32767
– The default storage-convention is ‘Big-Endian’
– The neutral value is 0 (i.e., silence)
• 16-bit stereo: sample-size is four bytes
– First sample-value is for left-hand channel,
second sample-value is right-hand channel
Our ‘mknoises.cpp’ demo
• Program creates a file named ‘noises.wav’
• It demonstrates stereophonic ‘white noise’
– First the left-hand channel plays white noise
– Then right-hand channel plays white noise
• Uses ‘8-bit stereo’ sample-format:
Left-channel pulse-code
Right-channel pulse-code
Each ‘sample’ stores a pair of 8-bit pulse-code values
In-class exercise #2
• Can you modify the ‘mknoises.cpp’ demo
so that noise from the left-hand channel
gradually diminishes in volume while the
noise from the right-channel is gradually
getting louder?
Download