User Tools

Site Tools


developer_center:recipe_book:extensions_and_core:equalizer_preset_provider

Equalizer Preset Provider

In Nightingale you can add sets of Equalizer presets with an extension. Nightingale offers you prebuilt classes you can use for presets, but you may also implement your own.

Create Presets

Each preset is an object in Nightingale. It consists of a name and the values of the bands. Please choose a unique name, as other extensions can easily overwrite it, but not consistently, as the overwriting order is different each start of Nightingale. To create such a preset the easiest way is to use the ngMutableEqualizerPreset from Nightingale. Due to it's XPCOM nature, it isn't as straight forward as creating a JS array, sadly, but you could use a function like this to create equalizer presets from a name and a JS array containing the band values (which are between -1 and 1):

Components.utils.import("resource://app/jsmodules/ArrayConverter.jsm");
function createPreset(name, values) {
  // instantiate a new preset object
  var newPreset = Components.classes["@getnightingale.com/equalizer-presets/mutable;1"].createInstance(Components.interfaces.ngIMutbaleEqualizerPreset);
  // name the preset
  newPreset.setName(name);
  // now follows the interesting part, converting the JS array into the XPCOM compatible nsIArray of nsISupportsDoubles
  var wrappedValues = [];
  for(var i = 0; i < values.length; ++i) { // length should be equal to 10
    var double = Components.classes["@mozilla.org/supports-double;1"].createInstance(Components.interfaces.nsISupportsDouble);
    double.data = values[i];
    wrappedValues.push(double);
  }
  newPreset.setValues(wrappedValues);
 
  // when returning, query interface the preset to the immutable ngIEqualizerPreset so it can't be changed anymore.
  return newPreset.QueryInterface(Components.interfaces.ngIEqualizerPreset);
}

Create a Provider

This function allows us to easily create presets. However in order for Nightingale to display those presets they need to be added to a provider and this provider added to Nightingale. There is no easy solution here. You will have to write your own provider in every case. Not that it has to be a valid XPCOM class.

const Cc = Components.classes;
const Cu = Components.utils;
const Ci = Components.interfaces;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://app/jsmodules/ArrayConverter.jsm");
 
function BasicExamplePresetProvider() {
  this._presets = [];
  // add your equalizer presets to this._presets, the preset is created using the function from the last chapter
  var examplePreset = this._createPreset("Example Preset", [1, 0.5, 0, 0, 0, 0, 0, 0, -0.5, -1]);
  this._presets.push(examplePreset);
 
  //register the provider in Nightingale
  var eppm = Cc["@getnightingale.com/equalizer-presets/manager;1"].getService(Ci.ngIEqualizerPresetProviderManager);
  eppm.registerPresetProvider(this);
}
 
ngDefaultPresetProvider.prototype = {
  classDescription: "Example Preset Provider for Nightingale",
  classID:          Components.ID("{Your UUID goes here}"),
  contractID:       "@getnightingale.com/example;1",
  QueryInterface:   XPCOMUtils.generateQI([Ci.ngIEqualizerPresetProvider]),
 
  // this is the array we save the presets in 
  _presets: null,
 
  // this is the preset creation function from the last chapter
  _createPreset: function(name, values) {
    // instantiate a new preset object
    var newPreset = Cc["@getnightingale.com/equalizer-presets/mutable;1"].createInstance(Ci.ngIMutbaleEqualizerPreset);
    // name the preset
    newPreset.setName(name);
    // now follows the interesting part, converting the JS array into the XPCOM compatible nsIArray of nsISupportsDoubles
    var wrappedValues = [];
    for(var i = 0; i < values.length; ++i) { // length should be equal to 10
      var double = Cc["@mozilla.org/supports-double;1"].createInstance(Ci.nsISupportsDouble);
      double.data = values[i];
      wrappedValues.push(double);
    }
    newPreset.setValues(wrappedValues);
 
    // when returning, query interface the preset to the immutable ngIEqualizerPreset so it can't be changed anymore.
    return newPreset.QueryInterface(Ci.ngIEqualizerPreset);
  },
 
  get presets() {
    if(this._presets)
      return ArrayConverter.nsIArray(this._presets);
    else
      throw Components.results.NS_ERROR_UNEXPECTED;
  }
};
 
var NSGetModule = XPCOMUtils.generateNSGetModule([ngDefaultPresetProvider]);
developer_center/recipe_book/extensions_and_core/equalizer_preset_provider.txt · Last modified: 2014/07/26 17:16 by freaktechnik