beautypg.com

Apple Newton Programmer’s Newton 2.0 (for Newton 2.0) User Manual

Page 564

background image

C H A P T E R 1 4

Sound

14-10

Using Sound

For an example that uses output from a view based on the

protoKeypad

prototype, see the Newton DTS sample code on this topic.

Manipulating Sample Data

14

This section describes how to use the utility functions

ExtractByte

and

StuffByte

to manipulate individual bytes in sound sample data. Because of

performance considerations, you’ll want to manipulate sample data on the Newton
only when it’s absolutely necessary. Even simple operations, like the example here,
can take a long time to perform on a relatively small sound sample.

The following example, extracts bytes from the end of the sample data and adds
them to its beginning, thus reassembling the samples in reverse order to create a
“backwards” sound.

// backwardSound is a slot in the app’s base view

// if it’s nil then create the backward sound

if (not backwardSound) then

begin

// get a frame to work with

backwardSound := deepclone(ROM_funbeep);

// a var to store the modified sample data

local sampleHolder := Clone(backwardSound.samples);

local theSize := Length(sampleHolder) -1 ;

// Copy bytes from one end of the binary object

// to the other.

for i := 0 to theSize do

StuffByte(backwardSound.samples,i,

ExtractByte(sampleHolder,theSize-i));

end;

A better solution is to provide the backwards sound as a resource that can be
played just like any other sound; a number of sound editors are available to create
such a resource on a desktop computer.