Download presentation
Presentation is loading. Please wait.
1
Using ‘random’ numbers Some ways the standard UNIX ‘rand()’ library-function can be deployed to generate graphics and sound
2
Automating pattern creation Use these standard runtime functions; –#include –int rand( void ); –void srand( unsigned int seed ); Make a new 8x8 bitmap pattern like this: unsigned charpat[ 8 ]; for (k = 0; k < 8; k++) pat[ k ] = rand(); fgcolor = rand(); bgcolor = rand();
3
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
4
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’)
5
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;
6
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 shortpat2bpp[ 8 ]; unsigned charpalette4[ 4 ]; for (r = 0; r < 8; r++) pat2bpp[ r ] = rand(); for (c = 0; c < 4; c++) palette4[ c ] = rand();
7
Tiling with a 4-color bitmap for (y = 0; y < hres; y++) { unsigned shortbits = pat2bpp[ y % 8 ]; for (x = 0; x < hres; x++) { inti = ( bits >> ( x % 8 )&3; intcolor = palette4[ i ]; vram[ y*hres + x ] = color; }
8
Automating an ‘art show’ Can use a standard C runtime function: #include void sleep( int seconds ); User views your screen for fixed duration: while ( !done ) { draw_next_scene(); sleep(1); }
9
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)?
10
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’
11
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
12
The RIFF chunk “RIFF” Total number of the file’s bytes that follow “WAVE” chunkName chunkSize chunkType (ascii characters) (unsigned int) (ascii characters) 12 bytes RIFF = “Resource Interchange File Format” Remainder of the RIFF chunk holds all the file’s other chunks
13
The FORMAT chunk “fmt “ (ascii characters) Number of chunk’s bytes that follow (i.e., 16) samplesPerSec (e.g., 44100) avBytesPerSec formatTag (i.e., 0 or 1) nChannels (mono=1 or stereo=2) frameAlignmt (i.e., 1, 2, 4) bitsPerSample (e.g., 8 or 16) Originally was a fixed size: header (8 bytes) plus parameters (16 bytes) TOTAL SIZE = 24 bytes
14
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
15
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
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
17
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-codeRight-channel pulse-code Each ‘sample’ stores a pair of 8-bit pulse-code values
18
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?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.