Python Example

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