Skip to content

Commit 594853d

Browse files
singalsulgirdwood
authored andcommitted
Tools: Testbench: Use in prepare() frame_fmt from stream
The file component new() sets function to s32. It's better to initialize to default function that just errors if called. The stream pointer is retrieved from buffer. There is no need to apply twice list_first_item() function. The sample width is set with get_sample_bytes(). The switch case for frame_fmt is changed to use stream. It avoids an error to use 16 bit file write to consume data from 32 bit buffer. This happens because ipc value is from topology while stream format is from command line override. Only the text file output becomes incorrect. The comp_data struct contained unnecessary fields such as period_bytes, frame_bytes, and frame_fmt. The patch also contains some switch-case code cleanup for more compact code and look. Signed-off-by: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
1 parent b502424 commit 594853d

2 files changed

Lines changed: 28 additions & 41 deletions

File tree

tools/testbench/file.c

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,13 @@ static int write_samples_s16(struct file_comp_data *cd, struct audio_stream *sou
410410
return samples_written;
411411
}
412412

413+
/* Default file copy function, just return error if called */
414+
static int file_default(struct comp_dev *dev, struct audio_stream *sink,
415+
struct audio_stream *source, uint32_t frames)
416+
{
417+
return -EINVAL;
418+
}
419+
413420
/* function for processing 32-bit samples */
414421
static int file_s32(struct comp_dev *dev, struct audio_stream *sink,
415422
struct audio_stream *source, uint32_t frames)
@@ -554,7 +561,7 @@ static struct comp_dev *file_new(const struct comp_driver *drv,
554561
comp_set_drvdata(dd->dai, cd);
555562

556563
/* default function for processing samples */
557-
cd->file_func = file_s32;
564+
cd->file_func = file_default;
558565

559566
/* get filename from IPC and open file */
560567
cd->fs.fn = strdup(ipc_file->fn);
@@ -783,38 +790,19 @@ static int file_prepare(struct comp_dev *dev)
783790
if (ret == COMP_STATUS_STATE_ALREADY_SET)
784791
return PPL_STATUS_PATH_STOP;
785792

786-
/* file component source or sink buffer */
787-
if (cd->fs.mode == FILE_WRITE) {
788-
stream = &list_first_item(&dev->bsource_list,
789-
struct comp_buffer, sink_list)->stream;
790-
} else {
791-
stream = &list_first_item(&dev->bsink_list, struct comp_buffer,
792-
source_list)->stream;
793-
}
794-
795-
if (stream->frame_fmt == SOF_IPC_FRAME_S16_LE)
796-
cd->sample_container_bytes = 2;
797-
else
798-
cd->sample_container_bytes = 4;
799-
800-
/* calculate period size based on config */
801-
cd->period_bytes = dev->frames * cd->sample_container_bytes *
802-
stream->channels;
803-
804793
/* file component sink/source buffer period count */
805794
switch (cd->fs.mode) {
806795
case FILE_READ:
807-
buffer = list_first_item(&dev->bsink_list, struct comp_buffer,
808-
source_list);
796+
buffer = list_first_item(&dev->bsink_list, struct comp_buffer, source_list);
809797
periods = dev->ipc_config.periods_sink;
810798
break;
811799
case FILE_WRITE:
812-
buffer = list_first_item(&dev->bsource_list,
813-
struct comp_buffer, sink_list);
800+
buffer = list_first_item(&dev->bsource_list, struct comp_buffer, sink_list);
814801
periods = dev->ipc_config.periods_source;
815802
break;
816803
default:
817804
/* TODO: duplex mode */
805+
fprintf(stderr, "Error: Unknown file mode %d\n", cd->fs.mode);
818806
break;
819807
}
820808

@@ -824,46 +812,47 @@ static int file_prepare(struct comp_dev *dev)
824812
}
825813

826814
/* set downstream buffer size */
827-
switch (dev->ipc_config.frame_fmt) {
828-
case(SOF_IPC_FRAME_S16_LE):
829-
ret = buffer_set_size(buffer, dev->frames * 2 *
830-
periods * buffer->stream.channels);
815+
stream = &buffer->stream;
816+
switch (stream->frame_fmt) {
817+
case SOF_IPC_FRAME_S16_LE:
818+
ret = buffer_set_size(buffer, 2 * dev->frames * periods * stream->channels);
831819
if (ret < 0) {
832820
fprintf(stderr, "error: file buffer size set\n");
833821
return ret;
834822
}
835-
buffer_reset_pos(buffer, NULL);
836823

837824
/* set file function */
838825
cd->file_func = file_s16;
839826
break;
840-
case(SOF_IPC_FRAME_S24_4LE):
841-
ret = buffer_set_size(buffer, dev->frames * 4 *
842-
periods * buffer->stream.channels);
827+
case SOF_IPC_FRAME_S24_4LE:
828+
ret = buffer_set_size(buffer, 4 * dev->frames * periods * stream->channels);
843829
if (ret < 0) {
844830
fprintf(stderr, "error: file buffer size set\n");
845831
return ret;
846832
}
847-
buffer_reset_pos(buffer, NULL);
848833

849834
/* set file function */
850835
cd->file_func = file_s24;
851836
break;
852-
case(SOF_IPC_FRAME_S32_LE):
853-
ret = buffer_set_size(buffer, dev->frames * 4 *
854-
periods * buffer->stream.channels);
837+
case SOF_IPC_FRAME_S32_LE:
838+
ret = buffer_set_size(buffer, 4 * dev->frames * periods * stream->channels);
855839
if (ret < 0) {
856840
fprintf(stderr, "error: file buffer size set\n");
857841
return ret;
858842
}
859-
buffer_reset_pos(buffer, NULL);
843+
844+
/* set file function */
845+
cd->file_func = file_s32;
860846
break;
861847
default:
848+
fprintf(stderr, "Warning: Unknown file sample format %d\n",
849+
dev->ipc_config.frame_fmt);
862850
return -EINVAL;
863851
}
864852

853+
cd->sample_container_bytes = get_sample_bytes(stream->frame_fmt);
854+
buffer_reset_pos(buffer, NULL);
865855
dev->state = COMP_STATE_PREPARE;
866-
867856
return ret;
868857
}
869858

tools/testbench/include/testbench/file.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,11 @@ struct file_state {
4040

4141
/* file comp data */
4242
struct file_comp_data {
43-
uint32_t period_bytes;
43+
struct file_state fs;
44+
enum sof_ipc_frame frame_fmt;
4445
uint32_t channels;
45-
uint32_t frame_bytes;
4646
uint32_t rate;
47-
struct file_state fs;
4847
int sample_container_bytes;
49-
enum sof_ipc_frame frame_fmt;
5048
int (*file_func)(struct comp_dev *dev, struct audio_stream *sink,
5149
struct audio_stream *source, uint32_t frames);
5250

0 commit comments

Comments
 (0)