music.set_sample_rate

music.set_sample_rate(rate)[source]

Set the sample rate routines use when they are not given one.

Every routine that takes a sample_rate defaults it to None, which means this rate. Called on its own line, the setting lasts for the rest of the thread or asyncio task; as the subject of a with statement, it lasts for the block, and the rate before it comes back when the block ends, however it ends. Either way it applies immediately, so there is no form of the call that sets nothing.

A sample_rate given to a routine always wins over this one, and an object that renders later – a Sequencer, a StimulationSession, an oscillator – keeps the rate that was in effect when it was made.

Parameters:
rateint

Samples per second, a positive whole number.

Returns:
context manager

The setting, which a with statement undoes on exit; as binds the rate. Use it in one with statement at most.

Raises:
ValueError

If rate is not a positive whole number. A fractional rate, or a boolean, is refused rather than truncated: nothing that writes a file could keep it.

Parameters:

rate (int)

Return type:

_RateSetting

See also

get_sample_rate

the rate in effect.

Examples

>>> with set_sample_rate(48000):
...     len(note(duration=1))
48000
>>> len(note(duration=1))
44100