# HyperCard IIgs #3 Pitching Sampled Sounds

## Written by Mark Cecys & Matt Deatherage (March 1991)

This Technical Note describes the "relative pitch" field used in sound resources played by HyperCard (and sound scraps that HyperCard doesn't use) -- what it does and what to put in it.

## What is this relative pitch thing?

There are basically two ways to use a sound sample, in HyperCard or anywhere else: as a sample of a wave of definite pitch, or as a miniature "tape recording" of some sound that is not intended to be used as a sample of indefinite pitch.

### Definite Pitch

To play a sample at the correct pitch, HyperCard assumes two things about the sample: it was sampled at a rate of 26.32 KHz, and the associated wave was playing a pitch of 261.63 Hz, when it was sampled.

In the real world, where most of us live, this is not very practical. To help compensate for reality, the sample sound format includes a "relative pitch" field, which can tell HyperCard (or anyone else playing the sound) how to compensate for the difference in pitch between the sample's actual pitch and a pitch of 261.63 Hz.

Follow these steps to calculate the relative pitch parameter for a given sampled sound resource. If the wave is of definite pitch, you must know the frequency of the source wave and the sampling rate for the sample in question.

1. Calculate the difference ratio r. In the equation below, Fw is the frequency of the sample (in Hz) and Fs is the sampling rate for the sample.
2. ```
261.63       Fs
r   =  ------  X  ------
Fw       26,320

```
3. Extract an offset to the pitch:
4. ```
offset  =  3072  X log (r)
2

```

(Remember that you can substitute (ln(r)/ln(2)) if your calculator doesn't provide the log in base 2.)

5. If offset is negative, make it positive and set bit 15 to tell sound players to lower the pitch instead of raise it. If offset is negative:
6. ```
relative  =  |offset| +  \$8000

```

If offset is positive:

```
relative  =  offset

```

That's all. Store the value of tuning in the sampled sound for the "relative pitch" field and HyperCard will take care of the rest.

### Indefinite pitch

Sounds which are not samples of definite pitch (for example, a thunder clap or the sound of your mother saying "hello") should not need to be made to match pitch. Only sounds produced using optional parameters of HyperCard's Play command need to go through the same process outlined for "Definite pitch". In these cases, however, you don't need to worry about the frequency of the sample. Instead of using the equation provided in step 1 above, use this instead:

```
Fs
r   =  ------
26,320

```

(or just use 261.63 for Fw.) Take the value of r and use it for steps two and three above.

## A HyperTalk sample

The following simple button script will calculate the correct value of relative for you, given the other values in card fields named Fw, Fs and card fields named offset and relative to use as containers:

```
on mouseUp
lock screen
set numberFormat to "0"
put the value of card field Fs * 261.63 into r
put the value of card field Fw into denominator -- the bottom of the fraction
multiply denominator by 26320
divide r by denominator

put log2(r) into card field offset
multiply card field offset by 3072

if card field offset <0 then
put abs(the value of card field offset) into card field tuning
add 32768 to card field relative
end if

unlock screen
end mouseUp

```

## Further Reference

This and all of the other Apple II Technical Notes have been converted to HTML by Aaron Heiss as a public service to the Apple II community, with permission by Apple Computer, Inc. Any and all trademarks, registered and otherwise, are properties of their owners.