bool SoundBuffer::update(unsigned int channelCount, unsigned int sampleRate) { // Check parameters if (!channelCount || !sampleRate || m_samples.empty()) return false; // Check if the format is valid if (channelCount > 2){ err() << "Failed to load sound buffer (unsupported number of channels: " << channelCount << ")" << std::endl; return false; } m_channelCount = channelCount; m_sampleRate = sampleRate; // First make a copy of the list of sounds so we can reattach later SoundList sounds(m_sounds); // Detach the buffer from the sounds that use it (to avoid OpenAL errors) for (SoundList::const_iterator it = sounds.begin(); it != sounds.end(); ++it) (*it)->resetBuffer(); // Compute the duration m_duration = seconds(static_cast<float>(m_samples.size()) / sampleRate / channelCount); // Now reattach the buffer to the sounds that use it for (SoundList::const_iterator it = sounds.begin(); it != sounds.end(); ++it) (*it)->setBuffer(*this); return true; }
void compile(const char* path, CompileOptions& opts) { static const uint32_t VERSION = 1; Buffer buf = opts.read(path); JSONParser json(array::begin(buf)); JSONElement root = json.root(); Array<LevelUnit> units(default_allocator()); Array<LevelSound> sounds(default_allocator()); { JSONElement sounds_arr = root.key("sounds"); const uint32_t size = sounds_arr.size(); for (uint32_t i = 0; i < size; i++) { JSONElement e = sounds_arr[i]; LevelSound ls; ls.name = e.key("name").to_resource_id("sound").name; ls.position = e.key("position").to_vector3(); ls.volume = e.key("volume").to_float(); ls.range = e.key("range").to_float(); ls.loop = e.key("loop").to_bool(); array::push_back(sounds, ls); } } { JSONElement units_arr = root.key("units"); const uint32_t size = units_arr.size(); for (uint32_t i = 0; i < size; i++) { JSONElement e = units_arr[i]; LevelUnit lu; lu.name = e.key("name").to_resource_id("unit").name; lu.position = e.key("position").to_vector3(); lu.rotation = e.key("rotation").to_quaternion(); array::push_back(units, lu); } } LevelResource lr; lr.version = VERSION; lr.num_units = array::size(units); lr.num_sounds = array::size(sounds); uint32_t offt = sizeof(LevelResource); lr.units_offset = offt; offt += sizeof(LevelUnit) * lr.num_units; lr.sounds_offset = offt; opts._bw & lr & units & sounds; }
bool SoundBuffer::update(unsigned int channelCount, unsigned int sampleRate) { // Check parameters if (!channelCount || !sampleRate || m_samples.empty()) return false; // Find the good format according to the number of channels ALenum format = priv::AudioDevice::getFormatFromChannelCount(channelCount); // Check if the format is valid if (format == 0) { err() << "Failed to load sound buffer (unsupported number of channels: " << channelCount << ")" << std::endl; return false; } // First make a copy of the list of sounds so we can reattach later SoundList sounds(m_sounds); // Detach the buffer from the sounds that use it (to avoid OpenAL errors) for (SoundList::const_iterator it = sounds.begin(); it != sounds.end(); ++it) (*it)->resetBuffer(); // Fill the buffer ALsizei size = static_cast<ALsizei>(m_samples.size()) * sizeof(Int16); alCheck(alBufferData(m_buffer, format, &m_samples[0], size, sampleRate)); // Compute the duration m_duration = seconds(static_cast<float>(m_samples.size()) / sampleRate / channelCount); // Now reattach the buffer to the sounds that use it for (SoundList::const_iterator it = sounds.begin(); it != sounds.end(); ++it) (*it)->setBuffer(*this); return true; }