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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// SPDX-License-Identifier: AGPL-3.0-or-later
//! `PitchShift` - Pitch shifting without tempo change
//!
//! Changes pitch by resampling in frequency domain.
//! Combines time stretching with resampling.
//!
//! Deep Debt Principles:
//! - Self-knowledge: Operation knows its computation
//! - Zero hardcoding: Hardware-agnostic implementation
//! - Modern idiomatic Rust: Safe, zero unsafe code
//! - Complete implementation: Production-ready, no mocks
//! - Hardware-agnostic: Pure WGSL for universal compute
use crate::device::compute_pipeline::ComputeDispatch;
use crate::error::{BarracudaError, Result};
use crate::tensor::Tensor;
const SHADER_F64: &str = include_str!("../shaders/audio/pitch_shift_f64.wgsl");
static SHADER_F32: std::sync::LazyLock<String> =
std::sync::LazyLock::new(|| SHADER_F64.to_string());
/// `PitchShift` operation
pub struct PitchShift {
signal: Tensor,
n_steps: f32, // Semitones to shift (positive = up, negative = down)
bins_per_octave: f32,
}
impl PitchShift {
/// Create a new pitch shift operation
///
/// # Errors
///
/// Returns [`Err`] if buffer allocation, GPU dispatch, or buffer
/// readback fails (e.g. device lost or out of memory).
pub fn new(signal: Tensor, n_steps: f32, bins_per_octave: f32) -> Result<Self> {
if bins_per_octave <= 0.0 {
return Err(BarracudaError::InvalidInput {
message: "bins_per_octave must be positive".to_string(),
});
}
Ok(Self {
signal,
n_steps,
bins_per_octave,
})
}
/// Get the WGSL shader source
fn wgsl_shader() -> &'static str {
&SHADER_F32
}
/// Execute the pitch shift operation
///
/// # Errors
///
/// Returns [`Err`] if buffer allocation, GPU dispatch, or buffer
/// readback fails (e.g. device lost or out of memory).
pub fn execute(self) -> Result<Tensor> {
let device = self.signal.device();
let input_length: usize = self.signal.shape().iter().product();
// Compute shift ratio: 2^(n_steps / bins_per_octave)
let rate = (self.n_steps / self.bins_per_octave).exp2();
let output_length = (input_length as f32 / rate) as usize;
// Access input buffer directly (zero-copy)
let input_buffer = self.signal.buffer();
// Create output buffer
let output_buffer = device.create_buffer_f32(output_length)?;
// Create uniform buffer for parameters
#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct Params {
input_length: u32,
output_length: u32,
rate: f32,
}
let params = Params {
input_length: input_length as u32,
output_length: output_length as u32,
rate,
};
let params_buffer = device
.device
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("PitchShift Params"),
contents: bytemuck::cast_slice(&[params]),
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
});
ComputeDispatch::new(device, "PitchShift")
.shader(Self::wgsl_shader(), "main")
.storage_read(0, input_buffer)
.storage_rw(1, &output_buffer)
.uniform(2, ¶ms_buffer)
.dispatch_1d(output_length as u32)
.submit()?;
// Output shape: [output_length]
let output_shape = vec![output_length];
// Return tensor without reading back (zero-copy)
Ok(Tensor::from_buffer(
output_buffer,
output_shape,
device.clone(),
))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_pitch_shift_basic() {
let device = crate::device::test_pool::get_test_device().await;
let signal = Tensor::from_vec_on(vec![0.5; 10_000], vec![10_000], device.clone())
.await
.unwrap();
let shifted = PitchShift::new(signal, 2.0, 12.0)
.unwrap()
.execute()
.unwrap();
assert!(shifted.shape()[0] > 0);
}
}