Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/facebookresearch/audioseal/llms.txt

Use this file to discover all available pages before exploring further.

Overview

The AudioSealWM class generates watermarks that can be embedded into audio signals. It supports both 0-bit watermarking (presence detection only) and n-bit watermarking (with secret messages).

Initialization

from audioseal import AudioSeal

# Load a pre-trained generator
generator = AudioSeal.load_generator("audioseal_wm_16bits")
Typically, you’ll load a generator using AudioSeal.load_generator() rather than instantiating directly.

Methods

get_watermark

Generate a watermark signal for the given audio without applying it.
import torch
import torchaudio

# Load audio
audio, sr = torchaudio.load("audio.wav")

# Generate watermark
watermark = generator.get_watermark(audio)

# Manually apply with custom strength
watermarked = audio + 0.5 * watermark

Parameters

x
torch.Tensor
required
Input audio tensor of shape (batch, channels, samples) or (batch, samples). The audio should be at the model’s expected sample rate (typically 16kHz).
sample_rate
int
default:"None"
Sample rate of the input audio. This parameter is deprecated and will be ignored in AudioSeal 0.2+. Ensure your audio is at the correct sample rate before calling this method.
message
torch.Tensor
default:"None"
Binary message tensor of shape (batch, nbits) or (nbits,). If None, a random message will be generated. Values should be 0 or 1.

Returns

watermark
torch.Tensor
The generated watermark signal with the same shape as the input audio.

Example

import torch
import torchaudio

# Load audio
audio, sr = torchaudio.load("audio.wav")

# Get watermark without message (random message)
watermark = generator.get_watermark(audio)

# Get watermark with specific message
message = torch.tensor([1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0])
watermark = generator.get_watermark(audio, message=message)

# Apply with custom strength
alpha = 0.8
watermarked = audio + alpha * watermark

forward

Generate and apply a watermark to the audio signal in one step.
# Apply watermark to audio
watermarked_audio = generator(audio, message=message, alpha=1.0)

Parameters

x
torch.Tensor
required
Input audio tensor of shape (batch, channels, samples) or (batch, samples).
sample_rate
int
default:"None"
Sample rate of the input audio. This parameter is deprecated and will be ignored.
message
torch.Tensor
default:"None"
Binary message tensor of shape (batch, nbits) or (nbits,). If None, uses a random message.
alpha
float
default:"1.0"
Watermark strength multiplier. Values between 0.0 and 1.0. Lower values make the watermark more subtle but potentially less robust.

Returns

watermarked_audio
torch.Tensor
The watermarked audio with the same shape as the input.

Example

import torch
import torchaudio
from audioseal import AudioSeal

# Load model and audio
generator = AudioSeal.load_generator("audioseal_wm_16bits")
audio, sr = torchaudio.load("audio.wav")

# Apply watermark with default strength
watermarked = generator(audio)

# Apply with custom message and strength
message = torch.randint(0, 2, (16,))  # 16-bit message
watermarked = generator(audio, message=message, alpha=0.8)

# Save result
torchaudio.save("watermarked.wav", watermarked, sr)

streaming

Context manager for streaming mode processing.
with generator.streaming(batch_size=1):
    for chunk in audio_chunks:
        watermarked_chunk = generator(chunk)

Parameters

batch_size
int
required
Number of parallel streams to process simultaneously.

Returns

context
ContextManager
A context manager that enables streaming mode for the encoder.

Example

import torch
from audioseal import AudioSeal

# Load generator
generator = AudioSeal.load_generator("audioseal_wm_16bits")

# Process audio in streaming mode
chunk_size = 16000  # 1 second at 16kHz
audio_chunks = audio.split(chunk_size, dim=-1)

watermarked_chunks = []
with generator.streaming(batch_size=1):
    for chunk in audio_chunks:
        watermarked = generator(chunk)
        watermarked_chunks.append(watermarked)

# Concatenate results
watermarked_audio = torch.cat(watermarked_chunks, dim=-1)
Streaming mode is only available in AudioSeal 0.2+ with Python 3.10+. A NotImplementedError will be raised if the feature is not available.

random_message

Generate a random binary message for watermarking.
# Generate random message for batch
message = generator.random_message(bsz=4)

Parameters

bsz
int
required
Batch size for the generated messages.

Returns

message
torch.Tensor
Random binary tensor of shape (bsz, nbits) with values 0 or 1.

Attributes

encoder
torch.nn.Module
The SEANet encoder that processes input audio into hidden representations.
decoder
torch.nn.Module
The SEANet decoder that generates the watermark signal from hidden representations.
msg_processor
MsgProcessor
The message processor that embeds the secret message into the hidden representation. None for 0-bit watermarking.
normalizer
NormalizationProcessor
Optional normalizer that fits the watermark within the audio envelope for improved imperceptibility.

Complete Example

import torch
import torchaudio
from audioseal import AudioSeal

# Load the generator
generator = AudioSeal.load_generator(
    "audioseal_wm_16bits",
    device="cuda"
)

# Load audio
audio, sr = torchaudio.load("input.wav")

# Create a custom 16-bit message
message = torch.tensor([
    1, 0, 1, 1, 0, 0, 1, 0,
    1, 1, 0, 1, 0, 1, 1, 0
])

# Generate watermarked audio
watermarked = generator(
    audio,
    message=message,
    alpha=0.8  # 80% watermark strength
)

# Save the result
torchaudio.save("watermarked.wav", watermarked.cpu(), sr)

print(f"Watermarked audio shape: {watermarked.shape}")
print(f"Message embedded: {message}")

See Also

Build docs developers (and LLMs) love