But it’s the sound library that actually modulates the pins, so it has to know which ones they are, anyway. On that note, you should be using the defines PIN_SPEAKER_1 and/or PIN_SPEAKER_2 in your library rather than just hard coding them.
The reason that the sound library should be responsible for setting up and controlling the speaker pin modes is because the “main” library may not know how to, for a particular sound library.
For instance, with the current Arduboy hardware it’s possible to set one pin as an output and the other as an “input with pullup” (mode INPUT_PULLUP). If you then toggle only the output pin, the speaker is coupled to the input pin through the built in pullup resistor. This results in a lower volume. I’ve talked about this here. A particular sound library could take advantage of this. However, if the “main” library changes the pin modes each time it changes the mute state, this could screw up this “non-standard” pin configuration that the sound library has set.
I’ve proposed that the next generation Arduboy be based on the Arduino Zero (and I’ve seen hints that it may be). This will mean that there will probably be a true hardware DAC available for sound. Some sound libraries may use this. Others may just toggle the pin digitally as they do now. Again, you don’t want the “main” library messing with the pin configuration that the sound library has set up.
For the above reasons, I’m really tempted to change the Arduboy2 library so that it never touches the speaker pins (or at most just sets them both to outputs once, during boot). Required pin settings would be the sole responsibility of the sound library. The “main” library’s audio class would only logically maintain the global mute state, with no sound hardware control whatsoever. In the future, it’s possible that the audio class could maintain a global volume level, in addition to the mute state, but this would still only provide a volume level value to the sound library, and the sound library would control the hardware to provide this volume, within it’s capabilities.
If I were to make this change to the Arduboy2 library, the ArduboyTones and ArduboyPlaytune libraries would still work fine because they both set the speaker pin modes themselves and use a callback to audio.enabled() to handle global muting themselves, as they see fit.