-
Notifications
You must be signed in to change notification settings - Fork 17
/
ldacdec.c
114 lines (96 loc) · 2.64 KB
/
ldacdec.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
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <math.h>
#include <float.h>
#include <string.h>
#include "ldacdec.h"
#include "sndfile.h"
SNDFILE *openAudioFile( const char *fileName, int freq, int channels )
{
SF_INFO sfinfo = {
.samplerate = freq,
.channels = channels,
.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16,
};
printf("opening \"%s\" for writing ...\n", fileName );
printf("sampling frequency: %d\n", freq );
printf("channel count: %d\n", channels );
SNDFILE *out = sf_open( fileName, SFM_WRITE, &sfinfo );
if( out == NULL )
{
printf("can't open output: %s\n", sf_strerror( NULL ) );
return NULL;
}
return out;
}
#define BUFFER_SIZE (680*2)
#define PCM_BUFFER_SIZE (256*2)
int main(int argc, char *args[] )
{
if( argc < 2 )
{
printf("usage:\n\t%s <input> <output, optional>\n", args[0] );
return EXIT_SUCCESS;
}
const char *inputFile = args[1];
const char *audioFile = "output.wav";
if( argc > 2 )
audioFile = args[2];
printf("opening \"%s\" ...\n", inputFile );
ldacdec_t dec;
ldacdecInit( &dec );
FILE *in = fopen( inputFile, "rb" );
if( in == NULL )
{
perror("can't open stream file");
return EXIT_FAILURE;
}
SNDFILE *out = NULL;
uint8_t buf[BUFFER_SIZE];
uint8_t *ptr = NULL;
int16_t pcm[PCM_BUFFER_SIZE] = { 0 };
size_t filePosition = 0;
int blockId = 0;
int bytesInBuffer = 0;
while(1)
{
LOG("%ld =>\n", filePosition );
int ret = fseek( in, filePosition, SEEK_SET );
if( ret < 0 )
break;
bytesInBuffer = fread( buf, 1, BUFFER_SIZE, in );
ptr = buf;
if( bytesInBuffer == 0 )
break;
#if 1
if(ptr[1] == 0xAA)
{
ptr++;
filePosition++;
}
#endif
LOG("sync: %02x\n", ptr[0] );
int bytesUsed = 0;
memset( pcm, 0, sizeof(pcm) );
LOG("count === %4d ===\n", blockId++ );
ret = ldacDecode( &dec, ptr, pcm, &bytesUsed );
if( ret < 0 )
break;
LOG_ARRAY( pcm, "%4d, " );
if( out == NULL )
{
printf("auto detect format!\n");
out = openAudioFile( audioFile, ldacdecGetSampleRate( &dec ), ldacdecGetChannelCount( &dec ) );
if( out == NULL )
return EXIT_FAILURE;
}
sf_writef_short( out, pcm, dec.frame.frameSamples );
filePosition += bytesUsed;
}
printf("done.\n");
sf_close( out );
fclose(in);
return EXIT_SUCCESS;
}