config.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. #include <windows.h>
  2. // LGIVEN Mods 4-10-05
  3. #include "main.h"
  4. #include <wmsdk.h>
  5. #include "../nu/AutoWide.h"
  6. #include "../nu/ns_wc.h"
  7. #include "../Agave/Language/api_language.h"
  8. #include <MMSystem.h>
  9. #include <assert.h>
  10. #define MAX_PASSES 1 // limited to 1pass encoding until we work out some code for 2pass encoding
  11. // LGIVEN Mods 4-10-05
  12. void readconfig(char *configfile, configtype *cfg)
  13. {
  14. cfg->config_bitrate = 0;
  15. cfg->config_bitsSample = 0;
  16. cfg->config_nch = 0;
  17. cfg->config_samplesSec = 0;
  18. cfg->config_encoder = 0;
  19. cfg->config_vbr = 0;
  20. cfg->config_passes = 1;
  21. if (configfile)
  22. {
  23. GetPrivateProfileStructA("audio_wma", "conf", cfg, sizeof(configtype), configfile);
  24. }
  25. }
  26. void writeconfig(char *configfile, configtype *cfg)
  27. {
  28. if (configfile)
  29. {
  30. WritePrivateProfileStructA("audio_wma", "conf", cfg, sizeof(configtype), configfile);
  31. }
  32. }
  33. // New global table for channels,samplerates and bitrates
  34. static EncoderType* encs = NULL; // Pointer to the TABLE with all config data
  35. // Globals store current selections from Config Dialog
  36. // Number of encoders
  37. static int encNumbs = 0; // Total number of encoders installed WMA
  38. // New routine to read all config info from WMA encoder and load tables
  39. static BOOL loadWMATables()
  40. {
  41. IWMProfileManager *profileManager;
  42. IWMProfileManager2 *profileManager2;
  43. IWMCodecInfo3 *codecInfo;
  44. WAVEFORMATEX *pwave;
  45. HRESULT hr;
  46. int legalFormats = 0;
  47. WMCreateProfileManager(&profileManager);
  48. profileManager->QueryInterface(&profileManager2);
  49. profileManager2->SetSystemProfileVersion(WMT_VER_9_0);
  50. profileManager->QueryInterface(&codecInfo);
  51. // Get the number of AUDIO Codecs
  52. DWORD numCodecs = 0;
  53. codecInfo->GetCodecInfoCount(WMMEDIATYPE_Audio, &numCodecs);
  54. // If there are no encoders, just return
  55. if (numCodecs == 0)
  56. {
  57. return false;
  58. }
  59. // Allocate structs for codecs and zero them all
  60. encs = (EncoderType *) calloc(numCodecs * 4, sizeof(struct EncoderType));
  61. if (encs != NULL)
  62. {
  63. encNumbs = numCodecs * 4;
  64. }
  65. else
  66. {
  67. wchar_t titleStr[32] = {0};
  68. MessageBoxW(NULL, WASABI_API_LNGSTRINGW(IDS_CANNOT_ALLOCATE_MEM),
  69. WASABI_API_LNGSTRINGW_BUF(IDS_WMA_ENCODER_ERROR,titleStr,32), MB_OK);
  70. return false;
  71. }
  72. // Now cycle through the codecs
  73. EncoderType* encp = encs;
  74. for (BOOL isVBR = 0;isVBR != 2;isVBR++)
  75. for (DWORD numPasses = 1;numPasses <= MAX_PASSES;numPasses++)
  76. for (DWORD i = 0;i != numCodecs;i++)
  77. {
  78. wchar_t codecName[5000] = {0};
  79. DWORD codecNameSize = 5000;
  80. codecInfo->SetCodecEnumerationSetting(WMMEDIATYPE_Audio, i, g_wszVBREnabled, WMT_TYPE_BOOL, (BYTE *)&isVBR, sizeof(BOOL));
  81. codecInfo->SetCodecEnumerationSetting(WMMEDIATYPE_Audio, i, g_wszNumPasses, WMT_TYPE_DWORD, (BYTE *)&numPasses, sizeof(DWORD));
  82. codecInfo->GetCodecName(WMMEDIATYPE_Audio, i, codecName, &codecNameSize);
  83. // Get the number of formats for this codec
  84. DWORD formatCount = 0;
  85. hr = codecInfo->GetCodecFormatCount( WMMEDIATYPE_Audio, i, &formatCount );
  86. if (FAILED(hr))
  87. {
  88. continue;
  89. }
  90. else if (formatCount == 0)
  91. {
  92. continue;
  93. }
  94. else
  95. {
  96. // Fill the EncoderType struct and allocate structs for format info
  97. // First allocate the space for all the formatType structs
  98. encp->formats = (formatType *) malloc(formatCount * sizeof(struct formatType));
  99. if (encp->formats == NULL)
  100. {
  101. wchar_t titleStr[32] = {0};
  102. MessageBoxW(NULL, WASABI_API_LNGSTRINGW(IDS_CANNOT_ALLOCATE_MEM),
  103. WASABI_API_LNGSTRINGW_BUF(IDS_WMA_ENCODER_ERROR,titleStr,32), MB_OK);
  104. return false;
  105. }
  106. // Now fill the EncoderType struct with name and info
  107. encp->encoderName = _wcsdup(codecName);
  108. encp->numFormats = formatCount;
  109. encp->offset = i;
  110. encp->vbr = isVBR;
  111. encp->numPasses = numPasses;
  112. }
  113. // Now cycle through the formats for this codec
  114. legalFormats = 0;
  115. formatType *fmts = encp->formats;
  116. for (DWORD f = 0;f != formatCount;f++)
  117. {
  118. wchar_t fDesc[5000] = {0};
  119. DWORD size = 5000;
  120. // Get the config info for this encoding format in string format
  121. IWMStreamConfig *streamConfig;
  122. codecInfo->GetCodecFormatDesc(WMMEDIATYPE_Audio, i, f, &streamConfig, fDesc, &size);
  123. // Now get the config info
  124. IWMMediaProps *props;
  125. streamConfig->QueryInterface(&props);
  126. // Get the bitrate
  127. //DWORD bitRate;
  128. //streamConfig->GetBitrate(&bitRate);
  129. // Get the Media Encoder type
  130. DWORD mediaTypeSize;
  131. props->GetMediaType(0, &mediaTypeSize);
  132. WM_MEDIA_TYPE *mediaType = (WM_MEDIA_TYPE *)new char[mediaTypeSize];
  133. props->GetMediaType(mediaType, &mediaTypeSize);
  134. // Go get the WAVEFORMATEX Struct from the
  135. if (mediaType->cbFormat >= sizeof(WAVEFORMATEX))
  136. {
  137. pwave = (WAVEFORMATEX*)mediaType->pbFormat;
  138. if (pwave != NULL)
  139. {
  140. // Check to see if this is an A/V codec format
  141. // If so, do not save it
  142. /*
  143. if ((pwave->nAvgBytesPerSec / pwave->nBlockAlign) ==
  144. ((pwave->nAvgBytesPerSec >= 3995) ? 5 : 3))
  145. {
  146. delete(mediaType);
  147. props->Release();
  148. streamConfig->Release();
  149. continue;
  150. }*/
  151. // old way of checking
  152. if ((wcsstr(fDesc, L"A/V")) != NULL)
  153. {
  154. delete[] (mediaType);
  155. props->Release();
  156. streamConfig->Release();
  157. continue;
  158. }
  159. // Load the format name
  160. fmts->formatName = _wcsdup(fDesc);
  161. // Load all the format values and the offset
  162. if ((pwave->nAvgBytesPerSec & 0x7FFFFF00) == 0x7FFFFF00)
  163. {
  164. fmts->bitrate = (pwave->nAvgBytesPerSec & 0x000000FF);
  165. fmts->vbr = 1;
  166. }
  167. else
  168. {
  169. fmts->bitrate = (pwave->nAvgBytesPerSec * 8);
  170. fmts->vbr = 0;
  171. }
  172. fmts->bitsSample = pwave->wBitsPerSample;
  173. fmts->nChannels = pwave->nChannels;
  174. fmts->samplesSec = pwave->nSamplesPerSec;
  175. fmts->offset = f;
  176. }
  177. else
  178. {
  179. wchar_t titleStr[32] = {0};
  180. MessageBoxW(NULL, WASABI_API_LNGSTRINGW(IDS_CANNOT_GET_STRUCTURE),
  181. WASABI_API_LNGSTRINGW_BUF(IDS_WMA_ENCODER_ERROR,titleStr,32), MB_OK);
  182. return false;
  183. }
  184. }
  185. else
  186. {
  187. wchar_t titleStr[32] = {0};
  188. MessageBoxW(NULL, WASABI_API_LNGSTRINGW(IDS_CANNOT_GET_ENCODER4_INFO),
  189. WASABI_API_LNGSTRINGW_BUF(IDS_WMA_ENCODER_ERROR,titleStr,32), MB_OK);
  190. return false;
  191. }
  192. // Set the media type value in the EncoderType struct on first legal format
  193. if (f == 0)
  194. {
  195. encp->mediaType = mediaType->subtype;
  196. }
  197. // Now point to the next table block and inc the legal formats count
  198. fmts++;
  199. legalFormats++;
  200. // And release the props and streams structs
  201. delete[] (mediaType);
  202. props->Release();
  203. streamConfig->Release();
  204. }
  205. // If there are no legal formats for this codec then skip it
  206. if (legalFormats == 0)
  207. {
  208. delete[] encp->encoderName;
  209. encp->encoderName = NULL;
  210. encp->numFormats = legalFormats;
  211. encp->offset = 0;
  212. }
  213. // Else load number of legal formats and save it
  214. else
  215. {
  216. encp->numFormats = legalFormats;
  217. encp++;
  218. }
  219. }
  220. return true;
  221. }
  222. static int FindFormatNumber(formatType *formats, int numFormats, configtype *cfg)
  223. {
  224. for (int i = 0;i < numFormats;i++, formats++)
  225. {
  226. if (formats->bitrate == cfg->config_bitrate
  227. && formats->bitsSample == cfg->config_bitsSample
  228. && formats->nChannels == cfg->config_nch
  229. && formats->samplesSec == cfg->config_samplesSec)
  230. return formats->offset;
  231. }
  232. return 0;
  233. }
  234. static VOID dumpProfile(char *configfile, BOOL isVBR, DWORD numPasses, int encNumb, int fmtNumb)
  235. {
  236. // Create a Profile and dump it
  237. IWMProfileManager *pmgr = NULL;
  238. IWMProfile *prof = NULL;
  239. IWMStreamConfig *sconf = NULL;
  240. IWMCodecInfo3 *cinfo = NULL;
  241. DWORD ssize;
  242. wchar_t errorTitle[128] = {0};
  243. WASABI_API_LNGSTRINGW_BUF(IDS_WMA_CONFIG_FILE_ERROR,errorTitle,128);
  244. HRESULT hr = WMCreateProfileManager(&pmgr);
  245. if (!FAILED(hr))
  246. {
  247. hr = pmgr->CreateEmptyProfile(WMT_VER_9_0, &prof);
  248. if (!FAILED(hr))
  249. {
  250. hr = pmgr->QueryInterface(&cinfo);
  251. if (!FAILED(hr))
  252. {
  253. cinfo->SetCodecEnumerationSetting(WMMEDIATYPE_Audio, encNumb, g_wszVBREnabled, WMT_TYPE_BOOL, (BYTE *)&isVBR, sizeof(BOOL));
  254. cinfo->SetCodecEnumerationSetting(WMMEDIATYPE_Audio, encNumb, g_wszNumPasses, WMT_TYPE_DWORD, (BYTE *)&numPasses, sizeof(DWORD));
  255. cinfo->GetCodecFormat(WMMEDIATYPE_Audio, encNumb, fmtNumb, &sconf);
  256. sconf->SetConnectionName(L"enc_wma");
  257. sconf->SetStreamName(L"enc_wma");
  258. sconf->SetStreamNumber(1);
  259. hr = prof->AddStream(sconf);
  260. if (!FAILED(hr))
  261. {
  262. hr = pmgr->SaveProfile(prof, NULL, &ssize);
  263. if (!FAILED(hr))
  264. {
  265. WCHAR* pstring = new WCHAR[ssize];
  266. if (pstring != NULL)
  267. {
  268. hr = pmgr->SaveProfile(prof, pstring, &ssize);
  269. if (!FAILED(hr))
  270. {
  271. wchar_t cstring[4000] = {0};
  272. wcsncpy(cstring, pstring, 4000 - 1);
  273. WritePrivateProfileStructW(L"audio_wma", L"profile", cstring, sizeof(cstring) / sizeof(*cstring), AutoWide(configfile));
  274. }
  275. else{ MessageBoxW(NULL, WASABI_API_LNGSTRINGW(IDS_SAVE_PROFILE_READ_ERROR), errorTitle, MB_OK); }
  276. }
  277. else{ MessageBoxW(NULL, WASABI_API_LNGSTRINGW(IDS_MEM_ALLOCATION_ERROR), errorTitle, MB_OK); }
  278. }
  279. else{ MessageBoxW(NULL, WASABI_API_LNGSTRINGW(IDS_PROFILE_SAVE_SIZE_ERROR), errorTitle, MB_OK); }
  280. }
  281. else{ MessageBoxW(NULL, WASABI_API_LNGSTRINGW(IDS_CANNOT_READ_AUDIO_STREAM), errorTitle, MB_OK); }
  282. }
  283. else{ MessageBoxW(NULL, WASABI_API_LNGSTRINGW(IDS_CANNOT_GET_CODEC_INFO), errorTitle, MB_OK); }
  284. }
  285. else{ MessageBoxW(NULL, WASABI_API_LNGSTRINGW(IDS_CANNOT_CREATE_A_PROFILE), errorTitle, MB_OK); }
  286. }
  287. else{ MessageBoxW(NULL, WASABI_API_LNGSTRINGW(IDS_CANNOT_CREATE_PROFILE_MANAGER), errorTitle, MB_OK); }
  288. pmgr->Release();
  289. prof->Release();
  290. sconf->Release();
  291. cinfo->Release();
  292. }
  293. static bool Has(HWND hwndDlg, int item, int data)
  294. {
  295. int numChoices = SendDlgItemMessage(hwndDlg, item, CB_GETCOUNT, 0, 0);
  296. for (int i = 0;i < numChoices;i++)
  297. {
  298. if (SendDlgItemMessage(hwndDlg, item, CB_GETITEMDATA, i, 0) == data)
  299. return true;
  300. }
  301. return false;
  302. }
  303. static int EncodeSampleFormat(int bps, int numChannels, int sampleRate)
  304. {
  305. // 20 bits sampleRate
  306. assert((sampleRate & 0xFFFFF) == sampleRate);
  307. // 6 bits numChannels
  308. assert((numChannels & 0x3F) == numChannels);
  309. // 6 bits bps
  310. assert((bps & 0x3F) == bps);
  311. return (sampleRate << 12) | (numChannels << 6) | (bps);
  312. }
  313. static void DecodeSampleFormat(int data, int &bps, int &numChannels, int &sampleRate)
  314. {
  315. bps = data & 0x3F;
  316. data >>= 6;
  317. numChannels = data & 0x3F;
  318. data >>= 6;
  319. sampleRate = data;
  320. }
  321. static int EncodeVBR(BOOL isVBR, DWORD numPasses)
  322. {
  323. // 1 bits VBR
  324. assert((isVBR & 0x1) == isVBR);
  325. // 15 bits numPasses
  326. assert((numPasses & 0x7FFF) == numPasses);
  327. return (isVBR << 15) | (numPasses);
  328. }
  329. static void DecodeVBR(int data, BOOL &isVBR, DWORD &numPasses)
  330. {
  331. numPasses = data & 0x7FFF;
  332. data >>= 15;
  333. isVBR = data & 0x1;
  334. }
  335. static void AutoSelect(HWND hwndDlg, int dlgItem)
  336. {
  337. if (SendDlgItemMessage(hwndDlg, dlgItem, CB_GETCURSEL, 0, 0) == CB_ERR)
  338. SendDlgItemMessage(hwndDlg, dlgItem, CB_SETCURSEL, 0, 0);
  339. }
  340. static EncoderType *FindEncoder(int encoderNumber, BOOL isVBR, DWORD numPasses)
  341. {
  342. EncoderType* enc = encs;
  343. for (int i = 0;i < encNumbs;i++, enc++)
  344. {
  345. if (enc->encoderName == NULL)
  346. return 0; //WTF?
  347. if (enc->offset == encoderNumber && enc->vbr == isVBR && enc->numPasses == numPasses)
  348. return enc;
  349. }
  350. return 0; //WTF?
  351. }
  352. #define MASK_ENCODER 0x1
  353. #define MASK_VBR 0x2
  354. #define MASK_SAMPLE_FORMAT 0x4
  355. #define MASK_BITRATE 0x8
  356. #define MASK_ALL 0xF
  357. static void ResetConfig(HWND hwndDlg, EncoderType *encs, configtype *cfg, int mask)
  358. {
  359. wchar_t buf1[100] = {0};
  360. EncoderType* enc = encs;
  361. if (mask & MASK_ENCODER) SendDlgItemMessage(hwndDlg, IDC_ENCODER, CB_RESETCONTENT, 0, 0);
  362. if (mask & MASK_SAMPLE_FORMAT) SendDlgItemMessage(hwndDlg, IDC_SAMPLE_FORMAT, CB_RESETCONTENT, 0, 0);
  363. if (mask & MASK_BITRATE) SendDlgItemMessage(hwndDlg, IDC_BRATE, CB_RESETCONTENT, 0, 0);
  364. if (mask & MASK_VBR) SendDlgItemMessage(hwndDlg, IDC_VBR, CB_RESETCONTENT, 0, 0);
  365. // reset encoders
  366. int thisVBR = EncodeVBR(cfg->config_vbr, cfg->config_passes);
  367. for (int i = 0;i < encNumbs;i++, enc++)
  368. {
  369. if (enc->encoderName == NULL)
  370. break;
  371. else if ((mask & MASK_ENCODER) && !Has(hwndDlg, IDC_ENCODER, enc->offset))
  372. {
  373. int newpos = SendDlgItemMessage(hwndDlg, IDC_ENCODER, CB_ADDSTRING, 0, (LPARAM)enc->encoderName);
  374. SendDlgItemMessage(hwndDlg, IDC_ENCODER, CB_SETITEMDATA, newpos, enc->offset);
  375. if (cfg->config_encoder == enc->offset)
  376. {
  377. SendDlgItemMessage(hwndDlg, IDC_ENCODER, CB_SETCURSEL, newpos, 0);
  378. }
  379. }
  380. int data = EncodeVBR(enc->vbr, enc->numPasses);
  381. if ((mask & MASK_VBR) && cfg->config_encoder == enc->offset && !Has(hwndDlg, IDC_VBR, data))
  382. {
  383. int newpos = CB_ERR;
  384. if (enc->vbr == FALSE && enc->numPasses == 1)
  385. newpos = SendDlgItemMessageW(hwndDlg, IDC_VBR, CB_ADDSTRING, 0, (LPARAM)WASABI_API_LNGSTRINGW(IDS_CBR));
  386. else if (enc->vbr == FALSE && enc->numPasses == 2)
  387. newpos = SendDlgItemMessageW(hwndDlg, IDC_VBR, CB_ADDSTRING, 0, (LPARAM)WASABI_API_LNGSTRINGW(IDS_2_PASS_CBR));
  388. else if (enc->vbr == TRUE && enc->numPasses == 1)
  389. newpos = SendDlgItemMessageW(hwndDlg, IDC_VBR, CB_ADDSTRING, 0, (LPARAM)WASABI_API_LNGSTRINGW(IDS_VBR));
  390. else if (enc->vbr == TRUE && enc->numPasses == 2)
  391. newpos = SendDlgItemMessageW(hwndDlg, IDC_VBR, CB_ADDSTRING, 0, (LPARAM)WASABI_API_LNGSTRINGW(IDS_ABR));
  392. SendDlgItemMessage(hwndDlg, IDC_VBR, CB_SETITEMDATA, newpos, data);
  393. if (thisVBR == data)
  394. SendDlgItemMessage(hwndDlg, IDC_VBR, CB_SETCURSEL, newpos, 0);
  395. }
  396. }
  397. AutoSelect(hwndDlg, IDC_ENCODER);
  398. AutoSelect(hwndDlg, IDC_VBR);
  399. int pos = SendDlgItemMessage(hwndDlg, IDC_VBR, CB_GETCURSEL, 0, 0);
  400. int data = SendDlgItemMessage(hwndDlg, IDC_VBR, CB_GETITEMDATA, pos, 0);
  401. DecodeVBR(data, cfg->config_vbr, cfg->config_passes);
  402. pos = SendDlgItemMessage(hwndDlg, IDC_ENCODER, CB_GETCURSEL, 0, 0);
  403. data = SendDlgItemMessage(hwndDlg, IDC_ENCODER, CB_GETITEMDATA, pos, 0);
  404. cfg->config_encoder = data;
  405. // Now set up for dialog fill
  406. enc = FindEncoder(cfg->config_encoder, cfg->config_vbr, cfg->config_passes);
  407. // Fill the current values
  408. formatType *fmt = enc->formats;
  409. int thisSampleFormat = EncodeSampleFormat(cfg->config_bitsSample, cfg->config_nch, cfg->config_samplesSec);
  410. for (int i = 0;i < enc->numFormats;i++, fmt++)
  411. {
  412. int data = EncodeSampleFormat(fmt->bitsSample, fmt->nChannels, fmt->samplesSec);
  413. // Add channels to list
  414. if ((mask & MASK_SAMPLE_FORMAT) && !Has(hwndDlg, IDC_SAMPLE_FORMAT, data))
  415. {
  416. if (fmt->nChannels == 1)
  417. wsprintfW(buf1, WASABI_API_LNGSTRINGW(IDS_MONO_INFO), fmt->bitsSample, fmt->samplesSec);
  418. else if (fmt->nChannels == 2)
  419. wsprintfW(buf1, WASABI_API_LNGSTRINGW(IDS_STEREO_INFO), fmt->bitsSample, fmt->samplesSec);
  420. else
  421. wsprintfW(buf1, WASABI_API_LNGSTRINGW(IDS_CHANNELS_INFO), fmt->bitsSample, fmt->nChannels, fmt->samplesSec);
  422. int newpos;
  423. if (fmt->bitsSample)
  424. newpos = SendDlgItemMessageW(hwndDlg, IDC_SAMPLE_FORMAT, CB_ADDSTRING, 0, (LPARAM)buf1);
  425. else
  426. newpos = SendDlgItemMessageW(hwndDlg, IDC_SAMPLE_FORMAT, CB_ADDSTRING, 0, (LPARAM)buf1 + 8); // skip "0 bits, "
  427. SendDlgItemMessage(hwndDlg, IDC_SAMPLE_FORMAT, CB_SETITEMDATA, newpos, data);
  428. // Now set current select for number of channels sample
  429. if (thisSampleFormat == data)
  430. SendDlgItemMessage(hwndDlg, IDC_SAMPLE_FORMAT, CB_SETCURSEL, newpos, 0);
  431. }
  432. }
  433. if (SendDlgItemMessage(hwndDlg, IDC_SAMPLE_FORMAT, CB_GETCURSEL, 0, 0) == CB_ERR)
  434. {
  435. int num = SendDlgItemMessage(hwndDlg, IDC_SAMPLE_FORMAT, CB_GETCOUNT, 0, 0);
  436. int defaultSampleFormat = EncodeSampleFormat(16, 2, 44100);
  437. for (int i = 0;i < num;i++)
  438. {
  439. int data = SendDlgItemMessage(hwndDlg, IDC_SAMPLE_FORMAT, CB_GETITEMDATA, i, 0);
  440. if (data == defaultSampleFormat)
  441. SendDlgItemMessage(hwndDlg, IDC_SAMPLE_FORMAT, CB_SETCURSEL, i, 0);
  442. }
  443. }
  444. AutoSelect(hwndDlg, IDC_SAMPLE_FORMAT);
  445. pos = SendDlgItemMessage(hwndDlg, IDC_SAMPLE_FORMAT, CB_GETCURSEL, 0, 0);
  446. data = SendDlgItemMessage(hwndDlg, IDC_SAMPLE_FORMAT, CB_GETITEMDATA, pos, 0);
  447. DecodeSampleFormat(data, cfg->config_bitsSample, cfg->config_nch, cfg->config_samplesSec);
  448. thisSampleFormat = EncodeSampleFormat(cfg->config_bitsSample, cfg->config_nch, cfg->config_samplesSec);
  449. // Next Show the Bitrates
  450. fmt = enc->formats;
  451. for (int i = 0;i < enc->numFormats;i++, fmt++)
  452. {
  453. int data = EncodeSampleFormat(fmt->bitsSample, fmt->nChannels, fmt->samplesSec);
  454. if (thisSampleFormat == data)
  455. {
  456. if ((mask & MASK_BITRATE) && !Has(hwndDlg, IDC_BRATE, fmt->bitrate))
  457. {
  458. if (fmt->vbr)
  459. SetDlgItemTextW(hwndDlg, IDC_STATIC_BITRATE, WASABI_API_LNGSTRINGW(IDS_QUALITY));
  460. else
  461. SetDlgItemTextW(hwndDlg, IDC_STATIC_BITRATE, WASABI_API_LNGSTRINGW(IDS_BITRATE));
  462. wsprintfW(buf1, L"%d", fmt->bitrate);
  463. int newpos = SendDlgItemMessageW(hwndDlg, IDC_BRATE, CB_ADDSTRING, 0, (LPARAM)buf1);
  464. SendDlgItemMessage(hwndDlg, IDC_BRATE, CB_SETITEMDATA, newpos, fmt->bitrate);
  465. // Set the current bit rate
  466. if (cfg->config_bitrate == fmt->bitrate)
  467. {
  468. SendDlgItemMessage(hwndDlg, IDC_BRATE, CB_SETCURSEL, newpos, 0);
  469. }
  470. }
  471. }
  472. }
  473. if (SendDlgItemMessage(hwndDlg, IDC_BRATE, CB_GETCURSEL, 0, 0) == CB_ERR)
  474. {
  475. int num = SendDlgItemMessage(hwndDlg, IDC_BRATE, CB_GETCOUNT, 0, 0);
  476. for (int i = 0;i < num;i++)
  477. {
  478. int data = SendDlgItemMessage(hwndDlg, IDC_BRATE, CB_GETITEMDATA, i, 0);
  479. if (data == 50 || (data / 1000 == 128))
  480. SendDlgItemMessage(hwndDlg, IDC_BRATE, CB_SETCURSEL, i, 0);
  481. }
  482. }
  483. AutoSelect(hwndDlg, IDC_BRATE);
  484. pos = SendDlgItemMessage(hwndDlg, IDC_BRATE, CB_GETCURSEL, 0, 0);
  485. data = SendDlgItemMessage(hwndDlg, IDC_BRATE, CB_GETITEMDATA, pos, 0);
  486. cfg->config_bitrate = data;
  487. }
  488. BOOL CALLBACK ConfigProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  489. {
  490. configwndrec *wc = NULL;
  491. if (uMsg == WM_INITDIALOG)
  492. {
  493. // LGIVEN Mod 4-10-05
  494. #if defined(_WIN64)
  495. SetWindowLong(hwndDlg, GWLP_USERDATA, lParam);
  496. #else
  497. SetWindowLong(hwndDlg, GWL_USERDATA, lParam);
  498. #endif
  499. if (lParam)
  500. {
  501. // Get the saved params
  502. wc = (configwndrec*)lParam;
  503. loadWMATables();
  504. ResetConfig(hwndDlg, encs , &wc->cfg, MASK_ALL);
  505. }
  506. return 1;
  507. }
  508. if (uMsg == WM_DESTROY)
  509. {
  510. #if defined(_WIN64)
  511. wc = (configwndrec*)SetWindowLong(hwndDlg, GWLP_USERDATA, 0);
  512. #else
  513. wc = (configwndrec*)SetWindowLong(hwndDlg, GWL_USERDATA, 0);
  514. #endif
  515. if (wc)
  516. {
  517. EncoderType *encoder=FindEncoder(wc->cfg.config_encoder,wc->cfg.config_vbr, wc->cfg.config_passes);
  518. int formatNumber = FindFormatNumber(encoder->formats, encoder->numFormats, &wc->cfg);
  519. // Dump the profile in WMA format
  520. dumpProfile(wc->configfile, wc->cfg.config_vbr, wc->cfg.config_passes, wc->cfg.config_encoder, formatNumber);
  521. // Write it to config file
  522. writeconfig(wc->configfile, &wc->cfg);
  523. free(wc->configfile);
  524. free(wc);
  525. }
  526. return 0;
  527. }
  528. if (uMsg == WM_COMMAND)
  529. {
  530. switch (LOWORD(wParam))
  531. {
  532. case IDC_VBR:
  533. if ((HIWORD(wParam) == CBN_SELCHANGE))
  534. {
  535. #if defined(_WIN64)
  536. wc = (configwndrec*)GetWindowLong(hwndDlg, GWLP_USERDATA);
  537. #else
  538. wc = (configwndrec*)GetWindowLong(hwndDlg, GWL_USERDATA);
  539. #endif
  540. int pos = SendDlgItemMessage(hwndDlg, IDC_VBR, CB_GETCURSEL, 0, 0);
  541. int data = SendDlgItemMessage(hwndDlg, IDC_VBR, CB_GETITEMDATA, pos, 0);
  542. DecodeVBR(data, wc->cfg.config_vbr, wc->cfg.config_passes);
  543. ResetConfig(hwndDlg, encs, &wc->cfg, MASK_BITRATE | MASK_SAMPLE_FORMAT);
  544. }
  545. break;
  546. case IDC_SAMPLE_FORMAT:
  547. if ((HIWORD(wParam) == CBN_SELCHANGE))
  548. {
  549. #if defined(_WIN64)
  550. wc = (configwndrec*)GetWindowLong(hwndDlg, GWLP_USERDATA);
  551. #else
  552. wc = (configwndrec*)GetWindowLong(hwndDlg, GWL_USERDATA);
  553. #endif
  554. int pos = SendDlgItemMessage(hwndDlg, IDC_SAMPLE_FORMAT, CB_GETCURSEL, 0, 0);
  555. int data = SendDlgItemMessage(hwndDlg, IDC_SAMPLE_FORMAT, CB_GETITEMDATA, pos, 0);
  556. DecodeSampleFormat(data, wc->cfg.config_bitsSample, wc->cfg.config_nch, wc->cfg.config_samplesSec);
  557. ResetConfig(hwndDlg, encs, &wc->cfg, MASK_BITRATE);
  558. }
  559. break;
  560. case IDC_BRATE:
  561. if ((HIWORD(wParam) == CBN_SELCHANGE))
  562. {
  563. #if defined(_WIN64)
  564. wc = (configwndrec*)GetWindowLong(hwndDlg, GWLP_USERDATA);
  565. #else
  566. wc = (configwndrec*)GetWindowLong(hwndDlg, GWL_USERDATA);
  567. #endif
  568. int pos = SendDlgItemMessage(hwndDlg, IDC_BRATE, CB_GETCURSEL, 0, 0);
  569. int data = SendDlgItemMessage(hwndDlg, IDC_BRATE, CB_GETITEMDATA, pos, 0);
  570. wc->cfg.config_bitrate = data;
  571. }
  572. break;
  573. case IDC_ENCODER:
  574. if ((HIWORD(wParam) == CBN_SELCHANGE))
  575. {
  576. #if defined(_WIN64)
  577. wc = (configwndrec*)GetWindowLong(hwndDlg, GWLP_USERDATA);
  578. #else
  579. wc = (configwndrec*)GetWindowLong(hwndDlg, GWL_USERDATA);
  580. #endif
  581. if (wc)
  582. {
  583. int pos = SendDlgItemMessage(hwndDlg, IDC_ENCODER, CB_GETCURSEL, 0, 0);
  584. int data = SendDlgItemMessage(hwndDlg, IDC_ENCODER, CB_GETITEMDATA, pos, 0);
  585. wc->cfg.config_encoder = data;
  586. ResetConfig(hwndDlg, encs, &wc->cfg, MASK_VBR | MASK_SAMPLE_FORMAT | MASK_BITRATE);
  587. }
  588. }
  589. break;
  590. }
  591. }
  592. return 0;
  593. }