7
$\begingroup$

Just wanna figure out if there is a decent way to add a same option to multiple functions simultaneously.

For example I have a series of SoundNote:

Sound[{SoundNote["D4", {0.4, 0.2}], SoundNote["G4", {0.6, 0.2}], SoundNote["B4", {0.8, 0.2}], SoundNote["D5", {1, 0.2}], SoundNote["A4", {1.2, 0.2}], SoundNote["E4", {1.4, 0.2}], SoundNote["G4", {1.6, 0.2}], SoundNote["A4", {1.8, 0.2}]}]

And now I want to add an option SoundVolume -> 1/2 to the arguments of all functions in the list and make it like:

Sound[{SoundNote["D4", {0.4, 0.2},SoundVolume->1/2], SoundNote["G4", {0.6, 0.2},SoundVolume->1/2], SoundNote["B4", {0.8, 0.2},SoundVolume->1/2], SoundNote["D5", {1, 0.2},SoundVolume->1/2], SoundNote["A4", {1.2, 0.2},SoundVolume->1/2], SoundNote["E4", {1.4, 0.2},SoundVolume->1/2], SoundNote["G4", {1.6, 0.2},SoundVolume->1/2], SoundNote["A4", {1.8, 0.2},SoundVolume->1/2]}]

Is there any way to add the option instead of adding them one by one?

I've tried combining pure functions and Append like this:

Sound[Append[#,SoundVolume->1/2]& /@{SoundNote["D4", {0.4, 0.2}], SoundNote["G4", {0.6, 0.2}], SoundNote["B4", {0.8, 0.2}], SoundNote["D5", {1, 0.2}], SoundNote["A4", {1.2, 0.2}], SoundNote["E4", {1.4, 0.2}], SoundNote["G4", {1.6, 0.2}], SoundNote["A4", {1.8, 0.2}]}]

But it didn't work as it seems like the compiler sees SoundVolume -> 1/2 as an option for Append.

$\endgroup$
  • 1
    $\begingroup$ Try a replacement rule: list /. SoundNote[args__] :> SoundNote[args, SoundVolume -> 1/2]. $\endgroup$ – J. M. is away Jun 21 '15 at 9:47
  • 1
    $\begingroup$ In this specific case you should use SoundVolume->1/2 as an option for Sound instead of an option for every single SoundNote. $\endgroup$ – Karsten 7. Jun 21 '15 at 9:52
  • 3
    $\begingroup$ You could also change the default option of SoundNote using SetOptions: SetOptions[SoundNote, SoundVolume -> 1/2]. $\endgroup$ – Karsten 7. Jun 21 '15 at 9:54
  • $\begingroup$ @Karsten, methinks your second comment is the best solution for this. Maybe expand on it a bit and make it an answer? $\endgroup$ – J. M. is away Jun 21 '15 at 10:00
  • 2
    $\begingroup$ cruiser, your code with Append works in version 10.1.0 under Windows. Which version are you using? $\endgroup$ – Mr.Wizard Jun 21 '15 at 10:34
7
$\begingroup$

The default option for SoundNote is

Options[SoundNote]

{SoundVolume -> 1}

One can change the default using SetOptions

SetOptions[SoundNote, SoundVolume -> 1/2]

Now all following SoundNote function calls will use the option SoundVolume -> 1/2.

$\endgroup$
  • $\begingroup$ Thanks a lot Karsten and Guess. I think SetOptions is perfect for one needing to change the option globally while the replacement rule is good for local modifications. $\endgroup$ – cruiser0631 Jun 21 '15 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.