|
1 | 1 | #!/usr/bin/python |
2 | 2 | # -*- coding: utf-8 -*- |
3 | 3 | """ |
4 | | -This module contains methods to synthesize Partitura object to wav using |
5 | | -additive synthesis |
| 4 | +This module contains methods to synthesize Partitura object to wav. |
6 | 5 | """ |
7 | 6 | from typing import Union, Optional, Callable, Dict, Any |
8 | 7 | import numpy as np |
|
13 | 12 | from partitura.performance import PerformanceLike |
14 | 13 |
|
15 | 14 | from partitura.utils.synth import synthesize, SAMPLE_RATE, A4 |
| 15 | +from partitura.utils.fluidsynth import ( |
| 16 | + synthesize_fluidsynth, |
| 17 | + DEFAULT_SOUNDFONT, |
| 18 | + HAS_FLUIDSYNTH, |
| 19 | +) |
16 | 20 |
|
17 | 21 | from partitura.utils.misc import PathLike |
18 | 22 |
|
19 | | -__all__ = ["save_wav"] |
| 23 | +__all__ = [ |
| 24 | + "save_wav", |
| 25 | + "save_wav_fluidsynth", |
| 26 | +] |
20 | 27 |
|
21 | 28 |
|
22 | 29 | def save_wav( |
@@ -89,8 +96,76 @@ def save_wav( |
89 | 96 | bpm=bpm, |
90 | 97 | ) |
91 | 98 |
|
| 99 | + if out is not None: |
| 100 | + # convert to 16bit integers (save as PCM 16 bit) |
| 101 | + # (some DAWs cannot load audio files that are float64, |
| 102 | + # e.g., Logic) |
| 103 | + amplitude = np.iinfo(np.int16).max |
| 104 | + if abs(audio_signal).max() <= 1: |
| 105 | + # convert to 16bit integers (save as PCM 16 bit) |
| 106 | + amplitude = np.iinfo(np.int16).max |
| 107 | + audio_signal *= amplitude |
| 108 | + wavfile.write(out, samplerate, audio_signal.astype(np.int16)) |
| 109 | + |
| 110 | + else: |
| 111 | + return audio_signal |
| 112 | + |
| 113 | + |
| 114 | +def save_wav_fluidsynth( |
| 115 | + input_data: Union[ScoreLike, PerformanceLike, np.ndarray], |
| 116 | + out: Optional[PathLike] = None, |
| 117 | + samplerate: int = SAMPLE_RATE, |
| 118 | + soundfont: PathLike = DEFAULT_SOUNDFONT, |
| 119 | + bpm: Union[float, np.ndarray, Callable] = 60, |
| 120 | +) -> Optional[np.ndarray]: |
| 121 | + """ |
| 122 | + Export a score (a `Score`, `Part`, `PartGroup` or list of `Part` instances), |
| 123 | + a performance (`Performance`, `PerformedPart` or list of `PerformedPart` instances) |
| 124 | + as a WAV file using fluidsynth |
| 125 | +
|
| 126 | + Parameters |
| 127 | + ---------- |
| 128 | + input_data : ScoreLike, PerformanceLike or np.ndarray |
| 129 | + A partitura object with note information. |
| 130 | + out : PathLike or None |
| 131 | + Path of the output Wave file. If None, the method outputs |
| 132 | + the audio signal as an array (see `audio_signal` below). |
| 133 | + samplerate: int |
| 134 | + The sample rate of the audio file in Hz. The default is 44100Hz. |
| 135 | + soundfont : PathLike |
| 136 | + Path to the soundfont in SF2/SF3 format for fluidsynth. |
| 137 | + bpm : float, np.ndarray, callable |
| 138 | + The bpm to render the output (if the input is a score-like object). |
| 139 | + See `partitura.utils.music.performance_notearray_from_score_notearray` |
| 140 | + for more information on this parameter. |
| 141 | +
|
| 142 | + Returns |
| 143 | + ------- |
| 144 | + audio_signal : np.ndarray |
| 145 | + Audio signal as a 1D array. Only returned if `out` is None. |
| 146 | + """ |
| 147 | + |
| 148 | + if not HAS_FLUIDSYNTH: |
| 149 | + raise ImportError("Fluidsynth is not installed!") |
| 150 | + |
| 151 | + audio_signal = synthesize_fluidsynth( |
| 152 | + note_info=input_data, |
| 153 | + samplerate=samplerate, |
| 154 | + soundfont=soundfont, |
| 155 | + bpm=bpm, |
| 156 | + ) |
| 157 | + |
92 | 158 | if out is not None: |
93 | 159 | # Write audio signal |
94 | | - wavfile.write(out, samplerate, audio_signal) |
| 160 | + |
| 161 | + # convert to 16bit integers (save as PCM 16 bit) |
| 162 | + # (some DAWs cannot load audio files that are float64, |
| 163 | + # e.g., Logic) |
| 164 | + amplitude = np.iinfo(np.int16).max |
| 165 | + if abs(audio_signal).max() <= 1: |
| 166 | + # convert to 16bit integers (save as PCM 16 bit) |
| 167 | + amplitude = np.iinfo(np.int16).max |
| 168 | + audio_signal *= amplitude |
| 169 | + wavfile.write(out, samplerate, audio_signal.astype(np.int16)) |
95 | 170 | else: |
96 | 171 | return audio_signal |
0 commit comments