Unformatted text preview:

Using ‘random’ numbersAutomating pattern creationEstheticsThe ‘default’ colors (mode 19)Choosing a random color-pairUsing patterns with more colorsTiling with a 4-color bitmapAutomating an ‘art show’In-class exercise #1Generating ‘white noise’The Waveform Audio-File FormatThe RIFF chunkThe FORMAT chunkThe DATA chunk8-Bit Sample Formats16-Bit Sample FormatsOur ‘mknoises.cpp’ demoIn-class exercise #2Using ‘random’ numbersSome ways the standard UNIX ‘rand()’ library-function can be deployed to generate graphics and soundAutomating 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-levelsThe ‘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 bitmapfor (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 FormatRIFF 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 systemThe RIFF chunk“RIFF”Total number of the file’s bytes that follow“WAVE” chunkName chunkSize chunkType (ascii characters) (unsigned int) (ascii characters) 12 bytesRIFF = “Resource Interchange File Format”Remainder of the RIFF chunk holds all the file’s other chunksThe FORMAT chunk“fmt “(ascii characters)Number of chunk’s bytes that follow (i.e., 16)samplesPerSec(e.g., 44100)avBytesPerSecformatTag(i.e., 0 or 1)nChannels(mono=1 orstereo=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 bytesThe 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 variable8-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 channel16-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 channelOur ‘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-codeEach ‘sample’ stores a pair of 8-bit pulse-code valuesIn-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


View Full Document

USF CS 686 - Random numbers

Documents in this Course
Load more
Download Random numbers
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Random numbers and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Random numbers 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?