Module: Audio::Cache

Defined in:
scripts/00000 Dependencies/00400 Patch_Ajouts_LiteRGSS/00800 Audio___Cache.rb

Overview

Module that cache sounds during the game

Class Method Summary collapse

Class Method Details

.create_sound_get_flags(flags) ⇒ Integer

Return the expected flag for create_sound_sound

Parameters:

  • flags (Integer, nil)

    the FMOD flags for the creation

Returns:



70
71
72
73
# File 'scripts/00000 Dependencies/00400 Patch_Ajouts_LiteRGSS/00800 Audio___Cache.rb', line 70

def create_sound_get_flags(flags)
  return (flags | FMOD::MODE::OPENMEMORY | FMOD::MODE::CREATESTREAM) if flags
  return (FMOD::MODE::LOOP_NORMAL | FMOD::MODE::FMOD_2D | FMOD::MODE::OPENMEMORY | FMOD::MODE::CREATESTREAM)
end

.create_sound_sound(filename, flags = nil) ⇒ FMOD::Sound

Create a bgm sound used to play the BGM

Parameters:

  • filename (String)

    the correct filename of the sound

  • flags (Integer, nil) (defaults to: nil)

    the FMOD flags for the creation

Returns:

  • (FMOD::Sound)

    the sound



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'scripts/00000 Dependencies/00400 Patch_Ajouts_LiteRGSS/00800 Audio___Cache.rb', line 48

def create_sound_sound(filename, flags = nil)
  Yuki::ElapsedTime.start(:audio_load_sound)
  if (file_data = @sound_cache[filename])
    @sound_count[filename] += 1
  else
    file_data = File.binread(filename)
    Yuki::ElapsedTime.show(:audio_load_sound, 'Loading sound from disk took')
  end
  gm_filename = filename.include?('.mid') && File.exist?('gm.dls') ? 'gm.dls' : nil
  sound_info = FMOD::SoundExInfo.new(file_data.bytesize, nil, nil, nil, nil, nil, gm_filename)
  sound = FMOD::System.createSound(file_data, create_sound_get_flags(flags), sound_info)
  sound.instance_variable_set(:@extinfo, sound_info)
  Yuki::ElapsedTime.show(:audio_load_sound, 'Creating sound object took')
  return sound
rescue Errno::ENOENT
  log_error("Failed to load sound : #{filename}")
  return nil
end

.flush_sound

Flush the sound cache if the sounds are not lapsed



76
77
78
79
80
81
82
83
84
85
86
# File 'scripts/00000 Dependencies/00400 Patch_Ajouts_LiteRGSS/00800 Audio___Cache.rb', line 76

def flush_sound
  to_delete = []
  @sound_cache.each_key do |filename|
    to_delete << filename if (@sound_count[filename] -= 1) <= 0
  end
  to_delete.reverse_each do |filename|
    log_info "Audio::Cache : #{filename} released."
    @sound_count.delete(filename)
    @sound_cache.delete(filename)
  end
end

.load

Start the file loading



25
26
27
# File 'scripts/00000 Dependencies/00400 Patch_Ajouts_LiteRGSS/00800 Audio___Cache.rb', line 25

def load
  @thread.wakeup
end

.load_files

Load the files



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'scripts/00000 Dependencies/00400 Patch_Ajouts_LiteRGSS/00800 Audio___Cache.rb', line 30

def load_files
  loads = @sound_loads.clone
  @sound_loads.clear
  Thread.new do
    while (filename = loads.pop)
      next(@sound_count[filename] = 5) if @sound_cache[filename]
      t = Time.new
      @sound_cache[filename] = File.open(filename, 'rb') { |f| f.read(f.size) }
      @sound_count[filename] = 5
      log_info "\rAudio::Cache : #{filename} loaded in #{Time.new - t}s" unless PSDK_CONFIG.release?
    end
  end
end

.preload_sound(filename)

Preload a sound

Parameters:



90
91
92
93
94
# File 'scripts/00000 Dependencies/00400 Patch_Ajouts_LiteRGSS/00800 Audio___Cache.rb', line 90

def preload_sound(filename)
  filename = Audio.search_filename(filename)
  return unless sound_exist?(filename)
  @sound_loads << filename
end

.sound_exist?(filename) ⇒ Boolean

Test if a sound exist

Parameters:

Returns:

  • (Boolean)


98
99
100
# File 'scripts/00000 Dependencies/00400 Patch_Ajouts_LiteRGSS/00800 Audio___Cache.rb', line 98

def sound_exist?(filename)
  return File.exist?(filename)
end

.start

Start the Audio cache



14
15
16
17
18
19
20
21
22
# File 'scripts/00000 Dependencies/00400 Patch_Ajouts_LiteRGSS/00800 Audio___Cache.rb', line 14

def start
  return if @thread
  @thread = Thread.new do
    loop do
      sleep
      load_files
    end
  end
end