7

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.

Sektor
  • 3,320
  • 7
  • 27
  • 36

1 Answers1

7

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.

Karsten7
  • 27,448
  • 5
  • 73
  • 134
  • 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. – cruiser0631 Jun 21 '15 at 13:00