sound: Add sample rate as a parameter for square wave
authorSimon Glass <sjg@chromium.org>
Fri, 16 Nov 2018 02:56:13 +0000 (19:56 -0700)
committerSimon Glass <sjg@chromium.org>
Thu, 29 Nov 2018 16:30:05 +0000 (09:30 -0700)
At present this value is hard-coded in the function that generates a
square wave. Since sample rates vary between different hardware, it makes
more sense to have this as a parameter.

Update the function and its users.

Signed-off-by: Simon Glass <sjg@chromium.org>
arch/sandbox/cpu/sdl.c
drivers/sound/sound-i2s.c
drivers/sound/sound.c
include/sound.h

index adcb73826fef64a4ae9979f6920f7bb317457039..36f1bf0c83bfe9e7b0692a0b26267ee1074962b5 100644 (file)
@@ -322,7 +322,7 @@ int sandbox_sdl_sound_start(uint frequency)
        if (!sdl.audio_active)
                return -1;
        sdl.frequency = frequency;
-       sound_create_square_wave((unsigned short *)sdl.audio_data,
+       sound_create_square_wave(22050, (unsigned short *)sdl.audio_data,
                                 sdl.audio_size, frequency);
        sdl.audio_pos = 0;
        SDL_PauseAudio(0);
index 9f09e3bf930834881fbcaae0460e79dcacca9257..f0f0b79bc52390e245fc65166c7611badcc073a0 100644 (file)
@@ -185,7 +185,8 @@ int sound_play(uint32_t msec, uint32_t frequency)
                return -1;
        }
 
-       sound_create_square_wave((unsigned short *)data,
+       sound_create_square_wave(g_i2stx_pri.samplingrate,
+                                (unsigned short *)data,
                                 data_size / sizeof(unsigned short),
                                 frequency);
 
index 6c1eb4c19cc3fad00f727241544213ceaec99507..4f0ad0d8f0d706f4bd0881e878341f37a388f414 100644 (file)
@@ -7,11 +7,11 @@
 #include <common.h>
 #include <sound.h>
 
-void sound_create_square_wave(unsigned short *data, int size, uint32_t freq)
+void sound_create_square_wave(uint sample_rate, unsigned short *data, int size,
+                             uint freq)
 {
-       const int sample = 48000;
        const unsigned short amplitude = 16000; /* between 1 and 32767 */
-       const int period = freq ? sample / freq : 0;
+       const int period = freq ? sample_rate / freq : 0;
        const int half = period / 2;
 
        assert(freq);
index 3269f2371c3b1bce18ea3762177f8cf2b7ce1df3..77bfe6a93b2ae89783ac6e416d2f9b34637391ca 100644 (file)
@@ -31,11 +31,13 @@ struct sound_codec_info {
 /*
  * Generates square wave sound data for 1 second
  *
+ * @param sample_rate   Sample rate in Hz
  * @param data          data buffer pointer
  * @param size          size of the buffer
  * @param freq          frequency of the wave
  */
-void sound_create_square_wave(unsigned short *data, int size, uint32_t freq);
+void sound_create_square_wave(uint sample_rate, unsigned short *data, int size,
+                             uint freq);
 
 /*
  * Initialises audio sub system