-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMpgRead.cpp
More file actions
120 lines (96 loc) · 3.03 KB
/
CMpgRead.cpp
File metadata and controls
120 lines (96 loc) · 3.03 KB
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
#include "CMpgRead.h"
#include "CWavRW.h"
CMpgRead::CMpgRead(const char* inFilePath, const uint32_t blockLen)
{
int errorHolder = MPG123_OK;
noError = true;
isInitialized = false;
currentMessage = "\nAudio decoding messages:\n";
this->blockLen = blockLen;
mpgHandle = NULL;
normFact = 1.0 / ((double) 0x8000);
bufferSize = 0;
nChans = 0;
errorHolder = mpg123_init();
if(errorHolder!=MPG123_OK)
{
currentMessage.append("Couldn't initialize mpg123 instance.\n");
currentMessage.append(mpg123_plain_strerror(errorHolder));
noError = false;
}
mpgHandle = mpg123_new(NULL, &errorHolder);
if(errorHolder!=MPG123_OK && noError)
{
currentMessage.append("Couldn't create new mpg123 handle.\n");
currentMessage.append(mpg123_plain_strerror(errorHolder));
noError = false;
}
errorHolder = mpg123_open(mpgHandle, inFilePath);
if(errorHolder!=MPG123_OK && noError)
{
currentMessage.append("Error when try to open the data stream.\n");
currentMessage.append(mpg123_strerror(mpgHandle));
noError = false;
}
errorHolder = mpg123_getformat(mpgHandle, &samplingRate, &nChans, &encodingID);
if(errorHolder!=MPG123_OK && noError)
{
currentMessage.append("Error when try to get the standart decode format.\n");
currentMessage.append(mpg123_strerror(mpgHandle));
noError = false;
}
if (noError)
{
bufferSize = blockLen*nChans*sizeof(int16_t);
outBuf = new int16_t[blockLen*nChans]();
nSamps = mpg123_length(mpgHandle);
isInitialized = true;
currentMessage.append("Decoder initialized.\nCurrent sampling rate: ");
currentMessage.append(std::to_string(samplingRate));
currentMessage.append("\nNumber of channels: ");
currentMessage.append(std::to_string(nChans));
}
}
int CMpgRead::decodeWholeFile(const char *outFilePath)
{
if (noError)
{
double outBufDouble[blockLen*nChans];
int errorHolder = MPG123_OK;
size_t nBytesDecoded;
CWavWrite wavOutput(outFilePath, blockLen, samplingRate, nChans, 16);
while (errorHolder==MPG123_OK)
{
errorHolder = mpg123_read(mpgHandle, (unsigned char*) outBuf, bufferSize, &nBytesDecoded);
fixedToFloat64(outBuf, outBufDouble, blockLen*nChans);
wavOutput.writeOneBlock(outBufDouble);
}
return 0;
}
return -1;
}
int CMpgRead::readOneBlock(double *outBufDouble)
{
size_t nBytesDecoded;
int errorHolder;
errorHolder = mpg123_read(mpgHandle, (unsigned char*) outBuf, bufferSize, &nBytesDecoded);
if (errorHolder == MPG123_OK)
{
fixedToFloat64(outBuf, outBufDouble, blockLen*nChans);
}
else if (errorHolder == MPG123_DONE)
{
emit isEOF();
}
return nBytesDecoded;
}
CMpgRead::~CMpgRead()
{
if (isInitialized)
{
delete[] outBuf;
}
mpg123_close(mpgHandle);
mpg123_delete(mpgHandle);
mpg123_exit();
}