class in Clatter.Core
Inherits from AudioEvent
Generates impact audio.
An Impact is actually a series of events. By reusing the same Impact object, data from previous impacts can affect the current impact. This is useful for situations such as an object repeatedly bouncing on a table.
Impact events are automatically generated from collision data within AudioGenerator
. You can also manually create an Impact and use it to generate audio without needing to use an AudioGenerator
. This can be useful if you want to generate audio without needing to create a physics simulation.
Name | Type | Description | Default Value |
---|---|---|---|
preventDistortion | bool | If true, clamp the audio amplitude values to less than or equal to 0.99, preventing distortion. | true |
clampContactTime | bool | If true, clamp the contact time to a plausible value. Set this to false if you want to generate impacts with unusually long contact times. | true |
minTimeBetweenImpacts | double | The minimum time in seconds between impacts. If an impact occurs an this much time hasn't yet elapsed, the impact will be ignored. This can prevent strange "droning" sounds caused by too many impacts in rapid succession. | 0.05 |
maxTimeBetweenImpacts | double | The maximum time in seconds between impacts. After this many seconds, this impact series will end and a subsequent impact collision will start a new Impact. | 3 |
simulationAmp | double | The overall amplitude of the simulation. The amplitude of generated audio is scaled by this factor. Must be between 0 and 0.99 Inherited from AudioEvent .
|
0.9 |
Name | Type | Description | Default Value |
---|---|---|---|
samples | Samples |
The audio samples generated from this event. Readonly. Inherited from AudioEvent .
|
|
state | EventState |
The current state of the AudioEvent. This is not the same thing as whether any audio is playing. An Impact ends when too much time has elapsed since the most recent impact collision. A Scrape ends when the object is moving too slowly. Inherited from AudioEvent .
|
EventState.start |
public Impact(ClatterObjectData primary, ClatterObjectData secondary, Random rng)
Name | Type | Description |
---|---|---|
primary | ClatterObjectData |
The primary object. |
secondary | ClatterObjectData |
The secondary object. |
rng | Random | The random number generator. |
public override bool GetAudio(double speed)
Generate audio. Returns true if audio was generated. This will set the samples field.
Inherited from AudioEvent
.
Name | Type | Description |
---|---|---|
speed | double | The collision speed in meters per second. |
using Clatter.Core;
public class ImpactAudioExample
{
private static void Main(string[] args)
{
// Load the materials.
ImpactMaterial primaryMaterial = ImpactMaterial.glass_1;
ImpactMaterial secondaryMaterial = ImpactMaterial.stone_4;
ImpactMaterialData.Load(primaryMaterial);
ImpactMaterialData.Load(secondaryMaterial);
// Set the objects.
ClatterObjectData primary = new ClatterObjectData(0, primaryMaterial, 0.2, 0.2, 1);
ClatterObjectData secondary = new ClatterObjectData(1, secondaryMaterial, 0.5, 0.1, 100);
// Create the impact event.
Impact impact = new Impact(primary, secondary, new Random());
// Generate audio.
impact.GetAudio(1);
// Write the audio as a .wav file.
WavWriter writer = new WavWriter("out.wav", overwrite: true);
writer.Write(impact.samples.ToInt16Bytes());
writer.End();
}
}