-
Notifications
You must be signed in to change notification settings - Fork 0
/
NrEncodeMain.cpp
156 lines (128 loc) · 3.38 KB
/
NrEncodeMain.cpp
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
/*
* file: NrEncodeMain.cpp
*
* This file is part of the NetStreamer software. This file is distributed
* under the GNU GENERAL PUBLIC LICENSE, see the accompanying COPYING file.
*
* Copyright (C) 1997 Rolf Fokkens
*
*/
#include <malloc.h>
#include <iostream>
#include "XxBase.h"
#include "XxStream.h"
#include "XxStdio.h"
#include "NrComp.h"
#include "NrRateAdjust.h"
using namespace std;
class NrEncoder;
class NrEncoder : public XxBase {
class EncIn;
class EncOut;
friend EncIn;
friend EncOut;
private:
EzString InBuf;
NrComp Comp;
NrRateAdjust Adjust;
void InEof (void);
void OutEof (void);
void GetRWFlags (int &rFlag, int &wFlag);
void HandleRead (EzString Data);
void OnBufEmpty (void);
class EncIn : public XxStdIn {
private:
NrEncoder *pEnc;
protected:
virtual void OnDisconnect (void) { pEnc->InEof (); };
virtual void HandleRead (EzString Data) { pEnc->HandleRead (Data); };
public:
EncIn (NrEncoder *pEnc) {EncIn::pEnc = pEnc; };
virtual ~EncIn (void) { };
virtual void GetRWFlags (int &rFlag, int &wFlag) {
pEnc->GetRWFlags (rFlag, wFlag);
};
};
class EncOut : public XxStdOut {
private:
NrEncoder *pEnc;
protected:
virtual void OnDisconnect (void) { pEnc->OutEof (); };
virtual void OnBufEmpty (void) { pEnc->OnBufEmpty (); };
public:
EncOut (NrEncoder *pEnc) {EncOut::pEnc = pEnc; };
virtual ~EncOut (void) { };
void Write (EzString Data) { XxStream::Write (Data); };
};
EncIn Input;
EncOut Output;
public:
NrEncoder (int InSampleRate, int OutSampleRate);
virtual ~NrEncoder (void) { };
};
NrEncoder::NrEncoder (int InSampleRate, int OutSampleRate)
: Comp (3), Adjust (InSampleRate, OutSampleRate)
, Input (this), Output (this)
{
};
void NrEncoder::InEof (void)
{
if (Output.GetOutBufSize () == 0) Remove ();
};
void NrEncoder::OutEof (void)
{
Remove ();
};
void NrEncoder::OnBufEmpty (void)
{
if (Input.GetStatus () == XxBlocker::StatClosed) Remove ();
};
void NrEncoder::GetRWFlags (int &rFlag, int &wFlag)
{
wFlag = 0;
rFlag = (Output.GetOutBufSize () < 10000);
};
void NrEncoder::HandleRead (EzString Data)
{
EzString Temp;
InBuf += Adjust.ProcessData (Data);
while (InBuf.Length () >= 2048) {
Temp = Substr (InBuf, 0, 2048);
InBuf = Substr (InBuf, 2048);
Temp = Comp.Compress (Temp);
Output.Write (Temp);
};
};
static void ShowArgErrorMessage (EzString ProgName)
{
cerr
<< "Bad parameters\n"
<< "Usage:" << endl
<< " " << ProgName
<< " InSampleRate OutSampleRate\n"
<< "Where:\n"
<< " InSampleRate : The Input SampleRate\n"
<< " e.g. 44\n"
<< " OutSampleRate : 8 or 16 (kHz)\n";
};
int main (int argc, char *argv[])
{
int InSampleRate, OutSampleRate;
if (argc < 3) {
ShowArgErrorMessage (argv[0]);
return 1;
};
InSampleRate = atoi (argv [1]);
OutSampleRate = atoi (argv [2]);
if (OutSampleRate != 8 && OutSampleRate != 16) {
ShowArgErrorMessage (argv[0]);
return 1;
};
if (InSampleRate < OutSampleRate) {
ShowArgErrorMessage (argv[0]);
return 1;
};
new NrEncoder (InSampleRate, OutSampleRate);
XxBlocker::MainLoop ();
return 0;
};