summaryrefslogtreecommitdiff
path: root/engine/voice_codecs/speex/VoiceEncoder_Speex.cpp
blob: 87516186fa412d3a87554912cffaa936eae79e84 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//
//=============================================================================//

/* This product contains Speex software.  The license terms of the Speex
software, distributed with this product, are as follows:

� 2002-2003, Jean-Marc Valin/Xiph.Org Foundation

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or
other materials provided with the distribution.

Neither the name of the Xiph.org Foundation nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.

This software is provided by the copyright holders and contributors "as is"
and any express or implied warranties, including, but not limited to, the
implied warranties of merchantability and fitness for a particular purpose
are disclaimed. In no event shall the foundation or contributors be liable
for any direct, indirect, incidental, special, exemplary, or consequential
damages (including, but not limited to, procurement of substitute goods or
services; loss of use, data, or profits; or business interruption) however
caused and on any theory of liability, whether in contract, strict
liability, or tort (including negligence or otherwise) arising in any way
out of the use of this software, even if advised of the possibility of such
damage. */

#include "ivoicecodec.h"
#include "VoiceEncoder_Speex.h"
#include <stdio.h>

#define SAMPLERATE			8000	// get 8000 samples/sec
#define RAW_FRAME_SIZE		160		// in 160 samples per frame

// each quality has a differnt farme size
const int ENCODED_FRAME_SIZE [11] = {6,6,15,15,20,20,28,28,38,38,38};	

/* useful Speex voice qualities are 0,2,4,6 and 8. each quality level
   has a diffrent encoded frame size and needed bitrate:

	Quality 0 :  6 bytes/frame,  2400bps
	Quality 2 : 15 bytes/frame,  6000bps
	Quality 4 : 20 bytes/frame,  8000bps
	Quality 6 : 28 bytes/frame, 11200bps
	Quality 8 : 38 bytes/frame, 15200bps */


extern IVoiceCodec* CreateVoiceCodec_Frame(IFrameEncoder *pEncoder);

void* CreateSpeexVoiceCodec()
{
	IFrameEncoder *pEncoder = new VoiceEncoder_Speex;
	return CreateVoiceCodec_Frame( pEncoder );
}

EXPOSE_INTERFACE_FN(CreateSpeexVoiceCodec, IVoiceCodec, "vaudio_speex")

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

VoiceEncoder_Speex::VoiceEncoder_Speex()
{
	m_EncoderState = NULL;
	m_DecoderState = NULL;
	m_Quality = 0;
}

VoiceEncoder_Speex::~VoiceEncoder_Speex()
{
	TermStates();
}

bool VoiceEncoder_Speex::Init(int quality, int &rawFrameSize, int &encodedFrameSize)
{
	if ( !InitStates() )
		return false;

	rawFrameSize = RAW_FRAME_SIZE * BYTES_PER_SAMPLE;

	// map gerneral voice quality 1-5 to speex quality levels
	switch ( quality )
	{
		case 1 :	m_Quality = 0; break;
		case 2 :	m_Quality = 2; break;
		case 3 :	m_Quality = 4; break;
		case 4 :	m_Quality = 6; break;
		case 5 : 	m_Quality = 8; break;
		default	:	m_Quality = 0; break;
	}

	encodedFrameSize = ENCODED_FRAME_SIZE[m_Quality];

	speex_encoder_ctl( m_EncoderState, SPEEX_SET_QUALITY, &m_Quality);
	speex_decoder_ctl( m_DecoderState, SPEEX_SET_QUALITY, &m_Quality);

	int postfilter = 1; // Set the perceptual enhancement on
	speex_decoder_ctl( m_DecoderState, SPEEX_SET_ENH, &postfilter);

	int samplerate = SAMPLERATE;
	speex_decoder_ctl( m_DecoderState, SPEEX_SET_SAMPLING_RATE, &samplerate );
	speex_encoder_ctl( m_EncoderState, SPEEX_SET_SAMPLING_RATE, &samplerate );

	return true;
}

void VoiceEncoder_Speex::Release()
{
	delete this;
}

void VoiceEncoder_Speex::EncodeFrame(const char *pUncompressedBytes, char *pCompressed)
{
	float input[RAW_FRAME_SIZE];
	short * in = (short*)pUncompressedBytes;

	/*Copy the 16 bits values to float so Speex can work on them*/
	for (int i=0;i<RAW_FRAME_SIZE;i++)
	{
		input[i]=(float)*in;
		in++;
	}

	/*Flush all the bits in the struct so we can encode a new frame*/
	speex_bits_reset( &m_Bits );

	/*Encode the frame*/
	speex_encode( m_EncoderState, input, &m_Bits );

	/*Copy the bits to an array of char that can be written*/
	int size = speex_bits_write(&m_Bits, pCompressed, ENCODED_FRAME_SIZE[m_Quality] );
	NOTE_UNUSED( size );

	// char text[255];	_snprintf(text, 255, "outsize %i,", size ); OutputDebugStr( text );
}

void VoiceEncoder_Speex::DecodeFrame(const char *pCompressed, char *pDecompressedBytes)
{
	float output[RAW_FRAME_SIZE];
	short * out = (short*)pDecompressedBytes;

	/*Copy the data into the bit-stream struct*/
	speex_bits_read_from(&m_Bits, (char *)pCompressed, ENCODED_FRAME_SIZE[m_Quality] );

	/*Decode the data*/
	speex_decode(m_DecoderState, &m_Bits, output);
	
	/*Copy from float to short (16 bits) for output*/
	for (int i=0;i<RAW_FRAME_SIZE;i++)
	{
		*out = (short)output[i];
		out++;
	}
}

bool VoiceEncoder_Speex::ResetState()
{
	speex_encoder_ctl(m_EncoderState, SPEEX_RESET_STATE , NULL );
	speex_decoder_ctl(m_DecoderState, SPEEX_RESET_STATE , NULL );
	return true;
}

bool VoiceEncoder_Speex::InitStates()
{
	speex_bits_init(&m_Bits);
	
	m_EncoderState = speex_encoder_init( &speex_nb_mode );	// narrow band mode 8kbp

	m_DecoderState = speex_decoder_init( &speex_nb_mode );
	
	return m_EncoderState && m_DecoderState;
}

void VoiceEncoder_Speex::TermStates()
{
	if(m_EncoderState)
	{
		speex_encoder_destroy( m_EncoderState );
		m_EncoderState = NULL;
	}

	if(m_DecoderState)
	{
		speex_decoder_destroy( m_DecoderState );
		m_DecoderState = NULL;
	}

	speex_bits_destroy( &m_Bits );
}