From 3aed6ef8421be53abe56e06d2d849e7a91d6a153 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Harald=20J=C3=B6rg?= Date: Sat, 7 Dec 2024 17:09:00 +0100 Subject: [PATCH] Bugfix: The YAML section had a mix of tabs and spaces for indentation. --- .../blog/2024/12/13/sound/index.markdown | 43 +++++++++---------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/statocles-site/blog/2024/12/13/sound/index.markdown b/statocles-site/blog/2024/12/13/sound/index.markdown index 05c792a..5d13e3f 100644 --- a/statocles-site/blog/2024/12/13/sound/index.markdown +++ b/statocles-site/blog/2024/12/13/sound/index.markdown @@ -10,11 +10,8 @@ images: alt: 'Score sheet for "In dulci jubilo"' data: attribution: |- - From ChoralWiki, available under the - Choral Public Domain License. - Copyright © 2016 by CPDL. + Banner image From ChoralWiki, available under the Choral Public Domain License. Copyright © 2016 by CPDL. data: - bio: haj description: 'Creating Sound with PDL (Perl Data Language)' --- @@ -45,11 +42,11 @@ the result in a one-dimensional PDL ndarray. Like audio CDs, we use # This subroutine is where the fun will happen. # Return N_SAMPLES for a wave with FREQUENCY - sub samples ($n_samples, $frequency) { - my $phase = sequence($n_samples) * 2 * PI * $frequency / RATE; - my $samples = sin($phase); - return $samples; - } + sub samples ($n_samples, $frequency) { + my $phase = sequence($n_samples) * 2 * PI * $frequency / RATE; + my $samples = sin($phase); + return $samples; + } The `sequence` constructor of PDL builds regular intervals which we adjust for the rate and the desired frequency. Also, PDL brings its @@ -64,12 +61,12 @@ constructor, and we make sure that they use the full value range for Perl scalar which holds the _binary_ array which we can directly print to a WAV file: - sub prepare ($samples) { - my $amplitude = 2**15-1; - my $max = max(abs $samples); - my $sound16 = short($samples / $max * $amplitude); - return $sound16->get_dataref; - } + sub prepare ($samples) { + my $amplitude = 2**15-1; + my $max = max(abs $samples); + my $sound16 = short($samples / $max * $amplitude); + return $sound16->get_dataref; + } The actual WAV formatting needs some bookkeeping but isn't all that interesting: @@ -183,8 +180,8 @@ samples: $samples -= short $samples; # drop the integer part $samples *= 2; $samples -= 1; - return $samples; - } + return $samples; + }