def codebook_to_audio(
    generated_codes: mx.array, model, delay_pattern, B=1, T=2600, C=9
):
    """Process a single codebook file to generate audio"""
    # Remove BOS token
    generated_codes = generated_codes[:, 1:]

    if generated_codes.shape[1] > T:
        generated_codes = generated_codes[:, :T]

    seq_length = generated_codes.shape[1]

    # Build revert indices
    t_idx_BxTxC, indices_BTCx3 = build_revert_indices(
        B=B, T=seq_length, C=C, delay_pattern=delay_pattern
    )

    # Transpose and add batch dimension
    audio_BxTxC = mx.expand_dims(mx.transpose(generated_codes, (1, 0)), 0)
    reverted_codebook = revert_audio_delay(
        audio_BxTxC=audio_BxTxC,
        pad_value=0,
        precomp=(t_idx_BxTxC, indices_BTCx3),
        T=seq_length,
    )
    reverted_codebook = reverted_codebook[:, :-30, :]

    codebook = mx.transpose(reverted_codebook, (0, 2, 1))

    min_valid_index = 0
    max_valid_index = 1023
    invalid_mask = (codebook < min_valid_index) | (codebook > max_valid_index)

    num_invalid = mx.sum(invalid_mask).item()
    if num_invalid > 0:
        print(
            f"Warning: Clamping {num_invalid} indices outside range [{min_valid_index}, {max_valid_index}] to 0."
        )

    # Set invalid values to 0
    zeros = mx.zeros_like(codebook)
    codebook = mx.where(invalid_mask, zeros, codebook)

    audio_array = decode(model, codebook)

    return audio_array
Process a single codebook file to generate audio
mlx_audio/tts/models/dia/audio.py