.qc (dot qc) - the group for quake c coders of all denominations. If you make quake one mods and write code in quake c, join our group! I'll try to answer any quake c questions - please post your inquiry in the forums...

Forum Thread
  Posts  
Using audio in Quake C (Groups : qc : Forum : vault() : Using audio in Quake C) Locked
Thread Options
numbersix
numbersix quake-c coder++
Jun 18 2014 Anchor

What is audio?

Reference for wave: En.wikipedia.org and ogg: En.wikipedia.org

Audio files are uncompressed or compressed digital sound storage.
This includes both sound effects and music.

The original quake and glquake used wav files for effects and CD tracks for background music.

Advanced engines like darkplaces ( Icculus.org ) support wav and ogg.

Additionally ogg tracks can substitute for CD audio by directory and file naming convention: "{quake root}/sound/cdtracks/track{number}.ogg" where "{number}" is the CD track to over lay and such files must be located in "sound/cdtracks".
Darkplaces will treat these exactly like CD tracks and the cd console command applies to them:
"cd: execute a CD drive command (cd on/off/reset/remap/close/play/loop/stop/pause/resume/eject/info) - use cd by itself for usage"


There are 3 major components to using audio in Quake.

1. Record the sound
2. Load the sound
3. Play the sound

Recording

This task I leave to you - for instruction try a search: Ixquick.com
The two tools I use the most are:
- cool edit 96 (runs under wine) Oldapps.com
- Audacity: Audacity.sourceforge.net

Loading

To load audio in engines prior to darkplaces you need this function:
Note: this does not apply to the darkplaces ogg cdtrack emulation.

// in defs.qc
string (string s) precache_sound = #19;

This is an engine builtin that can only be called during map load.
You have to call this function to load audio in any traditional quake engine.

For map loaded sounds (dynamic or static) this call can be made in the load function.
For all other sounds played in the game, it must be done similar to the other precache found in worldspawn().

Again, darkplaces and similar engines do not require this function call:
SV_SoundIndex("chaos/crate1a.wav"): not precached (fix your code), precaching anyway

Playing

You have two function calls to play sound:

// in defs.qc
void(entity e, float chan, string samp, float vol, float atten) sound = #8;
void(vector pos, string samp, float vol, float atten) ambientsound = #74;

Sound function 8 emits sound from the specified entity "e" on channel "chan".
Ambient sound is emitted from a location.

A sound is referred to with {path}/{audiofilename} - i.e. "sound/ogre/ogdrag.wav "

There are some caveats on playing audio that I will track down and append to the article when I find them.


Quake C manual ver 3.4 entries for audio:

8.3 Sound emission

Function: sound

void sound (entity source, float channel, string sample, float volume, float attenuation)

source = entity emiting the sound (ex: self)
channel = channel to use for sound
sample = name of the sample WAV file (ex: "ogre/ogdrag.wav")
volume = 0.0 for low volume, 1.0 for maximum volume
attenuation= attenuation of sound
The entity emits a sound, on one of it's 8 channels.

Function: ambientsound

void ambientsound(vector position, string sample, float volume, float attenuation)

position = position, in 3D space, inside the level sample = name of the sample WAV file (ex: "ogre/ogdrag.wav")
volume = 0.0 for low volume, 1.0 for maximum volume
attenuation = attenuation of sound
An ambient sound is emited, from the given position.

5.2 Values : Sound Channel of entities

CHAN_AUTO = 0; // Create a new sound
CHAN_WEAPON = 1; // Replace entitie's weapon noise
CHAN_VOICE = 2; // Replace entitie's voice
CHAN_ITEM = 3; // Replace entitie's item noise
CHAN_BODY = 4; // Replace entitie's body noise

Those values are meant to be used with the function sound.

5.3 Values : Sound Attenuation

ATTN_NONE = 0; // full volume everywhere in the level
ATTN_NORM = 1; // normal
ATTN_IDLE = 2; // [FIXME]
ATTN_STATIC = 3; // [FIXME]

8.12 Precaching files

Those functions are used to declare models, sounds and stuff, before the PAK file is built.

Just follow this rule: whenever one of your functions makes use of a file that's not defined in Quake, precache this file in a function that will be called by worldspawn().

Then, the QCC compiler can automatically include in the PAK file all the files that you really need to run your programs.
(Hint: not necessary)

And when the level starts running, those precache orders will be executed, so as to attribute a fixed table index to all those files.

DO NOT USE those functions in code that will be called after worldspawn() was called. As a matter of fact, that could bomb Quake (restarting the level, without crashing the game).

Files can only be precached in spawn functions.

Function: precache_file

void precache_file(string file)
file = name of the file to include in PAK file.

Does nothing during game play.

Function: precache_model

void precache_model(string file)
file = name of the MDL or BSP file to include in PAK file.

Does nothing during game play.

Must be used in a model's spawn function, to declare the model file.

Function: precache_sound

void precache_sound(string file)
file = name of the WAV file to include in PAK file.

Does nothing during game play.

Must be used in a model's spawn function, to declare the sound files.

Edited by: numbersix

Jun 18 2014 Anchor

+you can use this in darkplaces or another engin (for your own music player maybe)

WriteByte (MSG_ALL, SVC_CDTRACK);   
WriteByte (MSG_ALL, 1);  //track number
 WriteByte (MSG_ALL, 0); //loop

numbersix
numbersix quake-c coder++
Jun 18 2014 Anchor

I've decided to make this and using models a full tutorial. I'll include that.
Never really did a deep analysis of all the MSG instructions.

--

\|/
-*- Support free code: Patreon.com
/|\

Reply to thread
click to sign in and post

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.