| 114 | } |
| 115 | |
| 116 | PackedFloat32Array SoundSmith::process(const PackedFloat32Array &input) { |
| 117 | PackedFloat32Array output; |
| 118 | |
| 119 | if (channels < 1) { |
| 120 | return output; |
| 121 | } |
| 122 | |
| 123 | const int total_samples = input.size(); |
| 124 | |
| 125 | if (total_samples <= 0) { |
| 126 | return output; |
| 127 | } |
| 128 | |
| 129 | if (total_samples % channels != 0) { |
| 130 | ERR_FAIL_V_MSG(output, "Input array size must be a multiple of channel count."); |
| 131 | } |
| 132 | |
| 133 | const int input_frames = total_samples / channels; |
| 134 | |
| 135 | if (input_frames <= 0) { |
| 136 | return output; |
| 137 | } |
| 138 | |
| 139 | const float tf = (tempo > 0.0f) ? tempo : 1.0f; |
| 140 | int output_frames = (int)std::lround((double)input_frames / (double)tf); |
| 141 | |
| 142 | if (output_frames < 0) { |
| 143 | output_frames = 0; |
| 144 | } |
| 145 | |
| 146 | // Deinterleave |
| 147 | std::vector<std::vector<float>> in_ch; |
| 148 | in_ch.resize((size_t)channels); |
| 149 | |
| 150 | for (int c = 0; c < channels; c++) { |
| 151 | in_ch[(size_t)c].resize((size_t)input_frames); |
| 152 | } |
| 153 | |
| 154 | const float *src = input.ptr(); |
| 155 | |
| 156 | for (int i = 0; i < input_frames; i++) { |
| 157 | const int base = i * channels; |
| 158 | |
| 159 | for (int c = 0; c < channels; c++) { |
| 160 | in_ch[(size_t)c][(size_t)i] = src[base + c]; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | // Output buffers |
| 165 | std::vector<std::vector<float>> out_ch; |
| 166 | out_ch.resize((size_t)channels); |
| 167 | |
| 168 | for (int c = 0; c < channels; c++) { |
| 169 | out_ch[(size_t)c].assign((size_t)output_frames, 0.0f); |
| 170 | } |
| 171 | |
| 172 | std::vector<float *> in_ptrs((size_t)channels, nullptr); |
| 173 | std::vector<float *> out_ptrs((size_t)channels, nullptr); |