File src/remiaudio/dsp/stereoenhancer.cr from the latest check-in


     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
   100
   101
   102
   103
   104
   105
   106
   107
#### RemiAudio
#### Copyright (C) 2022-2024 Remilia Scarlet <remilia@posteo.jp>
####
#### This program is free software: you can redistribute it and/or modify it
#### under the terms of the GNU Affero General Public License as published by
#### the Free Software Foundation, either version 3 of the License, or (at your
#### option) any later version.
####
#### This program is distributed in the hope that it will be useful, but WITHOUT
#### ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
#### FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
#### License for more details.
####
#### You should have received a copy of the GNU Affero General Public License
#### along with this program.  If not, see <https://www.gnu.org/licenses/>.

module RemiAudio::DSP::StereoEnhancer
  # Applies a stereo enhancement effect to the audio blocks.  The coeff
  # parameter defines how much widening (or narrowing) to apply.  0.5 is
  # neutral, 0.0 mono-fies it, and values above 0.5 widen the sound.  The
  # expected range is 0.0 to 1.5.
  @[AlwaysInline]
  def self.process(blockLeft : Array(Float64)|Slice(Float64), blockRight : Array(Float64)|Slice(Float64),
                   coeff : Float64) : Nil
    sampleL : Float64 = 0.0
    sampleR : Float64 = 0.0
    adjust : Float64 = 0.0
    stereo : Float64 = 0.0

    blockLeft.size.times do |i|
      sampleL = blockLeft[i]
      sampleR = blockRight[i]
      adjust = (sampleL + sampleR) * 0.5
      stereo = (sampleL - sampleR) * coeff
      blockLeft[i] = adjust + stereo
      blockRight[i] = adjust - stereo
    end
  end

  # Applies a stereo enhancement effect to the audio blocks.  The coeff
  # parameter defines how much widening (or narrowing) to apply.  0.5 is
  # neutral, 0.0 mono-fies it, and values above 0.5 widen the sound.  The
  # expected range is 0.0 to 1.5.
  @[AlwaysInline]
  def self.process(blockLeft : Array(Float32)|Slice(Float32), blockRight : Array(Float32)|Slice(Float32),
                   coeff : Float64) : Nil
    sampleL : Float32 = 0.0
    sampleR : Float32 = 0.0
    adjust : Float64 = 0.0
    stereo : Float64 = 0.0

    blockLeft.size.times do |i|
      sampleL = blockLeft[i]
      sampleR = blockRight[i]
      adjust = (sampleL + sampleR) * 0.5
      stereo = (sampleL - sampleR) * coeff
      blockLeft[i] = (adjust + stereo).to_f32!
      blockRight[i] = (adjust - stereo).to_f32!
    end
  end

  # Applies a stereo enhancement effect to the interleaved audio
  # block.  The coeff parameter defines how much widening (or
  # narrowing) to apply.  0.5 is neutral, 0.0 mono-fies it, and values
  # above 0.5 widen the sound.  The expected range is 0.0 to 1.5.
  @[AlwaysInline]
  def self.process(block : Array(Float64)|Slice(Float64), coeff : Float64) : Nil
    sampleL : Float64 = 0.0
    sampleR : Float64 = 0.0
    adjust : Float64 = 0.0
    stereo : Float64 = 0.0
    pos : Int32 = 0

    while pos < block.size
      sampleL = block[pos]
      sampleR = block[pos + 1]
      adjust = (sampleL + sampleR) * 0.5
      stereo = (sampleL - sampleR) * coeff
      block[pos] = adjust + stereo
      block[pos + 1] = adjust - stereo
      pos += 2
    end
  end

  # Applies a stereo enhancement effect to the interleaved audio
  # block.  The coeff parameter defines how much widening (or
  # narrowing) to apply.  0.5 is neutral, 0.0 mono-fies it, and values
  # above 0.5 widen the sound.  The expected range is 0.0 to 1.5.
  @[AlwaysInline]
  def self.process(block : Array(Float32)|Slice(Float32), coeff : Float64) : Nil
    sampleL : Float64 = 0.0
    sampleR : Float64 = 0.0
    adjust : Float64 = 0.0
    stereo : Float64 = 0.0
    pos : Int32 = 0

    while pos < block.size
      sampleL = block[pos].to_f64!
      sampleR = block[pos + 1].to_f64!
      adjust = (sampleL + sampleR) * 0.5
      stereo = (sampleL - sampleR) * coeff
      block[pos] = (adjust + stereo).to_f32!
      block[pos + 1] = (adjust - stereo).to_f32!
      pos += 2
    end
  end
end