A sample replaces the waveform of a track and is played when a note is set in the corresponding pitch. It can be loaded from a WAVE file or from raw audio data and can contain 1 or more channels. If it contains more than 1 channel, the number of channels must match the context’s number of channels, or else it cannot be set to a track and is ignored.

Samples are loaded into the general audio data container BKData and can be atached to multiple track.

// Data object to hold the frames
BKData data;

// Open WAVE file
FILE * file = fopen ("pika.wav", "rb");

// Initialize sample object with audio data from file
BKDataInitAndLoadWAVE (& data, file);

// Set sample to track
BKSetPtr (& track, BK_SAMPLE, & data);

// Play sample in original pitch
BKSetAttr (& track, BK_NOTE, BK_C_4 * BK_FINT20_UNIT);

// Play sample 1 octave higher
BKSetAttr (& track, BK_NOTE, BK_C_5 * BK_FINT20_UNIT);

Normalizing

The function BKDataNormalize expands the amplitudes of the sample frames to their maximum possible value relatively to the highest absolute amplitude.

// Normalize sample frames
BKDataNormalize (& data);

Pitch

Samples are not resampled when loaded from a file. This means, if a sample has a higher sample rate than the context, it would be played too slow. However, the BKData object has an attribute BK_SAMPLE_PITCH which can be used to tune the sample. The sample should be tuned to BK_C_4 to represent the correct note.

When a sample is attached to a track, the data object’s BK_SAMPLE_PITCH attribute overwrites the one of the track, so this attribute has to be set before.

// Tune sample
BKSetAttr (& data, BK_SAMPLE_PITCH, -0.257 * BK_FINT20_UNIT);