All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] Sample generation on big endian platforms was broken.
@ 2009-07-03 20:31 Kenneth Johansson
  2009-07-03 20:53 ` Takashi Iwai
  0 siblings, 1 reply; 2+ messages in thread
From: Kenneth Johansson @ 2009-07-03 20:31 UTC (permalink / raw)
  To: patch; +Cc: Takashi Iwai, alsa-devel

Has not worked since commit 3d1fa924906996463ac33cba5b5143f762d913cf

Signed-off-by: Kenneth Johansson <kenneth@southpole.se>
---

Fixes regression for 3 byte format in version 1.
tested on both little and big endian platform

 test/pcm.c |   24 +++++++++++++-----------
 1 files changed, 13 insertions(+), 11 deletions(-)

diff --git a/test/pcm.c b/test/pcm.c
index ee27422..0476c5f 100644
--- a/test/pcm.c
+++ b/test/pcm.c
@@ -34,14 +34,10 @@ static void generate_sine(const snd_pcm_channel_area_t *areas,
 	static double max_phase = 2. * M_PI;
 	double phase = *_phase;
 	double step = max_phase*freq/(double)rate;
-	double res;
-	unsigned char *samples[channels], *tmp;
+	unsigned char *samples[channels];
 	int steps[channels];
-	unsigned int chn, byte;
-	union {
-		int i;
-		unsigned char c[4];
-	} ires;
+	unsigned int chn;
+
 	unsigned int maxval = (1 << (snd_pcm_format_width(format) - 1)) - 1;
 	int bps = snd_pcm_format_width(format) / 8;  /* bytes per sample */
 	
@@ -61,12 +57,18 @@ static void generate_sine(const snd_pcm_channel_area_t *areas,
 	}
 	/* fill the channel areas */
 	while (count-- > 0) {
+		int res,i;
 		res = sin(phase) * maxval;
-		ires.i = res;
-		tmp = ires.c;
 		for (chn = 0; chn < channels; chn++) {
-			for (byte = 0; byte < (unsigned int)bps; byte++)
-				*(samples[chn] + byte) = tmp[byte];
+			/* Generate data in native endian format */
+			for ( i=0 ; i < bps ; i++ ){
+#if (__BYTE_ORDER == __BIG_ENDIAN)	
+				*(samples[chn] + bps-1-i) = (res >> i*8) & 0xff;
+#else
+
+				*(samples[chn] + i) = (res >> i*8) & 0xff;
+#endif			
+			}
 			samples[chn] += steps[chn];
 		}
 		phase += step;
-- 
1.6.1.GIT

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] Sample generation on big endian platforms was broken.
  2009-07-03 20:31 [PATCH v2] Sample generation on big endian platforms was broken Kenneth Johansson
@ 2009-07-03 20:53 ` Takashi Iwai
  0 siblings, 0 replies; 2+ messages in thread
From: Takashi Iwai @ 2009-07-03 20:53 UTC (permalink / raw)
  To: Kenneth Johansson; +Cc: alsa-devel

At Fri, 03 Jul 2009 22:31:38 +0200,
Kenneth Johansson wrote:
> 
> Has not worked since commit 3d1fa924906996463ac33cba5b5143f762d913cf
> 
> Signed-off-by: Kenneth Johansson <kenneth@southpole.se>
> ---
> 
> Fixes regression for 3 byte format in version 1.
> tested on both little and big endian platform

Thanks.  I applied this one with a minor modification of coding style.


Takashi  (who still prefers memcpy() ;)

> 
>  test/pcm.c |   24 +++++++++++++-----------
>  1 files changed, 13 insertions(+), 11 deletions(-)
> 
> diff --git a/test/pcm.c b/test/pcm.c
> index ee27422..0476c5f 100644
> --- a/test/pcm.c
> +++ b/test/pcm.c
> @@ -34,14 +34,10 @@ static void generate_sine(const snd_pcm_channel_area_t *areas,
>  	static double max_phase = 2. * M_PI;
>  	double phase = *_phase;
>  	double step = max_phase*freq/(double)rate;
> -	double res;
> -	unsigned char *samples[channels], *tmp;
> +	unsigned char *samples[channels];
>  	int steps[channels];
> -	unsigned int chn, byte;
> -	union {
> -		int i;
> -		unsigned char c[4];
> -	} ires;
> +	unsigned int chn;
> +
>  	unsigned int maxval = (1 << (snd_pcm_format_width(format) - 1)) - 1;
>  	int bps = snd_pcm_format_width(format) / 8;  /* bytes per sample */
>  	
> @@ -61,12 +57,18 @@ static void generate_sine(const snd_pcm_channel_area_t *areas,
>  	}
>  	/* fill the channel areas */
>  	while (count-- > 0) {
> +		int res,i;
>  		res = sin(phase) * maxval;
> -		ires.i = res;
> -		tmp = ires.c;
>  		for (chn = 0; chn < channels; chn++) {
> -			for (byte = 0; byte < (unsigned int)bps; byte++)
> -				*(samples[chn] + byte) = tmp[byte];
> +			/* Generate data in native endian format */
> +			for ( i=0 ; i < bps ; i++ ){
> +#if (__BYTE_ORDER == __BIG_ENDIAN)	
> +				*(samples[chn] + bps-1-i) = (res >> i*8) & 0xff;
> +#else
> +
> +				*(samples[chn] + i) = (res >> i*8) & 0xff;
> +#endif			
> +			}
>  			samples[chn] += steps[chn];
>  		}
>  		phase += step;
> -- 
> 1.6.1.GIT
> 
> 
> 

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2009-07-03 20:53 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-07-03 20:31 [PATCH v2] Sample generation on big endian platforms was broken Kenneth Johansson
2009-07-03 20:53 ` Takashi Iwai

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.