| 131 | } |
| 132 | |
| 133 | bool GranulationNode::setGrainSource(ContextRenderLock & r, std::shared_ptr<AudioBus> buffer) |
| 134 | { |
| 135 | ASSERT(grainSourceBus); |
| 136 | |
| 137 | // Granulation Settings Sanity Check |
| 138 | /// @fixme these values should be per sample, not per quantum |
| 139 | /// -or- they should be settings if they don't vary per sample |
| 140 | std::cout << "GranulationNode::WindowFunction " << s_window_types[windowFunc->valueUint32()] << std::endl; |
| 141 | std::cout << "GranulationNode::numGrains " << numGrains->value() << std::endl; |
| 142 | std::cout << "GranulationNode::grainDuration " << grainDuration->value() << std::endl; |
| 143 | std::cout << "GranulationNode::grainPositionMin " << grainPositionMin->value() << std::endl; |
| 144 | std::cout << "GranulationNode::grainPositionMax " << grainPositionMax->value() << std::endl; |
| 145 | std::cout << "GranulationNode::grainPlaybackFreq " << grainPlaybackFreq->value() << std::endl; |
| 146 | |
| 147 | grainSourceBus->setBus(buffer.get()); |
| 148 | output(0)->setNumberOfChannels(r, buffer ? buffer->numberOfChannels() : 0); |
| 149 | |
| 150 | // Compute useful values |
| 151 | /// @fixme these values should be per sample, not per quantum |
| 152 | /// -or- they should be settings if they don't vary per sample |
| 153 | const float grain_duration_seconds = grainDuration->value(); |
| 154 | const uint64_t grain_duration_samples = static_cast<uint64_t>(grain_duration_seconds * r.context()->sampleRate()); |
| 155 | |
| 156 | // Setup window/envelope |
| 157 | std::vector<float> grain_window(grain_duration_samples, 1.f); |
| 158 | lab::ApplyWindowFunctionInplace(static_cast<WindowFunction>(windowFunc->valueUint32()), grain_window.data(), static_cast<int>(grain_window.size())); |
| 159 | |
| 160 | window_bus.reset(new lab::AudioBus(1, static_cast<int>(grain_duration_samples))); |
| 161 | window_bus->setSampleRate(r.context()->sampleRate()); |
| 162 | |
| 163 | float * windowData = window_bus->channel(0)->mutableData(); |
| 164 | for (uint32_t sample = 0; sample < grain_window.size(); ++sample) |
| 165 | { |
| 166 | windowData[sample] = grain_window[sample]; |
| 167 | } |
| 168 | |
| 169 | // Setup grains |
| 170 | UniformRandomGenerator rnd; |
| 171 | auto sample_to_granulate = grainSourceBus->valueBus(); |
| 172 | |
| 173 | int ng = static_cast<int>(numGrains->value()); |
| 174 | for (int i = 0; i < ng; ++i) |
| 175 | { |
| 176 | /// @fixme these values should be per sample, not per quantum |
| 177 | /// -or- they should be settings if they don't vary per sample |
| 178 | const float random_pos_offset = rnd.random_float(grainPositionMin->value(), grainPositionMax->value()); |
| 179 | |
| 180 | printf("grain offset %f\n", random_pos_offset); |
| 181 | |
| 182 | grain_pool.emplace_back(grain(sample_to_granulate, window_bus, r.context()->sampleRate(), random_pos_offset, grain_duration_seconds, grainPlaybackFreq->value())); |
| 183 | } |
| 184 | |
| 185 | return true; |
| 186 | } |
| 187 | |
| 188 | void GranulationNode::process(ContextRenderLock& r, int bufferSize) |
| 189 | { |
no test coverage detected