33 #include <nsIMutableArray.h>
34 #include <nsISimpleEnumerator.h>
35 #include <nsIPrefBranch.h>
36 #include <nsISupportsPrimitives.h>
37 #include <nsIPrefLocalizedString.h>
39 #include <nsComponentManagerUtils.h>
40 #include <nsAutoPtr.h>
47 #include <ngIEqualizerPresetProvider.h>
48 #include <ngIEqualizerPreset.h>
55 static PRLogModuleInfo * BaseMediacoreMultibandEqualizer() {
56 static PRLogModuleInfo* gBaseMediacoreMultibandEqualizer =
57 PR_NewLogModule(
"sbBaseMediacoreMultibandEqualizer");
58 return gBaseMediacoreMultibandEqualizer;
61 #define TRACE(args) PR_LOG(BaseMediacoreMultibandEqualizer() , PR_LOG_DEBUG, args)
62 #define LOG(args) PR_LOG(BaseMediacoreMultibandEqualizer() , PR_LOG_WARN, args)
76 else if(aGain < aMin) {
99 aGainStr.Assign(gain);
104 #define SB_EQ_PRESET_PREF "songbird.eq.currentPreset"
114 {32, 64, 125, 250, 500, 1000, 2000, 4000, 8000, 16000};
121 , mEqEnabled(PR_FALSE)
122 , mCurrentPresetName(NO_PRESET)
123 , mSettingPreset(
false)
127 TRACE((
"sbBaseMediacoreMultibandEqualizer[0x%x] - Created",
this));
132 TRACE((
"sbBaseMediacoreMultibandEqualizer[0x%x] - Destroyed",
this));
135 nsAutoMonitor::DestroyMonitor(
mMonitor);
138 if (
mBands.IsInitialized()) {
146 TRACE((
"sbBaseMediacoreMultibandEqualizer[0x%x] - EnsureBandIsCached",
this));
148 NS_ENSURE_TRUE(
mMonitor, NS_ERROR_NOT_INITIALIZED);
149 NS_ENSURE_TRUE(
mBands.IsInitialized(), NS_ERROR_NOT_INITIALIZED);
151 PRUint32 bandIndex = 0;
152 nsresult rv = aBand->GetIndex(&bandIndex);
153 NS_ENSURE_SUCCESS(rv, rv);
155 PRBool success =
mBands.Get(bandIndex, nsnull);
157 success =
mBands.Put(bandIndex, aBand);
158 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
161 PRUint32 bandFrequency = 0;
162 rv = aBand->GetFrequency(&bandFrequency);
163 NS_ENSURE_SUCCESS(rv, rv);
165 PRFloat64 bandGain = 0;
166 rv = aBand->GetGain(&bandGain);
167 NS_ENSURE_SUCCESS(rv, rv);
169 nsCOMPtr<sbIMediacoreEqualizerBand> band;
170 mBands.Get(bandIndex, getter_AddRefs(band));
172 rv = band->SetFrequency(bandFrequency);
173 NS_ENSURE_SUCCESS(rv, rv);
175 rv = band->SetGain(bandGain);
176 NS_ENSURE_SUCCESS(rv, rv);
185 TRACE((
"sbBaseMediacoreMultibandEqualizer[0x%x] - InitBaseMediacoreMultibandEqualizer",
this));
187 mMonitor = nsAutoMonitor::NewMonitor(
"sbBaseMediacoreMultibandEqualizer::mMonitor");
188 NS_ENSURE_TRUE(
mMonitor, NS_ERROR_OUT_OF_MEMORY);
190 PRBool success =
mBands.Init(10);
191 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
194 NS_ENSURE_SUCCESS(rv, rv);
197 NS_ENSURE_SUCCESS (rv, rv);
198 nsCOMPtr<nsIPrefLocalizedString>
data;
200 NS_ENSURE_SUCCESS(rv, rv);
201 rv = data->GetData(getter_Copies(mCurrentPresetName));
202 NS_ENSURE_SUCCESS(rv, rv);
205 NS_ENSURE_SUCCESS(rv, rv);
208 rv = mPresets->HasPresetNamed(mCurrentPresetName, &hasPreset);
209 NS_ENSURE_SUCCESS(rv, rv);
211 rv = this->SetCurrentPresetName(
NO_PRESET);
212 NS_ENSURE_SUCCESS(rv, rv);
226 return NS_ERROR_NOT_IMPLEMENTED;
237 return NS_ERROR_NOT_IMPLEMENTED;
248 return NS_ERROR_NOT_IMPLEMENTED;
261 return NS_ERROR_NOT_IMPLEMENTED;
272 return NS_ERROR_NOT_IMPLEMENTED;
280 sbBaseMediacoreMultibandEqualizer::GetEqEnabled(PRBool *aEqEnabled)
282 TRACE((
"sbBaseMediacoreMultibandEqualizer[0x%x] - GetEqEnabled",
this));
284 NS_ENSURE_TRUE(
mMonitor, NS_ERROR_NOT_INITIALIZED);
285 NS_ENSURE_ARG_POINTER(aEqEnabled);
294 sbBaseMediacoreMultibandEqualizer::SetEqEnabled(PRBool aEqEnabled)
296 TRACE((
"sbBaseMediacoreMultibandEqualizer[0x%x] - SetEqEnabled",
this));
298 NS_ENSURE_TRUE(
mMonitor, NS_ERROR_NOT_INITIALIZED);
303 NS_ENSURE_SUCCESS(rv, rv);
313 nsIMutableArray *
array = (nsIMutableArray*)aArray;
315 nsCOMPtr<nsISupports> supports = do_QueryInterface(aData, &rv);
316 NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
318 rv = array->AppendElement(aData,
false);
319 NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
321 return PL_DHASH_NEXT;
326 sbBaseMediacoreMultibandEqualizer::EnumerateIntoArrayUint32Key(
327 const PRUint32 &aKey,
331 TRACE((
"sbBaseMediacoreMultibandEqualizer[0x%x] - EnumerateIntoArray (PRUint32)"));
339 TRACE((
"sbBaseMediacoreMultibandEqualizer[0x%x] - GetBands",
this));
341 NS_ENSURE_TRUE(
mBands.IsInitialized(), NS_ERROR_NOT_INITIALIZED);
343 nsresult rv = NS_ERROR_UNEXPECTED;
347 nsCOMPtr<nsIMutableArray> mutableArray =
348 do_CreateInstance(
"@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
349 NS_ENSURE_SUCCESS(rv, rv);
352 sbBaseMediacoreMultibandEqualizer::EnumerateIntoArrayUint32Key,
357 rv = mutableArray->Enumerate(aBands);
358 NS_ENSURE_SUCCESS(rv, rv);
366 NS_ENSURE_ARG_POINTER(aBands);
368 nsresult rv = NS_ERROR_UNEXPECTED;
372 while(NS_SUCCEEDED(aBands->HasMoreElements(&hasMore)) &&
374 NS_SUCCEEDED(aBands->GetNext(getter_AddRefs(element)))) {
376 nsCOMPtr<sbIMediacoreEqualizerBand> band =
377 do_QueryInterface(element, &rv);
378 NS_ENSURE_SUCCESS(rv, rv);
381 NS_ENSURE_SUCCESS(rv, rv);
384 if(!mSettingPreset) {
385 rv = this->SetCurrentPresetName(
NO_PRESET);
386 NS_ENSURE_SUCCESS(rv, rv);
394 sbBaseMediacoreMultibandEqualizer::GetBandCount(PRUint32 *aBandCount)
396 TRACE((
"sbBaseMediacoreMultibandEqualizer[0x%x] - GetBandCount",
this));
398 NS_ENSURE_TRUE(
mMonitor, NS_ERROR_NOT_INITIALIZED);
399 NS_ENSURE_ARG_POINTER(aBandCount);
404 NS_ENSURE_SUCCESS(rv, rv);
411 sbBaseMediacoreMultibandEqualizer::GetCurrentPresetName(nsAString& aCurrentPresetName)
413 TRACE((
"sbBaseMediacoreMultibandEqualizer[0x%x] - GetCurrentPresetName",
this));
415 NS_ENSURE_TRUE(
mMonitor, NS_ERROR_NOT_INITIALIZED);
419 aCurrentPresetName = mCurrentPresetName;
424 sbBaseMediacoreMultibandEqualizer::SetCurrentPresetName(
const nsAString& aCurrentPresetName)
426 TRACE((
"sbBaseMediacoreMultibandEqualizer[0x%x] - SetCurrentPresetName",
this));
430 NS_ENSURE_TRUE(mPrefs, NS_ERROR_NOT_INITIALIZED);
431 NS_ENSURE_TRUE(mPresets, NS_ERROR_NOT_INITIALIZED);
433 mSettingPreset =
true;
440 if(mCurrentPresetName != aCurrentPresetName)
442 mCurrentPresetName = aCurrentPresetName;
444 LOG((
"Updating currentPreset pref"));
445 nsCOMPtr<nsIPrefLocalizedString>
data (do_CreateInstance(
"@mozilla.org/pref-localizedstring;1", &rv));
446 NS_ENSURE_SUCCESS(rv, rv);
447 rv =
data->SetDataWithLength(aCurrentPresetName.Length(), aCurrentPresetName.BeginReading());
448 NS_ENSURE_SUCCESS(rv, rv);
450 NS_ENSURE_SUCCESS(rv, rv);
456 rv = mPresets->HasPresetNamed(aCurrentPresetName, &presetExists);
457 NS_ENSURE_SUCCESS(rv, rv);
458 LOG((
"Preset exists: %i", presetExists));
462 nsCOMPtr<nsISimpleEnumerator>
bands = nsnull;
463 rv = this->GetBands(getter_AddRefs(bands));
464 NS_ENSURE_SUCCESS(rv, rv);
465 NS_ENSURE_TRUE(bands, NS_ERROR_UNEXPECTED);
467 nsCOMPtr<ngIEqualizerPreset> preset = nsnull;
468 rv = mPresets->GetPresetByName(aCurrentPresetName, getter_AddRefs(preset));
469 NS_ENSURE_SUCCESS(rv, rv);
470 NS_ENSURE_TRUE(preset, NS_ERROR_UNEXPECTED);
471 nsCOMPtr<nsIArray> valuesArray = nsnull;
472 rv = preset->GetValues(getter_AddRefs(valuesArray));
473 NS_ENSURE_SUCCESS(rv, rv);
474 NS_ENSURE_TRUE(valuesArray, NS_ERROR_UNEXPECTED);
475 LOG((
"Got the two enumerators"));
476 PRBool hasMoreBands = PR_FALSE;
478 nsCOMPtr<nsISupports> element = nsnull;
479 nsCOMPtr<nsISupports>
value = nsnull;
481 while(NS_SUCCEEDED(bands->HasMoreElements(&hasMoreBands)) &&
483 NS_SUCCEEDED(bands->GetNext(getter_AddRefs(element)))) {
484 LOG((
"Getting the info for a band"));
485 nsCOMPtr<sbIMediacoreEqualizerBand> band(do_QueryInterface(element, &rv));
486 NS_ENSURE_SUCCESS(rv, rv);
489 rv = band->GetIndex(&index);
490 NS_ENSURE_SUCCESS(rv, rv);
492 nsCOMPtr<nsISupportsDouble> gain;
493 rv = valuesArray->QueryElementAt(index, NS_GET_IID(nsISupportsDouble), getter_AddRefs(gain));
494 NS_ENSURE_SUCCESS(rv, rv);
495 LOG((
"Extracting the GAIN value"));
497 rv = gain->GetData(&gainValue);
498 NS_ENSURE_SUCCESS(rv, rv);
499 LOG((
"Applying the GAIN value"));
500 rv = band->SetGain(gainValue);
501 NS_ENSURE_SUCCESS(rv, rv);
503 LOG((
"Applying the band"));
504 rv = this->SetBand(band);
505 NS_ENSURE_SUCCESS(rv, rv);
508 LOG((
"Applying new GAIN value to the band slider"));
509 nsEmbedCString gainString;
511 nsCOMPtr<nsISupportsString> supportsGainString(do_CreateInstance(
"@mozilla.org/supports-string;1", &rv));
512 NS_ENSURE_SUCCESS(rv, rv);
513 rv = supportsGainString->SetData(NS_ConvertUTF8toUTF16(gainString));
514 NS_ENSURE_SUCCESS(rv, rv);
516 nsEmbedCString bandPrefName(NS_LITERAL_CSTRING(
"songbird.eq.band."));
517 bandPrefName.AppendInt(index);
519 LOG((
"Band: %i, Gain: %s", index, gainString.get()));
520 rv = mPrefs->SetComplexValue(bandPrefName.get(), NS_GET_IID(
nsISupportsString), supportsGainString);
521 NS_ENSURE_SUCCESS(rv, rv);
526 return this->SetCurrentPresetName(
NO_PRESET);
530 mSettingPreset =
false;
539 TRACE((
"sbBaseMediacoreMultibandEqualizer[0x%x] - GetBand",
this));
541 NS_ENSURE_TRUE(
mMonitor, NS_ERROR_NOT_INITIALIZED);
542 NS_ENSURE_TRUE(
mBands.IsInitialized(), NS_ERROR_NOT_INITIALIZED);
543 NS_ENSURE_ARG_POINTER(_retval);
544 nsresult rv = NS_ERROR_UNEXPECTED;
546 nsCOMPtr<sbIMediacoreEqualizerBand> band;
550 if(!
mBands.Get(aBandIndex, getter_AddRefs(band))) {
551 nsRefPtr<sbMediacoreEqualizerBand> newBand;
553 NS_ENSURE_TRUE(newBand, NS_ERROR_OUT_OF_MEMORY);
556 NS_ENSURE_SUCCESS(rv, rv);
559 NS_ENSURE_SUCCESS(rv, rv);
561 NS_ADDREF(*_retval = newBand);
566 band.forget(_retval);
575 TRACE((
"sbBaseMediacoreMultibandEqualizer[0x%x] - SetBand",
this));
577 NS_ENSURE_TRUE(
mMonitor, NS_ERROR_NOT_INITIALIZED);
578 nsresult rv = NS_ERROR_UNEXPECTED;
584 NS_ENSURE_SUCCESS(rv, rv);
587 if(!mSettingPreset) {
588 rv = this->SetCurrentPresetName(
NO_PRESET);
589 NS_ENSURE_SUCCESS(rv, rv);
593 NS_ENSURE_SUCCESS(rv, rv);
const nsIPrefLocalizedString
const sbCreateProxiedComponent do_ProxiedGetService(const nsCID &aCID, nsresult *error=0)
StringArrayEnumerator prototype hasMore
_updateTextAndScrollDataForFrame aData