Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tools: Tune: Collection of patches to produce ALSA UCM friendly binary control blobs #9070

Merged
merged 6 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions tools/tune/common/sof_ucm_blob_write.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
function sof_ucm_blob_write(fn, blob8)

% Export blob to UCM2 cset-tlv binary format
%
% sof_ucm_blob_write(fn, blob)
%
% Input parameters
% fn - Filename for the blob
% blob - Vector of data with uint8 type
%

% SPDX-License-Identifier: BSD-3-Clause
%
% Copyright (c) 2024, Intel Corporation. All rights reserved.

% Export for UCM cset-tlv with additional 8 bytes header
SOF_CTRL_CMD_BINARY = 3;
nh = 8;
nb = length(blob8);
ublob8 = zeros(nb + nh, 1, 'uint8');
ublob8(1:4) = w32b(SOF_CTRL_CMD_BINARY);
ublob8(5:8) = w32b(nb);
ublob8(9:end) = blob8;

%% Write blob
check_create_dir(fn);
fh = fopen(fn, 'wb');
fwrite(fh, ublob8, 'uint8');
fclose(fh);

%% Print as 8 bit hex
nb = length(ublob8);
nl = ceil(nb/16);
for i = 1:nl
m = min(16, nb-(i-1)*16);
for j = 1:m
fprintf(1, "%02x ", ublob8((i-1)*16 + j));
end
fprintf(1, "\n");
end

fprintf(1, "\n");
end

function bytes = w32b(word)
sh = [0 -8 -16 -24];
bytes = uint8(zeros(1,4));
bytes(1) = bitand(bitshift(word, sh(1)), 255);
bytes(2) = bitand(bitshift(word, sh(2)), 255);
bytes(3) = bitand(bitshift(word, sh(3)), 255);
bytes(4) = bitand(bitshift(word, sh(4)), 255);
end
18 changes: 12 additions & 6 deletions tools/tune/crossover/example_crossover.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ function export_crossover(cr)
endian = "little";
tpath1 = '../../topology/topology1/m4/crossover';
tpath2 = '../../topology/topology2/include/components/crossover';
ctlpath = '../../ctl/ipc3';
ctlpath3 = '../../ctl/ipc3/crossover';
ctlpath4 = '../../ctl/ipc4/crossover';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@singalsu presumably an OEM or integrator will have multiple SKUs to tune. Should there be some sort of prefix for the blobs so that the storage can be specified as SKU-specific?

And while I am at it, can those tuning scripts actually be used in a script that generates blobs for multiple platforms, just taking input parameters for each SKU?


str_way = sprintf('%dway', cr.num_sinks);
str_freq = get_str_freq(cr);
Expand All @@ -49,8 +50,10 @@ function export_crossover(cr)
tplg1_fn = sprintf('%s/coef_%s_%s_%s.m4', tpath1, str_way, str_freq, str_pid); % Control Bytes File
tplg2_fn = sprintf('%s/coef_%s_%s_%s.conf', tpath2, str_way, str_freq, str_pid);
% Use those files with sof-ctl to update the component's configuration
blob_fn = sprintf('%s/crossover_coef_%dway.blob', ctlpath, cr.num_sinks); % Blob binary file
alsa_fn = sprintf('%s/crossover_coef_%dway.txt', ctlpath, cr.num_sinks); % ALSA CSV format file
blob3_fn = sprintf('%s/coef_%dway.blob', ctlpath3, cr.num_sinks); % Blob binary file
alsa3_fn = sprintf('%s/coef_%dway.txt', ctlpath3, cr.num_sinks); % ALSA CSV format file
blob4_fn = sprintf('%s/coef_%dway.blob', ctlpath4, cr.num_sinks); % Blob binary file
alsa4_fn = sprintf('%s/coef_%dway.txt', ctlpath4, cr.num_sinks); % ALSA CSV format file

% This array is an example on how to assign a buffer from pipeline 1 to output 0,
% buffer from pipeline 2 to output 1, etc...
Expand Down Expand Up @@ -85,11 +88,14 @@ function export_crossover(cr)

mkdir_check(tpath1);
mkdir_check(tpath2);
mkdir_check(ctlpath);
mkdir_check(ctlpath3);
mkdir_check(ctlpath4);
tplg_write(tplg1_fn, blob8, "CROSSOVER");
tplg2_write(tplg2_fn, blob8_ipc4, "crossover_config", 'Exported Control Bytes');
blob_write(blob_fn, blob8);
alsactl_write(alsa_fn, blob8);
sof_ucm_blob_write(blob3_fn, blob8);
sof_ucm_blob_write(blob4_fn, blob8_ipc4);
alsactl_write(alsa3_fn, blob8);
alsactl_write(alsa4_fn, blob8_ipc4);

% Plot Magnitude and Phase Response of each sink
crossover_plot_freq(crossover.lp, crossover.hp, cr.fs, cr.num_sinks);
Expand Down
4 changes: 2 additions & 2 deletions tools/tune/dcblock/example_dcblock.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ function dcblock_blob_calculate(prm)
tplg_write(tplg1_fn, blob8, "DCBLOCK", ...
"Exported with script example_dcblock.m", ...
"cd tools/tune/dcblock; octave example_dcblock.m");
blob_write(blob3_fn, blob8);
sof_ucm_blob_write(blob3_fn, blob8);
alsactl_write(alsa3_fn, blob8);

tplg2_write(tplg2_fn, blob8_ipc4, "dcblock_config", ...
"Exported with script example_dcblock.m" , ...
"cd tools/tune/dcblock; octave example_dcblock.m");
blob_write(blob4_fn, blob8_ipc4);
sof_ucm_blob_write(blob4_fn, blob8_ipc4);
alsactl_write(alsa4_fn, blob8_ipc4);

% Plot Filter's Transfer Function and Step Response
Expand Down
4 changes: 2 additions & 2 deletions tools/tune/drc/example_drc.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ function drc_coefs_and_config_export(params, id)
drc_howto = sprintf("cd tools/tune/drc; octave --no-window-system %s.m", my_name);
tplg_write(tplg1_fn, blob8, "DRC", drc_note, drc_howto);
tplg2_write(tplg2_fn, blob8_ipc4, "drc_config", drc_note, drc_howto);
blob_write(blob3_fn, blob8);
sof_ucm_blob_write(blob3_fn, blob8);
alsactl_write(alsa3_fn, blob8);
blob_write(blob4_fn, blob8_ipc4);
sof_ucm_blob_write(blob4_fn, blob8_ipc4);
alsactl_write(alsa4_fn, blob8_ipc4);

% Plot x-y response in dB
Expand Down
49 changes: 0 additions & 49 deletions tools/tune/eq/eq_blob_write.m

This file was deleted.

28 changes: 16 additions & 12 deletions tools/tune/eq/example_fir_eq.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@ function example_fir_eq()

%% Common definitions
fs = 48e3;
fn.cpath3 = '../../ctl/ipc3';
fn.cpath4 = '../../ctl/ipc4';
fn.cpath3 = '../../ctl/ipc3/eq_fir';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no check to see if these directories exist before writing files into them, which may lead to errors. Can you consider adding checks and possibly creating directories that do not currently exist?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to leave to another PR. There could be a function dir = sof_directory_create_check(dir);

fn.cpath4 = '../../ctl/ipc4/eq_fir';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here - same as well?

fn.tpath1 = '../../topology/topology1/m4';
fn.tpath2 = '../../topology/topology2/include/components/eqfir';
fn.priv = 'DEF_EQFIR_PRIV';

addpath ../common

%% -------------------
%% Example 1: Loudness
%% -------------------
fn.bin = 'eq_fir_loudness.bin';
fn.txt = 'eq_fir_loudness.txt';
fn.bin = 'loudness.blob';
fn.txt = 'loudness.txt';
fn.tplg1 = 'eq_fir_coef_loudness.m4';
fn.tplg2 = 'loudness.conf';
comment = 'Loudness effect, created with example_fir_eq.m';
Expand Down Expand Up @@ -50,8 +52,8 @@ function example_fir_eq()
%% -------------------
%% Example 2: Mid boost
%% -------------------
fn.bin = 'eq_fir_mid.bin';
fn.txt = 'eq_fir_mid.txt';
fn.bin = 'mid.blob';
fn.txt = 'mid.txt';
fn.tplg1 = 'eq_fir_coef_mid.m4';
fn.tplg2 = 'midboost.conf';
comment = 'Mid boost, created with example_fir_eq.m';
Expand All @@ -77,8 +79,8 @@ function example_fir_eq()
%% -------------------
%% Example 3: Flat EQ
%% -------------------
fn.bin = 'eq_fir_flat.bin';
fn.txt = 'eq_fir_flat.txt';
fn.bin = 'flat.blob';
fn.txt = 'flat.txt';
fn.tplg1 = 'eq_fir_coef_flat.m4';
fn.tplg2 = 'flat.conf';
comment = 'Flat response, created with example_fir_eq.m';
Expand All @@ -104,8 +106,8 @@ function example_fir_eq()
%% --------------------------
%% Example 4: Pass-through EQ
%% --------------------------
fn.bin = 'eq_fir_pass.bin';
fn.txt = 'eq_fir_pass.txt';
fn.bin = 'pass.blob';
fn.txt = 'pass.txt';
fn.tplg1 = 'eq_fir_coef_pass.m4';
fn.tplg2 = 'passthrough.conf';
comment = 'Pass-through response, created with example_fir_eq.m';
Expand Down Expand Up @@ -133,6 +135,8 @@ function example_fir_eq()
%% Done.
%% --------------------------

rmpath ../common

end

%% -------------------
Expand Down Expand Up @@ -221,7 +225,7 @@ function eq_pack_export(bm, fn, note)

bp = eq_fir_blob_pack(bm, 3); % IPC3
if ~isempty(fn.bin)
eq_blob_write(fullfile(fn.cpath3, fn.bin), bp);
sof_ucm_blob_write(fullfile(fn.cpath3, fn.bin), bp);
end
if ~isempty(fn.txt)
eq_alsactl_write(fullfile(fn.cpath3, fn.txt), bp);
Expand All @@ -232,7 +236,7 @@ function eq_pack_export(bm, fn, note)

bp = eq_fir_blob_pack(bm, 4); % IPC4
if ~isempty(fn.bin)
eq_blob_write(fullfile(fn.cpath4, fn.bin), bp);
sof_ucm_blob_write(fullfile(fn.cpath4, fn.bin), bp);
end
if ~isempty(fn.txt)
eq_alsactl_write(fullfile(fn.cpath4, fn.txt), bp);
Expand Down
12 changes: 7 additions & 5 deletions tools/tune/eq/example_iir_bandsplit.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ function example_iir_bandsplit()
%% Common definitions
fs = 48e3;
tpath = '../../topology/topology1/m4';
cpath = '../../ctl';
cpath = '../../ctl/ipc3/eq_iir';
priv = 'DEF_EQIIR_PRIV';

addpath ../common

%% --------------------------------------------------
%% Example: Band-split 2ch to 4ch low and high bands
%% --------------------------------------------------
blob_fn = fullfile(cpath, 'eq_iir_bandsplit.bin');
alsa_fn = fullfile(cpath, 'eq_iir_bandsplit.txt');
blob_fn = fullfile(cpath, 'bandsplit.blob');
alsa_fn = fullfile(cpath, 'bandsplit.txt');
tplg_fn = fullfile(tpath, 'eq_iir_bandsplit.m4');
comment = 'Bandsplit, created with example_iir_bandsplit.m';

Expand All @@ -42,11 +44,11 @@ function example_iir_bandsplit()
%% Pack and write file
eq_pack_export(bm, blob_fn, alsa_fn, tplg_fn, priv, comment)


%% ------------------------------------
%% Done.
%% ------------------------------------

rmpath ../common
end

%% -------------------
Expand Down Expand Up @@ -126,7 +128,7 @@ function eq_pack_export(bm, bin_fn, ascii_fn, tplg_fn, priv, note)
bp = eq_iir_blob_pack(bm);

if ~isempty(bin_fn)
eq_blob_write(bin_fn, bp);
sof_ucm_blob_write(bin_fn, bp);
end

if ~isempty(ascii_fn)
Expand Down
Loading