Usage Examples#
Create and Record Audio to File#
import omni.audiorecorder
import carb.audio
import time
# Create an audio recorder instance
recorder = omni.audiorecorder.create_audio_recorder()
# Start recording directly to a file
result = recorder.begin_recording_to_file(
frame_rate=48000, # 48kHz sample rate
channels=2, # Stereo recording
filename="recording.wav", # Output file path
output_format=carb.audio.SampleFormat.PCM16 # 16-bit PCM WAV format
)
if result:
print("Recording started successfully!")
# Record for 5 seconds
time.sleep(5)
# Stop recording and save the file
recorder.stop_recording(flush=True)
print("Recording stopped and saved")
else:
print("Failed to start recording")
Record Audio with Callback Function#
import omni.audiorecorder
import carb.audio
import time
# Create an audio recorder instance
recorder = omni.audiorecorder.create_audio_recorder()
# Callback function to process audio data
def audio_callback(data):
# 'data' contains audio samples that can be processed
print(f"Received {len(data)} audio samples")
# Process audio data as needed
# e.g., calculate audio levels, analyze frequency content, etc.
# Start recording with float PCM format and a callback
result = recorder.begin_recording_float(
callback=audio_callback, # Function to call with audio data
frame_rate=48000, # 48kHz sample rate
channels=2, # Stereo recording
buffer_length=4800, # Buffer size in frames
period=1200, # Callback period in frames
length_type=carb.audio.UnitType.FRAMES,
filename="recording_with_callback.wav", # Optional: also save to file
)
if result:
print("Recording started with callback")
# Record for 5 seconds
time.sleep(5)
# Stop recording
recorder.stop_recording()
print("Recording stopped")
else:
print("Failed to start recording with callback")
Draw Audio Waveforms from Different Sample Types#
import omni.audiorecorder
import math
from PIL import Image
# Generate some sample audio data (a sine wave)
sample_count = 4800 # 0.1 seconds at 48kHz
samples_float = [math.sin(i / 48.0) for i in range(sample_count)]
samples_int16 = [int(s * (2**15 - 1)) for s in samples_float]
samples_int32 = [int(s * (2**31 - 1)) for s in samples_float]
# Set waveform dimensions
width = 256
height = 128
# Set colors (RGBA format 0.0-1.0)
foreground_color = [1.0, 1.0, 1.0, 1.0] # White
background_color = [0.0, 0.0, 0.0, 1.0] # Black
# Draw waveform from float data
float_image_data = omni.audiorecorder.draw_waveform_from_blob_float(
samples_float, 1, width, height, foreground_color, background_color
)
# Create and save the image
with Image.frombytes("RGBA", (width, height), bytes(float_image_data), 'raw') as img:
img.save("waveform_float.png")
# Draw waveform from int16 data
int16_image_data = omni.audiorecorder.draw_waveform_from_blob_int16(
samples_int16, 1, width, height, foreground_color, background_color
)
# Create and save the image
with Image.frombytes("RGBA", (width, height), bytes(int16_image_data), 'raw') as img:
img.save("waveform_int16.png")
# Draw waveform from int32 data
int32_image_data = omni.audiorecorder.draw_waveform_from_blob_int32(
samples_int32, 1, width, height, foreground_color, background_color
)
# Create and save the image
with Image.frombytes("RGBA", (width, height), bytes(int32_image_data), 'raw') as img:
img.save("waveform_int32.png")