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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
| /*
BASSenc recording example
Copyright (c) 2003-2011 Un4seen Developments Ltd.
*/
#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include "bass.h"
#include "bassenc.h"
HWND win;
#define FREQ 44100
#define CHANS 2
int input; // current input source
int encoder; // current encoder
WAVEFORMATEX *acmform=0; // ACM codec format
DWORD acmformlen; // ACM codec format size
HRECORD rchan=0; // recording channel
HSTREAM chan=0; // playback channel
HENCODE hencode; // encoder handle
// encoder command-lines and output files
const char *commands[2]={
"oggenc -o bass.ogg -", // oggenc (OGG)
"lame --alt-preset standard - bass.mp3" // lame (MP3)
};
const char *files[3]={"bass.ogg","bass.mp3","bass.wav"}; // OGG,MP3,ACM
// display error messages
void Error(const char *es)
{
char mes[200];
sprintf(mes,"%s\n(error code: %d)",es,BASS_ErrorGetCode());
MessageBox(win,mes,0,0);
}
// messaging macros
#define MESS(id,m,w,l) SendDlgItemMessage(win,id,m,(WPARAM)(w),(LPARAM)(l))
#define DLGITEM(id) GetDlgItem(win,id)
BOOL CALLBACK RecordingCallback(HRECORD handle, const void *buffer, DWORD length, void *user)
{
return BASS_Encode_IsActive(handle); // continue recording if encoder is alive
}
void StartRecording()
{
if (chan) { // free old recording
BASS_StreamFree(chan);
chan=0;
EnableWindow(DLGITEM(11),FALSE);
// close output device before recording incase of half-duplex device
BASS_Free();
}
// start recording (paused to add encoder first)
rchan=BASS_RecordStart(FREQ,CHANS,BASS_RECORD_PAUSE,&RecordingCallback,0);
if (!rchan) {
Error("Couldn't start recording");
return;
}
// get selected encoder (0=OGG, 1=MP3, 2=ACM)
if (MESS(18,BM_GETCHECK,0,0)) encoder=2;
else encoder=MESS(17,BM_GETCHECK,0,0);
if (encoder==2) {
if (!BASS_Encode_GetACMFormat(rchan,acmform,acmformlen,(char*)NULL,BASS_ACM_DEFAULT) // select the ACM codec
|| !(hencode=BASS_Encode_StartACMFile(rchan,acmform,0,files[2]))) { // start the ACM encoder
Error("Couldn't start encoding");
BASS_ChannelStop(rchan);
rchan=0;
return;
}
} else if (!(hencode=BASS_Encode_Start(rchan,commands[encoder],0,NULL,0))) { // start the OGG/MP3 encoder
Error("Couldn't start encoding...\n"
"Make sure OGGENC.EXE (if encoding to OGG) is in the same\n"
"direcory as this example, or LAME.EXE (if encoding to MP3).");
BASS_ChannelStop(rchan);
rchan=0;
return;
}
BASS_ChannelPlay(rchan,FALSE); // resume recoding
MESS(10,WM_SETTEXT,0,"Stop");
EnableWindow(DLGITEM(16),FALSE);
EnableWindow(DLGITEM(17),FALSE);
EnableWindow(DLGITEM(18),FALSE);
}
void StopRecording()
{
// stop recording & encoding
BASS_ChannelStop(rchan);
rchan=0;
BASS_Encode_Stop(hencode);
MESS(10,WM_SETTEXT,0,"Record");
EnableWindow(DLGITEM(16),TRUE);
EnableWindow(DLGITEM(17),TRUE);
EnableWindow(DLGITEM(18),TRUE);
// setup output device (using default device)
if (!BASS_Init(-1,44100,0,win,NULL)) {
Error("Can't initialize output device");
return;
}
// create a stream from the recording
if (chan=BASS_StreamCreateFile(FALSE,files[encoder],0,0,0))
EnableWindow(DLGITEM(11),TRUE); // enable "play" button
else
BASS_Free();
}
void UpdateInputInfo()
{
char *type;
float level;
int it=BASS_RecordGetInput(input,&level); // get info on the input
MESS(14,TBM_SETPOS,TRUE,level*100); // set the level slider
switch (it&BASS_INPUT_TYPE_MASK) {
case BASS_INPUT_TYPE_DIGITAL:
type="digital";
break;
case BASS_INPUT_TYPE_LINE:
type="line-in";
break;
case BASS_INPUT_TYPE_MIC:
type="microphone";
break;
case BASS_INPUT_TYPE_SYNTH:
type="midi synth";
break;
case BASS_INPUT_TYPE_CD:
type="analog cd";
break;
case BASS_INPUT_TYPE_PHONE:
type="telephone";
break;
case BASS_INPUT_TYPE_SPEAKER:
type="pc speaker";
break;
case BASS_INPUT_TYPE_WAVE:
type="wave/pcm";
break;
case BASS_INPUT_TYPE_AUX:
type="aux";
break;
case BASS_INPUT_TYPE_ANALOG:
type="analog";
break;
default:
type="undefined";
}
MESS(15,WM_SETTEXT,0,type); // display the type
}
BOOL InitDevice(int device)
{
BASS_RecordFree(); // free current device (and recording channel) if there is one
// initalize new device
if (!BASS_RecordInit(device)) {
Error("Can't initialize recording device");
return FALSE;
}
{ // get list of inputs
int c;
const char *i;
MESS(13,CB_RESETCONTENT,0,0);
for (c=0;i=BASS_RecordGetInputName(c);c++) {
MESS(13,CB_ADDSTRING,0,i);
if (!(BASS_RecordGetInput(c,NULL)&BASS_INPUT_OFF)) { // this one is currently "on"
input=c;
MESS(13,CB_SETCURSEL,input,0);
UpdateInputInfo();
}
}
}
return TRUE;
}
BOOL CALLBACK dialogproc(HWND h,UINT m,WPARAM w,LPARAM l)
{
switch (m) {
case WM_TIMER:
{ // update the recording/playback counter
char text[30]="";
if (rchan) // recording/encoding
sprintf(text,"%I64d",BASS_Encode_GetCount(hencode,BASS_ENCODE_COUNT_IN));
else if (chan) {
if (BASS_ChannelIsActive(chan)) // playing
sprintf(text,"%I64d / %I64d",BASS_ChannelGetPosition(chan,BASS_POS_BYTE),BASS_ChannelGetLength(chan,BASS_POS_BYTE));
else
sprintf(text,"%I64d",BASS_ChannelGetLength(chan,BASS_POS_BYTE));
}
MESS(20,WM_SETTEXT,0,text);
}
break;
case WM_COMMAND:
switch (LOWORD(w)) {
case IDCANCEL:
DestroyWindow(h);
break;
case 10:
if (!rchan)
StartRecording();
else
StopRecording();
break;
case 11:
BASS_ChannelPlay(chan,TRUE); // play the recorded data
break;
case 13:
if (HIWORD(w)==CBN_SELCHANGE) { // input selection changed
int i;
input=MESS(13,CB_GETCURSEL,0,0); // get the selection
// enable the selected input
for (i=0;BASS_RecordSetInput(i,BASS_INPUT_OFF,-1);i++) ; // 1st disable all inputs, then...
BASS_RecordSetInput(input,BASS_INPUT_ON,-1); // enable the selected
UpdateInputInfo(); // update info
}
break;
case 19:
if (HIWORD(w)==CBN_SELCHANGE) { // device selection changed
int i=MESS(19,CB_GETCURSEL,0,0); // get the selection
// initialize the selected device
if (InitDevice(i)) {
if (rchan) { // continue recording on the new device...
HRECORD newrchan=BASS_RecordStart(FREQ,CHANS,0,RecordingCallback,0);
if (!newrchan)
Error("Couldn't start recording");
else {
rchan=newrchan;
BASS_Encode_SetChannel(hencode,rchan); // move the encoder to the new recording
}
}
}
}
break;
}
break;
case WM_HSCROLL:
if (l) { // set input source level
float level=SendMessage((HWND)l,TBM_GETPOS,0,0)/100.f;
if (!BASS_RecordSetInput(input,0,level)) // failed to set input level
BASS_RecordSetInput(-1,0,level); // try master level instead
}
break;
case WM_INITDIALOG:
win=h;
MESS(14,TBM_SETRANGE,FALSE,MAKELONG(0,100));
MESS(16,BM_SETCHECK,BST_CHECKED,0);
{ // get list of recording devices
int c,def;
BASS_DEVICEINFO di;
for (c=0;BASS_RecordGetDeviceInfo(c,&di);c++) {
MESS(19,CB_ADDSTRING,0,di.name);
if (di.flags&BASS_DEVICE_DEFAULT) { // got the default device
MESS(19,CB_SETCURSEL,c,0);
def=c;
}
}
InitDevice(def); // initialize default recording device
}
SetTimer(h,0,200,0); // timer to update the position display
// allocate ACM format buffer, using suggested buffer size
acmformlen=BASS_Encode_GetACMFormat(0,NULL,0,(char*)NULL,0);
acmform=(WAVEFORMATEX*)malloc(acmformlen);
memset(acmform,0,acmformlen);
return 1;
case WM_DESTROY:
// release all BASS stuff
BASS_RecordFree();
BASS_Free();
free(acmform); // free ACM format buffer
break;
}
return 0;
}
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{
// check the correct BASS was loaded
if (HIWORD(BASS_GetVersion())!=BASSVERSION) {
MessageBox(0,"An incorrect version of BASS.DLL was loaded",0,MB_ICONERROR);
return 0;
}
{ // enable trackbar support (for the level control)
INITCOMMONCONTROLSEX cc={sizeof(cc),ICC_BAR_CLASSES};
InitCommonControlsEx(&cc);
}
DialogBox(hInstance,(char*)1000,0,&dialogproc);
return 0;
} |
Partager