Skip to content

Commit 55b904f

Browse files
committed
possible example for frame_process_method
1 parent 0e09888 commit 55b904f

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,8 @@ this generator as a stream source. The data can be provided in ``array`` type o
279279
the audio data has, and the sample with in bytes.
280280

281281

282-
*function* ``stream_with_callbacks (sample_stream: Generator[Union[bytes, array.array], int, NoneType], progress_callback: Optional[Callable[[int], NoneType]] = None, frame_process_method: Union[Callable[[array.array], None], None] = None, end_callback: Optional[Callable] = None) -> Generator[Union[bytes, array.array], int, NoneType]``
283-
> Convenience generator function to add callback and processing functionality to another stream. You can specify a:
282+
*function* ``stream_with_callbacks (sample_stream: Generator[Union[bytes, array.array], int, NoneType], progress_callback: Optional[Callable[[int], NoneType]] = None, frame_process_method: Union[Callable[[array.array], array.array], None] = None, end_callback: Optional[Callable] = None) -> Generator[Union[bytes, array.array], int, NoneType]``
283+
> Convenience generator function to add callback and processing functionality to another stream. You can specify:
284284
A callback function that gets called during play and takes an ``int``
285285
for the number of frames played.
286286
A function that can be used to process raw data frames before they are yielded back

examples/playcallbacks.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import sys
66
import miniaudio
7+
import array
78

89

910
def stream_end_callback() -> None:
@@ -14,11 +15,18 @@ def stream_progress_callback(framecount: int) -> None:
1415
print(framecount, ",", end="", flush=True)
1516

1617

18+
def process_method(samples: array.array) -> array.array:
19+
# For example: silence the left stereo channel:
20+
# for i in range(0, len(samples), 2):
21+
# samples[i] = 0
22+
return samples
23+
24+
1725
if __name__ == "__main__":
1826
if len(sys.argv) != 2:
1927
raise SystemExit("use one argument: filename")
2028
filestream = miniaudio.stream_file(sys.argv[1])
21-
callbacks_stream = miniaudio.stream_with_callbacks(filestream, stream_progress_callback, stream_end_callback)
29+
callbacks_stream = miniaudio.stream_with_callbacks(filestream, stream_progress_callback, process_method, stream_end_callback)
2230
next(callbacks_stream) # start the generator
2331
with miniaudio.PlaybackDevice() as device:
2432
print("playback in background. press enter to exit:\n")

miniaudio.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1268,7 +1268,7 @@ def on_close() -> None:
12681268

12691269
def stream_with_callbacks(sample_stream: PlaybackCallbackGeneratorType,
12701270
progress_callback: Union[Callable[[int], None], None] = None,
1271-
frame_process_method: Union[Callable[[array.array], None], None] = None,
1271+
frame_process_method: Union[Callable[[array.array], array.array], None] = None,
12721272
end_callback: Union[Callable, None] = None) -> PlaybackCallbackGeneratorType:
12731273
"""
12741274
Convenience generator function to add callback and processing functionality to another stream.

0 commit comments

Comments
 (0)