All of lore.kernel.org
 help / color / mirror / Atom feed
From: Simon Glass <sjg@chromium.org>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v2 06/22] dm: sandbox: Update sound to use two buffers
Date: Mon, 10 Dec 2018 10:37:35 -0700	[thread overview]
Message-ID: <20181210173751.177266-7-sjg@chromium.org> (raw)
In-Reply-To: <20181210173751.177266-1-sjg@chromium.org>

At present we use a single buffer for sound which means we cannot be
playing one sound while queueing up the next. This wouldn't matter except
that a long sound (more than a second) has to be created as a single
buffer, thus using a lot of memory. To better mimic what real sound
drivers do, add support for double buffering in sandbox.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

Changes in v2: None

 arch/sandbox/cpu/sdl.c | 88 ++++++++++++++++++++++++++++++------------
 1 file changed, 63 insertions(+), 25 deletions(-)

diff --git a/arch/sandbox/cpu/sdl.c b/arch/sandbox/cpu/sdl.c
index c7a8d945492..4dacdbf993f 100644
--- a/arch/sandbox/cpu/sdl.c
+++ b/arch/sandbox/cpu/sdl.c
@@ -13,6 +13,21 @@ enum {
 	SAMPLE_RATE	= 22050,
 };
 
+/**
+ * struct buf_info - a data buffer holding audio data
+ *
+ * @pos:	Current position playing in audio buffer
+ * @size:	Size of data in audio buffer (0=empty)
+ * @alloced:	Allocated size of audio buffer (max size it can hold)
+ * @data:	Audio data
+ */
+struct buf_info {
+	uint pos;
+	uint size;
+	uint alloced;
+	uint8_t *data;
+};
+
 static struct sdl_info {
 	SDL_Surface *screen;
 	int width;
@@ -20,12 +35,11 @@ static struct sdl_info {
 	int depth;
 	int pitch;
 	uint frequency;
-	uint audio_pos;
-	uint audio_size;
 	uint sample_rate;
-	uint8_t *audio_data;
 	bool audio_active;
 	bool inited;
+	int cur_buf;
+	struct buf_info buf[2];
 } sdl;
 
 static void sandbox_sdl_poll_events(void)
@@ -243,24 +257,37 @@ int sandbox_sdl_key_pressed(int keycode)
 
 void sandbox_sdl_fill_audio(void *udata, Uint8 *stream, int len)
 {
+	struct buf_info *buf;
 	int avail;
+	int i;
 
-	avail = sdl.audio_size - sdl.audio_pos;
-	if (avail < len)
-		len = avail;
-
-	SDL_MixAudio(stream, sdl.audio_data + sdl.audio_pos, len,
-		     SDL_MIX_MAXVOLUME);
-	sdl.audio_pos += len;
-
-	/* Loop if we are@the end */
-	if (sdl.audio_pos == sdl.audio_size)
-		sdl.audio_pos = 0;
+	for (i = 0; i < 2; i++) {
+		buf = &sdl.buf[sdl.cur_buf];
+		avail = buf->size - buf->pos;
+		if (avail <= 0) {
+			sdl.cur_buf = 1 - sdl.cur_buf;
+			continue;
+		}
+		if (avail > len)
+			avail = len;
+
+		SDL_MixAudio(stream, buf->data + buf->pos, avail,
+			     SDL_MIX_MAXVOLUME);
+		buf->pos += avail;
+		len -= avail;
+
+		/* Move to next buffer if we are at the end */
+		if (buf->pos == buf->size)
+			buf->size = 0;
+		else
+			break;
+	}
 }
 
 int sandbox_sdl_sound_init(void)
 {
 	SDL_AudioSpec wanted;
+	int i;
 
 	if (sandbox_sdl_ensure_init())
 		return -1;
@@ -276,13 +303,20 @@ int sandbox_sdl_sound_init(void)
 	wanted.callback = sandbox_sdl_fill_audio;
 	wanted.userdata = NULL;
 
-	sdl.audio_size = sizeof(uint16_t) * wanted.freq;
-	sdl.audio_data = malloc(sdl.audio_size);
-	if (!sdl.audio_data) {
-		printf("%s: Out of memory\n", __func__);
-		return -1;
+	for (i = 0; i < 2; i++) {
+		struct buf_info *buf = &sdl.buf[i];
+
+		buf->alloced = sizeof(uint16_t) * wanted.freq * wanted.channels;
+		buf->data = malloc(buf->alloced);
+		if (!buf->data) {
+			printf("%s: Out of memory\n", __func__);
+			if (i == 1)
+				free(sdl.buf[0].data);
+			return -1;
+		}
+		buf->pos = 0;
+		buf->size = 0;
 	}
-	sdl.audio_pos = 0;
 
 	if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
 		printf("Unable to initialize SDL audio: %s\n", SDL_GetError());
@@ -296,23 +330,27 @@ int sandbox_sdl_sound_init(void)
 	}
 	sdl.audio_active = true;
 	sdl.sample_rate = wanted.freq;
+	sdl.cur_buf = 0;
 
 	return 0;
 
 err:
-	free(sdl.audio_data);
+	for (i = 0; i < 2; i++)
+		free(sdl.buf[i].data);
 	return -1;
 }
 
 int sandbox_sdl_sound_start(uint frequency)
 {
+	struct buf_info *buf = &sdl.buf[0];
+
 	if (!sdl.audio_active)
 		return -1;
 	sdl.frequency = frequency;
-	sound_create_square_wave(sdl.sample_rate,
-				 (unsigned short *)sdl.audio_data,
-				 sdl.audio_size, frequency);
-	sdl.audio_pos = 0;
+	sound_create_square_wave(sdl.sample_rate, (unsigned short *)buf->data,
+				 buf->alloced, frequency);
+	buf->pos = 0;
+	buf->size = buf->alloced;
 	SDL_PauseAudio(0);
 
 	return 0;
-- 
2.20.0.rc2.403.gdbc3b29805-goog

  parent reply	other threads:[~2018-12-10 17:37 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-10 17:37 [U-Boot] [PATCH v2 00/22] dm: sound: Convert to driver model Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 01/22] dm: sound: exynos: Correct codec bus addresses Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 02/22] dm: sound: Create an option to use driver model for sound Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 03/22] dm: sound: Rename samsung_i2s_priv to i2s_uc_priv Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 04/22] dm: sound: Create a uclass for audio codecs Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 05/22] dm: sound: Create a uclass for i2s Simon Glass
2018-12-10 17:37 ` Simon Glass [this message]
2018-12-10 17:37 ` [U-Boot] [PATCH v2 07/22] dm: sound: Create a uclass for sound Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 08/22] dm: core: Add a function to read into a unsigned int Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 09/22] dm: sound: Start i2c IDs from 0 Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 10/22] dm: sound: Add conversion to driver model Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 11/22] exynos: Add proid_is_exynos542x() for common 542x Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 12/22] exynos: Add support for exynos5420 i2s pinmux Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 13/22] dm: sound: Move common code out of maxim98095 Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 14/22] dm: sound: exynos: Add support for max98090 Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 15/22] dm: exynos: sound: Convert to use driver model Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 16/22] dm: sandbox: " Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 17/22] dm: exynos: Drop CONFIG_DM_I2C_COMPAT Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 18/22] dm: sound: Complete migration to driver model Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 19/22] dm: sound: Fix license headers Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 20/22] dm: sound: max98095: Tidy up error codes Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 21/22] dm: sandbox: Allow selection of sample rate and channels Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 22/22] dm: sound: Use the correct number of channels for sound Simon Glass
2018-12-14 15:35 ` sjg at google.com
2018-12-14 15:35 ` [U-Boot] [PATCH v2 21/22] dm: sandbox: Allow selection of sample rate and channels sjg at google.com
2018-12-14 15:35 ` [U-Boot] [PATCH v2 20/22] dm: sound: max98095: Tidy up error codes sjg at google.com
2018-12-14 15:35 ` [U-Boot] [PATCH v2 19/22] dm: sound: Fix license headers sjg at google.com
2018-12-14 15:35 ` [U-Boot] [PATCH v2 18/22] dm: sound: Complete migration to driver model sjg at google.com
2018-12-14 15:35 ` [U-Boot] [PATCH v2 17/22] dm: exynos: Drop CONFIG_DM_I2C_COMPAT sjg at google.com
2018-12-14 15:35 ` [U-Boot] [PATCH v2 16/22] dm: sandbox: sound: Convert to use driver model sjg at google.com
2018-12-14 15:35 ` [U-Boot] [PATCH v2 15/22] dm: exynos: " sjg at google.com
2018-12-14 15:35 ` [U-Boot] [PATCH v2 14/22] dm: sound: exynos: Add support for max98090 sjg at google.com
2018-12-14 15:35 ` [U-Boot] [PATCH v2 13/22] dm: sound: Move common code out of maxim98095 sjg at google.com
2018-12-14 15:35 ` [U-Boot] [PATCH v2 12/22] exynos: Add support for exynos5420 i2s pinmux sjg at google.com
2018-12-14 15:35 ` [U-Boot] [PATCH v2 11/22] exynos: Add proid_is_exynos542x() for common 542x sjg at google.com
2018-12-14 15:35 ` [U-Boot] [PATCH v2 10/22] dm: sound: Add conversion to driver model sjg at google.com
2018-12-14 15:35 ` [U-Boot] [PATCH v2 09/22] dm: sound: Start i2c IDs from 0 sjg at google.com
2018-12-14 15:35 ` [U-Boot] [PATCH v2 08/22] dm: core: Add a function to read into a unsigned int sjg at google.com
2018-12-14 15:38 ` [U-Boot] [PATCH v2 07/22] dm: sound: Create a uclass for sound sjg at google.com
2018-12-14 15:38 ` [U-Boot] [PATCH v2 06/22] dm: sandbox: Update sound to use two buffers sjg at google.com
2018-12-14 15:38 ` [U-Boot] [PATCH v2 05/22] dm: sound: Create a uclass for i2s sjg at google.com
2018-12-14 15:38 ` [U-Boot] [PATCH v2 04/22] dm: sound: Create a uclass for audio codecs sjg at google.com
2018-12-14 15:38 ` [U-Boot] [PATCH v2 03/22] dm: sound: Rename samsung_i2s_priv to i2s_uc_priv sjg at google.com
2018-12-14 15:38 ` [U-Boot] [PATCH v2 02/22] dm: sound: Create an option to use driver model for sound sjg at google.com
2018-12-14 15:38 ` [U-Boot] [PATCH v2 01/22] dm: sound: exynos: Correct codec bus addresses sjg at google.com

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181210173751.177266-7-sjg@chromium.org \
    --to=sjg@chromium.org \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.