summaryrefslogtreecommitdiff
path: root/engine/audio/public/voice.h
blob: 2bfea8158b319cd363c32bbc6b72cdbf78bde978 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//

#ifndef VOICE_H
#define VOICE_H
#pragma once


#include "ivoicetweak.h"


/*! @defgroup Voice Voice
Defines the engine's interface to the voice code.
@{
*/

// Voice_Init will pick a sample rate, it must be within RATE_MAX
#define VOICE_OUTPUT_SAMPLE_RATE_LOW		11025	// Sample rate that we feed to the mixer.
#define VOICE_OUTPUT_SAMPLE_RATE_HIGH		22050	// Sample rate that we feed to the mixer.
#define VOICE_OUTPUT_SAMPLE_RATE_MAX		22050	// Sample rate that we feed to the mixer.


//! Returned on error from certain voice functions.
#define VOICE_CHANNEL_ERROR			-1
#define VOICE_CHANNEL_IN_TWEAK_MODE	-2	// Returned by AssignChannel if currently in tweak mode (not an error).


//! Initialize the voice code.
bool Voice_Init( const char *pCodec, int nSampleRate );

//! Inits voice with defaults if it is not initialized normally, e.g. for local mixer use.
void Voice_ForceInit();

//! Get the default sample rate to use for this codec
inline int Voice_GetDefaultSampleRate( const char *pCodec ) // Inline for DEDICATED builds
{
	// Use legacy lower rate for speex
	if ( Q_stricmp( pCodec, "vaudio_speex" ) == 0 )
	{
		return VOICE_OUTPUT_SAMPLE_RATE_LOW;
	}
	else if ( Q_stricmp( pCodec, "steam" ) == 0 )
	{
		return 0; // For the steam codec, 0 passed to voice_init means "use optimal steam voice rate"
	}

	// Use high sample rate by default for other codecs.
	return VOICE_OUTPUT_SAMPLE_RATE_HIGH;
}

//! Shutdown the voice code.
void Voice_Deinit();

//! Returns true if the client has voice enabled
bool Voice_Enabled( void );

//! The codec voice was initialized with. Empty string if voice is not initialized.
const char *Voice_ConfiguredCodec();

//! The sample rate voice was initialized with. -1 if voice is not initialized.
int Voice_ConfiguredSampleRate();

//! Returns true if the user can hear themself speak.
bool Voice_GetLoopback();


//! This is called periodically by the engine when the server acks the local player talking.
//! This tells the client DLL that the local player is talking and fades after about 200ms.
void Voice_LocalPlayerTalkingAck();


//! Call every frame to update the voice stuff.
void Voice_Idle(float frametime);


//! Returns true if mic input is currently being recorded.
bool Voice_IsRecording();

//! Begin recording input from the mic.
bool Voice_RecordStart(
	//! Filename to store incoming mic data, or NULL if none.
	const char *pUncompressedFile,	
	
	//! Filename to store the output of compression and decompressiong with the codec, or NULL if none.
	const char *pDecompressedFile,	
	
	//! If this is non-null, the voice manager will use this file for input instead of the mic.
	const char *pMicInputFile		
	);

// User wants to stop recording
void Voice_UserDesiresStop();

//! Stop recording from the mic.
bool Voice_RecordStop();


//! Get the most recent N bytes of compressed data. If nCount is less than the number of
//! available bytes, it discards the first bytes and gives you the last ones.
//! Set bFinal to true on the last call to this (it will flush out any stored voice data).
int Voice_GetCompressedData(char *pchData, int nCount, bool bFinal);



//! Pass incoming data from the server into here.
//! The data should have been compressed and gotten through a Voice_GetCompressedData call.
int Voice_AddIncomingData(
	//! Channel index.
	int nChannel, 
	//! Compressed data to add to the channel.
	const char *pchData, 
	//! Number of bytes in pchData.
	int nCount,
	//! Sequence number. If a packet is missed, it adds padding so the time isn't squashed.
	int iSequenceNumber
	);

//! Call this to reserve a voice channel for the specified entity to talk into.
//! \return A channel index for use with Voice_AddIncomingData or VOICE_CHANNEL_ERROR on error.
int Voice_AssignChannel(int nEntity, bool bProximity );

//! Call this to get the channel index that the specified entity is talking into.
//! \return A channel index for use with Voice_AddIncomingData or VOICE_CHANNEL_ERROR if the entity isn't talking.
int Voice_GetChannel(int nEntity);

#if !defined( NO_VOICE )
extern IVoiceTweak g_VoiceTweakAPI;
extern bool g_bUsingSteamVoice;
#endif

/*! @} */


#endif // VOICE_H