# SNDEX_SetVolume

## Syntax

  `#include <twl/snd.h>`

      SNDEXResult SNDEX_SetVolume( u8 volume );

## Arguments

volume | Speaker volume. You can specify any value between 0 (defined as `SNDEX_VOLUME_MIN`) and 7 (defined as `SNDEX_VOLUME_MAX`).

## Return Values

`SNDEX_RESULT_SUCCESS` | The whole series of operations completed successfully.
`SNDEX_RESULT_ILLEGAL_STATE` | This function has been called on the NITRO platform or from an exception handler.
`SNDEX_RESULT_INVALID_PARAM` | A value outside of the specifiable range is given for volume.
`SNDEX_RESULT_BEFORE_INIT` | Extended sound features have not been initialized by the `SNDEX_Init` function.

`SNDEX_RESULT_EXCLUSIVE`

Processing for other extended sound features is running. Note that mutexes are used for each of the extended sound features, so they cannot be used simultaneously.
This is also returned when a mutex error occurred in the ARM7. When this is the case, the situation may improve on a retry because the error is dependent on the progress of processing in ARM7.
As mentioned in the SNDEX Library Overview, the error also returns during processing of shutter sound playback.

`SNDEX_RESULT_PXI_SEND_ERROR` | A PXI command failed to be sent to the ARM7. This error occurs when the PXI send queue for the ARM7 has filled up or when the hardware has detected an error. As a result, the state may improve when a retry is made.
`SNDEX_RESULT_DEVICE_ERROR` | The ARM7 failed to change the speaker volume for an external device. Conditions may improve on a retry, but continued failures after multiple retries could indicate that the external device is not responding, so you may handle this as if it were a return value indicating success.
`SNDEX_RESULT_FATAL_ERROR` | This error should not ordinarily occur. This may occur when an illegal PXI command was sent directly, ignoring library state management; when the ARM7 component and the extended sound feature library have different versions; when library state management has fallen into an abnormal state due to memory corruption; or when other such conditions arise. There is no way to recover from this at run time.

## Description

The function synchronously changes the speaker volume. The changes are not necessarily applied immediately.For more information, see Note, below.

In addition to this function, the `SNDEX_SetVolumeAsync` function and the console's volume switch can be used to change the speaker volume. The console records the changed value and also applies it at the next restart.

This function calls the `SNDEX_SetVolumeAsync` function internally and stops its own calling thread until asynchronous processing has completed. This function expects to be called from a thread, so it fails if it is called from an exception handler. Note that this function continues to block internally if PXI receive interrupts are prohibited, because PXI receive interrupts are used to detect the processing's completion.

## Note

Be sure that your method for changing the volume adheres to the Programming Guidelines.

The speaker volume is saved in dedicated nonvolatile memory each time it is changed. Consequently, you cannot change the speaker volume an unlimited number of times.Avoid having your program make frequent changes to the speaker volume.

The library controls the execution of this function to control the number times the volume is changed.
If the function is called during the roughly 10 frames (approximately 167 ms) since the volume was last changed, the new value is not immediately applied but instead held pending. Even if this is the case, the function returns `SNDEX_RESULT_SUCCESS`.
When the storage time for this pending change has ended, the speaker volume is changed to the value specified in the argument of the last-called `SNDEX_SetVolume[Async]` function.
The value returned by the `SNDEX_GetVolume[Async]` function is the pending change to the speaker volume. If no value is pending, the currently set speaker volume is returned.

## See Also

`SNDEX_SetVolumeAsync`
`SNDEX_GetVolume`

## Revision History

2009/02/03 Added mention of the Programming Guidelines in Note.
2009/01/06 Added mention of library control over changes in speaker volume.
2008/11/07 Added mention that `SNDEX_RESULT_EXCLUSIVE` is returned during shutter sound playback.
2008/07/10 Revised the explanation about speaker volume.
2008/02/22 Standardized terminology regarding speaker volume and added Description.
2008/02/15 Initial version.

CONFIDENTIAL