🎵 AudioSource – GameCrom Audio System

The AudioSource component allows you to play sounds on any object. This system uses WebAudio and preloads all sounds when the scene loads, enabling instant playback even on mobile devices or iPad.


📦 AudioSource Properties

{
  "type": "AudioSource",
  "clip": "explosion.mp3",
  "volume": 0.8,
  "loop": false,
  "playOnStart": false
}

🧠 Automatic Memory Preload (WebAudio)

The engine preloads ALL audio files before the game starts:

This guarantees excellent performance even on iPad and mobile devices.


🎮 Script Functions

▶ Play sound

PlayAudioSource(obj);

⏹ Stop sound

StopAudioSource(obj);

🌫 Fade Out (fade + stop)

FadeOutAudio(obj, 2);   // 2 seconds

🌅 Fade In (starts at volume 0)

FadeInAudio(obj, 2);    // 2 seconds

🎧 Change clip dynamically

const audio = getComponent(obj, "AudioSource");
audio.clip = "jump.mp3";
PlayAudioSource(obj);

🔥 Full Example: Changing Music with a Fade

// AUDIO_CONTROL.js

function Start(obj) {
  Timer(3, "Stop_music", false, obj);
}

function Stop_music(obj) {
  const audio = getComponent(obj, "AudioSource");

  // Change clip
  audio.clip = "jump.mp3";

  // Fade in the new sound
  FadeInAudio(obj, 2);
}

function Update(obj, dt) {}

🎚 CrossFade Between Tracks

Smooth transition between two music tracks.

function CrossFade(obj, newClip, time) {
  FadeOutAudio(obj, time);
  Timer(time, "StartNewMusic", false, { obj, newClip, time });
}

function StartNewMusic(ctx) {
  const audio = getComponent(ctx.obj, "AudioSource");
  audio.clip = ctx.newClip;
  FadeInAudio(ctx.obj, ctx.time);
}

// Usage:
CrossFade(obj, "boss_theme.mp3", 2);

📌 Change Volume from Script

const audio = getComponent(obj, "AudioSource");
audio.volume = 0.3;   // 30%

📌 Play Independent SFX (One Shot)

You can use PlaySound() to play sounds without an AudioSource:

PlaySound("laser.mp3", 1.0);
Useful for:

📌 Best Practices

✔ Use one AudioSource per background music track.
✔ Use PlaySound() for quick sound effects.
✔ Dynamically change .clip to reuse the same AudioSource.
✔ Avoid multiple loops playing at the same time.
✔ Use FadeIn/FadeOut for professional transitions.
✔ All sounds are automatically preloaded: no runtime loading.