Browse Source

Adds support for loading from .audio files.

Chris Culy 14 years ago
parent
commit
c314476642
2 changed files with 62 additions and 5 deletions
  1. 59 2
      gameplay/src/AudioSource.cpp
  2. 3 3
      gameplay/src/AudioSource.h

+ 59 - 2
gameplay/src/AudioSource.cpp

@@ -31,6 +31,22 @@ AudioSource* AudioSource::create(const char* path)
 {
 {
     assert(path);
     assert(path);
 
 
+    // Load from a .audio file.
+    std::string pathStr = path;
+    if (pathStr.find(".audio") != pathStr.npos)
+    {
+        Properties* properties = Properties::create(path);
+        assert(properties);
+        if (properties == NULL)
+        {
+            return NULL;
+        }
+
+        AudioSource* audioSource = create(properties->getNextNamespace());
+        SAFE_DELETE(properties);
+        return audioSource;
+    }
+
     // Create an audio buffer from this path.
     // Create an audio buffer from this path.
     AudioBuffer* buffer = AudioBuffer::create(path);
     AudioBuffer* buffer = AudioBuffer::create(path);
     if (buffer == NULL)
     if (buffer == NULL)
@@ -51,8 +67,49 @@ AudioSource* AudioSource::create(const char* path)
 
 
 AudioSource* AudioSource::create(Properties* properties)
 AudioSource* AudioSource::create(Properties* properties)
 {
 {
-    // TODO: Implement this!
-    return NULL;
+    // Check if the properties is valid and has a valid namespace.
+    assert(properties);
+    if (!properties || !(strcmp(properties->getNamespace(), "audio") == 0))
+    {
+        WARN("Failed to load audio source from properties object: must be non-null object and have namespace equal to \'audio\'.");
+        return NULL;
+    }
+
+    const char* path = properties->getString("path");
+    if (path == NULL)
+    {
+        WARN("Audio file failed to load; the file path was not specified.");
+        return NULL;
+    }
+
+    // Create the audio source.
+    AudioSource* audio = AudioSource::create(path);
+    if (audio == NULL)
+    {
+        WARN_VARG("Audio file '%s' failed to load properly.", path);
+        return NULL;
+    }
+
+    // Set any properties that the user specified in the .audio file.
+    if (properties->getString("looped") != NULL)
+    {
+        audio->setLooped(properties->getBool("looped"));
+    }
+    if (properties->getString("gain") != NULL)
+    {
+        audio->setGain(properties->getFloat("gain"));
+    }
+    if (properties->getString("pitch") != NULL)
+    {
+        audio->setPitch(properties->getFloat("pitch"));
+    }
+    Vector3 v;
+    if (properties->getVector3("velocity", &v))
+    {
+        audio->setVelocity(v);
+    }
+
+    return audio;
 }
 }
 
 
 AudioSource::State AudioSource::getState() const
 AudioSource::State AudioSource::getState() const

+ 3 - 3
gameplay/src/AudioSource.h

@@ -33,11 +33,11 @@ public:
     };
     };
 
 
     /**
     /**
-     * Create an audio source. This is used to instantiate an Audio Source. Currently only wav, au and raw files are supported.
+     * Create an audio source. This is used to instantiate an Audio Source. Currently only wav, au, raw and .audio files are supported.
      *
      *
-     * @param path The relative location on disk of the sound file.
+     * @param path The relative location on disk of the sound file or .audio file.
      * 
      * 
-     * @return The newly created audio source, or NULL if an audio source with the given ID already exists.
+     * @return The newly created audio source, or NULL if an audio source cannot be created.
      */
      */
     static AudioSource* create(const char* path);
     static AudioSource* create(const char* path);