-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathShoutVSTEncoderMP3.cpp
182 lines (143 loc) · 5.17 KB
/
ShoutVSTEncoderMP3.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
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
#include <stdio.h>
#include <windows.h>
#include <tchar.h>
#include "ShoutVST.h"
#include "ShoutVSTEncoderMP3.h"
#include <shlwapi.h>
// note to self
#define STEREO 2
ShoutVSTEncoderMP3::ShoutVSTEncoderMP3( ShoutVST * p )
: ShoutVSTEncoder(p)
{
}
extern HINSTANCE hInstance;
bool ShoutVSTEncoderMP3::Preload()
{
// try 1: check in default dirs
hDLL = LoadLibrary("lame_enc.dll");
char szVstPath[MAX_PATH];
// try 2: check next to ourselves (the dll)
if (!hDLL) {
GetModuleFileName(hInstance,szVstPath,MAX_PATH);
if (strrchr(szVstPath,'\\')) {
*strrchr(szVstPath,'\\') = 0;
}
PathAppend(szVstPath,"lame_enc.dll");
hDLL = LoadLibrary(szVstPath);
}
if (!hDLL) {
HKEY hk = NULL;
// try 3: check in the defaut vst dir
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,_T("SOFTWARE\\VST"),0,KEY_READ,&hk) == ERROR_SUCCESS) {
ZeroMemory(szVstPath,MAX_PATH);
DWORD type = 0, size = MAX_PATH;
if (RegQueryValueEx(hk,_T("VSTPluginsPath"),NULL,&type,(LPBYTE)szVstPath,&size) == ERROR_SUCCESS && type != REG_SZ) {
PathAppend(szVstPath,"lame_enc.dll");
hDLL = LoadLibrary(szVstPath);
}
RegCloseKey(hk);
}
}
if (!hDLL) return false;
pVST->Log("[mp3] Lame_enc.dll found!\r\n");
beInitStream = (BEINITSTREAM) GetProcAddress(hDLL, TEXT_BEINITSTREAM);
beEncodeChunk = (BEENCODECHUNK) GetProcAddress(hDLL, TEXT_BEENCODECHUNK);
beDeinitStream = (BEDEINITSTREAM) GetProcAddress(hDLL, TEXT_BEDEINITSTREAM);
beCloseStream = (BECLOSESTREAM) GetProcAddress(hDLL, TEXT_BECLOSESTREAM);
beVersion = (BEVERSION) GetProcAddress(hDLL, TEXT_BEVERSION);
beWriteVBRHeader= (BEWRITEVBRHEADER) GetProcAddress(hDLL,TEXT_BEWRITEVBRHEADER);
beWriteInfoTag = (BEWRITEINFOTAG) GetProcAddress(hDLL,TEXT_BEWRITEINFOTAG);
if(!beInitStream || !beEncodeChunk || !beDeinitStream || !beCloseStream || !beVersion || !beWriteVBRHeader) {
pVST->Log("[mp3] Imported function from lame.dll is missing!\r\n");
return false;
}
return true;
}
bool ShoutVSTEncoderMP3::Initialize()
{
bInitialized = false;
hbeStream = NULL;
BE_VERSION v;
beVersion( &v );
pVST->Log("[mp3] lame_enc.dll version %u.%02u\r\n",v.byDLLMajorVersion, v.byDLLMinorVersion);
BE_CONFIG cfg;
memset(&cfg,0,sizeof(BE_CONFIG));
cfg.dwConfig = BE_CONFIG_LAME;
cfg.format.LHV1.dwStructVersion = 1;
cfg.format.LHV1.dwStructSize = sizeof(BE_CONFIG);
cfg.format.LHV1.dwSampleRate = (long)pVST->updateSampleRate();
cfg.format.LHV1.nMode = BE_MP3_MODE_JSTEREO;
cfg.format.LHV1.dwMaxBitrate = 256;
cfg.format.LHV1.bWriteVBRHeader = TRUE;
cfg.format.LHV1.bEnableVBR = TRUE;
cfg.format.LHV1.nVBRQuality = min(10 - pVST->GetQuality(), 9);
/*
cfg.format.LHV1.nPreset = LQP_R3MIX;
cfg.format.LHV1.dwMpegVersion = MPEG1;
cfg.format.LHV1.bOriginal = TRUE;
*/
if(beInitStream(&cfg, &dwSamples, &dwMP3Buffer, &hbeStream) != BE_ERR_SUCCESSFUL)
{
pVST->Log("[mp3] Error opening encoding stream\r\n");
return false;
}
pMP3Buffer = new BYTE[dwMP3Buffer];
pWAVBuffer = new SHORT[dwSamples];
dwSamplesSoFar = 0;
dwSamples /= STEREO;
bInitialized = true;
return true;
}
bool ShoutVSTEncoderMP3::Close()
{
bInitialized = false;
DWORD dwWrite = 0;
if(beDeinitStream(hbeStream, pMP3Buffer, &dwWrite) != BE_ERR_SUCCESSFUL)
{
beCloseStream(hbeStream);
pVST->Log("[mp3] Error in beDeinitStream\r\n");
return false;
}
pVST->SendDataToICE(pMP3Buffer,dwWrite);
delete[] pWAVBuffer;
delete[] pMP3Buffer;
return true;
}
bool ShoutVSTEncoderMP3::Process( float **inputs, long sampleFrames )
{
if (!bInitialized) return false;
int nSamplesToProcess = sampleFrames;
int nInputPointer = 0;
while (nSamplesToProcess > 0) {
int nSampleRoom = dwSamples - dwSamplesSoFar;
int nSamplesProcessed = min(nSampleRoom,nSamplesToProcess);
SHORT * p = pWAVBuffer + dwSamplesSoFar * STEREO;
for (int i=0; i < nSamplesProcessed; i++) {
*(p++) = (SHORT)(min(1.0f,max(-1.0f,inputs[0][nInputPointer])) * 32767.0f);
*(p++) = (SHORT)(min(1.0f,max(-1.0f,inputs[1][nInputPointer])) * 32767.0f);
nInputPointer++;
}
dwSamplesSoFar += nSamplesProcessed;
if (dwSamplesSoFar == dwSamples) {
DWORD dwWrite = 0;
if(beEncodeChunk(hbeStream, dwSamples * STEREO, pWAVBuffer, pMP3Buffer, &dwWrite) != BE_ERR_SUCCESSFUL)
{
beCloseStream(hbeStream);
pVST->Log("[mp3] Error in beDeinitStream\r\n");
return false;
}
/*
DWORD dw = 0;
HANDLE hLogFile = CreateFile( "dump.raw", GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, NULL, NULL);
SetFilePointer(hLogFile,0,NULL,FILE_END);
WriteFile(hLogFile,pWAVBuffer,dwSamples * STEREO * sizeof(SHORT),&dw,NULL);
CloseHandle(hLogFile);
*/
//pVST->Log("[mp3] Send data: %d\r\n",dwWrite);
if (!pVST->SendDataToICE(pMP3Buffer,dwWrite)) return false;
dwSamplesSoFar = 0;
}
nSamplesToProcess -= nSamplesProcessed;
}
return true;
}