1import iris.sdk
2import numpy as np
3import soundfile as sf
4from pathlib import Path
5
6from utils import check_licence_info, check_context_config, check_context_request, create_output_filepath #, file_to_string
7
8
9def process_callback(event):
10 match event.type:
11 case iris.sdk.ProcessEventType.start:
12 print(f"\rProcessing: started")
13 case iris.sdk.ProcessEventType.process:
14 print(f"Processing: {event.percentage:.2f}%", end='\r')
15 case iris.sdk.ProcessEventType.end:
16 print(f"\nProcessing: ended")
17
18
19if __name__ == "__main__":
20 # initialise iris sdk. either pass in the licence and key as strings or let the sdk discover
21 # them from its default data location. See readme.md for default locations
22 # licence = file_to_string("/full/path/to/iris.lic")
23 # key = file_to_string("/full/path/to/iris.key")
24 # info = iris.sdk.init(licence, key)
25 info = iris.sdk.init()
26 print(f"Initialised iris-sdk {info.version} for {info.name}")
27 check_licence_info(info)
28
29 # Load a wav file and fetch the sample rate and data format
30 script_dir = Path(__file__).resolve().parent
31 input_filepath = script_dir.parent / "sample_audio" / "breaking_point_f32_48k.wav"
32 audio_in, sample_rate = sf.read(input_filepath, dtype='float32')
33 file_info = sf.info(input_filepath)
34 original_format = file_info.subtype
35
36 # prepare an output buffer to hold the processed audio
37 audio_out = np.zeros_like(audio_in, dtype=np.float32)
38
39 # Create an audio processing context request
40 context_request = iris.sdk.AudioContextRequest()
41 context_request.processors = ["passthrough"] # set the processor chain to use
42 context_request.sample_rate = sample_rate # set the sample rate of the input audio
43 context_request.channel_count = file_info.channels # set the channel count of the input audio
44 context_request.resampler_mode = iris.sdk.ResamplerMode.HQ # set the resampler quality (HQ or SPEED)
45 check_context_request(context_request)
46
47 # Create an audio processing context using the context request
48 config = iris.sdk.create_audio_context(context_request)
49 check_context_config(config)
50
51 # Process the entire buffer in one go, accounting for any transport delay
52 # If processing a stream, call iris.sdk.process when each new buffer of stream data arrives instead
53 # In the streaming case, frames should be set to the size of the stream buffer size.
54 # If each buffer is the same size, set fixed_frame_count to true in the context request
55 # If the stream buffer size is known up front, set the buffer size in the context request
56 # streaming pseudocode
57 # while new_stream_data_available:
58 # process_in_buffer = get_stream_source()
59 # process_out_buffer = np.zeros_like(process_in_buffer, dtype=np.float32)
60 # iris.sdk.process(config.context_id, process_in_buffer, process_out_buffer, len(process_in_buffer))
61 # put_stream_sink(process_out_buffer)
62
63 print(f"Processing {input_filepath}")
64 iris.sdk.process_offline(config.context_id, audio_in, audio_out, file_info.frames, process_callback)
65
66 # save the processed output alongside the input file in the original file format
67 output_filepath = create_output_filepath(input_filepath, config.description)
68 sf.write(output_filepath, audio_out, sample_rate, subtype=original_format)
69 print(f"Saving {output_filepath}")
70
71 # Release the context
72 print(f"Cleaning up...")
73 iris.sdk.release_audio_context(config.context_id)
74 iris.sdk.cleanup()
75 print(f"Done.")