-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspeexdec.c
203 lines (170 loc) · 4.71 KB
/
speexdec.c
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
203
#include <speex/speex.h>
#include <stdio.h>
#define HAVE_SYS_SOUNDCARD_H
#ifdef HAVE_SYS_SOUNDCARD_H
#include <sys/soundcard.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#elif defined HAVE_SYS_AUDIOIO_H
#include <sys/types.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/audioio.h>
#ifndef AUDIO_ENCODING_SLINEAR
#define AUDIO_ENCODING_SLINEAR AUDIO_ENCODING_LINEAR /* Solaris */
#endif
#endif
FILE *out_file_open(char *outFile, int rate, int *channels)
{
FILE *fout=NULL;
/*Open output file*/
if (strlen(outFile)==0)
{
#if defined HAVE_SYS_SOUNDCARD_H
int audio_fd, format, stereo;
audio_fd=open("/dev/dsp", O_WRONLY);
if (audio_fd<0)
{
perror("Cannot open /dev/dsp");
exit(1);
}
format=AFMT_S16_NE;
// format=AFMT_S16_LE;
if (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &format)==-1)
{
perror("SNDCTL_DSP_SETFMT");
close(audio_fd);
exit(1);
}
stereo=0;
if (*channels==2)
stereo=1;
if (ioctl(audio_fd, SNDCTL_DSP_STEREO, &stereo)==-1)
{
perror("SNDCTL_DSP_STEREO");
close(audio_fd);
exit(1);
}
if (stereo!=0)
{
if (*channels==1)
fprintf (stderr, "Cannot set mono mode, will decode in stereo\n");
*channels=2;
}
if (ioctl(audio_fd, SNDCTL_DSP_SPEED, &rate)==-1)
{
perror("SNDCTL_DSP_SPEED");
close(audio_fd);
exit(1);
}
fout = fdopen(audio_fd, "w");
printf ("/dev/dsp opened");
#elif defined HAVE_SYS_AUDIOIO_H
audio_info_t info;
int audio_fd;
audio_fd = open("/dev/audio", O_WRONLY);
if (audio_fd<0)
{
perror("Cannot open /dev/audio");
exit(1);
}
AUDIO_INITINFO(&info);
#ifdef AUMODE_PLAY /* NetBSD/OpenBSD */
info.mode = AUMODE_PLAY;
#endif
info.play.encoding = AUDIO_ENCODING_SLINEAR;
info.play.precision = 16;
info.play.sample_rate = rate;
info.play.channels = *channels;
if (ioctl(audio_fd, AUDIO_SETINFO, &info) < 0)
{
perror ("AUDIO_SETINFO");
exit(1);
}
fout = fdopen(audio_fd, "w");
#elif defined WIN32 || defined _WIN32
{
unsigned int speex_channels = *channels;
if (Set_WIN_Params (INVALID_FILEDESC, rate, SAMPLE_SIZE, speex_channels))
{
fprintf (stderr, "Can't access %s\n", "WAVE OUT");
exit(1);
}
}
#else
fprintf (stderr, "No soundcard support\n");
exit(1);
#endif
}
return fout;
}
/*The frame size in hardcoded for this sample code but it doesn’t have to be*/
#define FRAME_SIZE 320
#define FRAMES_PER_RTP_PACKET 21
int main(int argc, char **argv)
{
// char *outFile;
// FILE *fout;
FILE *fout=NULL;
int channels=1;
int rate=16000;
/*Holds the audio that will be written to file (16 bits per sample)*/
short out[FRAME_SIZE];
/*Speex handle samples as float, so we need an array of floats*/
short output[FRAME_SIZE];
char cbits[1500];
int nbBytes;
/*Holds the state of the decoder*/
void *state;
/*Holds bits so they can be read and written to by the Speex routines*/
SpeexBits bits;
int i, tmp;
fout = out_file_open("", rate, &channels);
/*Create a new decoder state in narrowband mode*/
state = speex_decoder_init(&speex_wb_mode);
/*Set the perceptual enhancement on*/
tmp=1;
speex_decoder_ctl(state, SPEEX_SET_ENH, &tmp);
int frame_size;
speex_decoder_ctl(state, SPEEX_GET_FRAME_SIZE, &frame_size);
// outFile = argv[1];
// fout = fopen(outFile, "w");
/*Initialization of the structure that holds the bits*/
speex_bits_init(&bits);
while (1)
{
/*Read the size encoded by sampleenc, this part will likely be
different in your application*/
fread(&nbBytes, sizeof(int), 1, stdin);
fprintf (stderr, "nbBytes: %d\n", nbBytes);
if (feof(stdin))
break;
/*Read the "packet" encoded by sampleenc*/
fread(cbits, 1, nbBytes, stdin);
/*Copy the data into the bit-stream struct*/
speex_bits_read_from(&bits, cbits, nbBytes);
int nFrames = 1;
while(nFrames<= FRAMES_PER_RTP_PACKET){
/*Decode the data*/
speex_decode_int(state, &bits, output);
// speex_decode(state, &bits, output);
/*Copy from float to short (16 bits) for output*/
// for (i=0;i<frame_size;i++)
// out[i]=output[i];
/*Write the decoded audio to file*/
fwrite(output, sizeof(short), FRAME_SIZE, fout);
fflush(fout);
nFrames++;
}
}
if (fout != NULL)
fclose(fout);
/*Destroy the decoder state*/
speex_decoder_destroy(state);
/*Destroy the bit-stream truct*/
speex_bits_destroy(&bits);
// fclose(fout);
return 0;
}