forked from applidium/Cracking-Siri
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sampledec.c
73 lines (58 loc) · 1.95 KB
/
sampledec.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
#include <speex/speex.h>
#include <stdio.h>
/*The frame size in hardcoded for this sample code but it doesn't have to be*/
#define FRAME_SIZE 320
int main(int argc, char **argv)
{
char *outFile;
FILE *fout;
/*Holds the audio that will be written to file (16 bits per sample)*/
short * out;
/*Speex handle samples as float, so we need an array of floats*/
float * output;
char cbits[4096];
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;
/*Create a new decoder state in wideband mode*/
state = speex_decoder_init(&speex_wb_mode);
outFile = argv[1];
fout = fopen(outFile, "w");
/*Initialization of the structure that holds the bits*/
speex_bits_init(&bits);
int frame_size;
speex_decoder_ctl(state, SPEEX_GET_FRAME_SIZE, &frame_size);
printf("Frame size = %d\n", frame_size);
out = malloc(sizeof(short)*frame_size);
output = malloc(sizeof(float)*frame_size);
while (1)
{
/*Read the size encoded by sampleenc, this part will likely be
different in your application*/
//fread(&nbBytes, sizeof(int), 1, stdin);
nbBytes = 695;
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);
/*Decode the data*/
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(out, sizeof(short), FRAME_SIZE, fout);
}
/*Destroy the decoder state*/
speex_decoder_destroy(state);
/*Destroy the bit-stream truct*/
speex_bits_destroy(&bits);
fclose(fout);
return 0;
}