示例#1
0
文件: Audio.cpp 项目: AlexYates/nme
bool loadOggSampleFromFile(const char *inFileURL, QuickVec<unsigned char> &outBuffer, int *channels, int *bitsPerSample, int* outSampleRate)
{
    FILE *f;

    //Read the file data
#ifdef ANDROID
    FileInfo info = AndroidGetAssetFD(inFileURL);
    f = fdopen(info.fd, "rb");
    fseek(f, info.offset, 0);
#else
    f = fopen(inFileURL, "rb");
#endif

    if (!f)
    {
        LOG_SOUND("FAILED to read sound file, file pointer as null?\n");
        return false;
    }

    OggVorbis_File oggFile;
    //Read the file data
#ifdef ANDROID
    ov_open(f, &oggFile, NULL, info.length);
#else
    ov_open(f, &oggFile, NULL, 0);
#endif

    return loadOggSample(oggFile, outBuffer, channels, bitsPerSample, outSampleRate);
}
示例#2
0
 int AudioStream_Ogg::getLength(const std::string &path) {
      
      if (openal_is_shutdown) return 0;
      
      int result;
      mPath = std::string(path.c_str());
      mIsValid = true;
      
      #ifdef ANDROID
      
      mInfo = AndroidGetAssetFD(path.c_str());
      oggFile = fdopen(mInfo.fd, "rb");
      fseek(oggFile, mInfo.offset, 0);
      
      ov_callbacks callbacks;
      callbacks.read_func = &nme::AudioStream_Ogg::read_func;
      callbacks.seek_func = &nme::AudioStream_Ogg::seek_func;
      callbacks.close_func = &nme::AudioStream_Ogg::close_func;
      callbacks.tell_func = &nme::AudioStream_Ogg::tell_func;
      
      #else
      
      oggFile = fopen(path.c_str(), "rb");
      
      #endif
      
      if(!oggFile) {
          //throw std::string("Could not open Ogg file.");
          LOG_SOUND("Could not open Ogg file.");
          mIsValid = false;
          return 0;
      }
      
      oggStream = new OggVorbis_File();
      
      #ifdef ANDROID
      result = ov_open_callbacks(this, oggStream, NULL, 0, callbacks);
      #else
      result = ov_open(oggFile, oggStream, NULL, 0);
      #endif
      
      if(result < 0) {
       
          fclose(oggFile);
          oggFile = 0;
       
          //throw std::string("Could not open Ogg stream. ") + errorString(result);
          LOG_SOUND("Could not open Ogg stream.");
          //LOG_SOUND(errorString(result).c_str());
          mIsValid = false;
          return 0;
      }
      
      return ov_time_total(oggStream, -1);
 }
示例#3
0
文件: Audio.cpp 项目: AlexYates/nme
AudioFormat determineFormatFromFile(const std::string &filename)
{
    std::string extension = _get_extension(filename);

    if( extension.compare("ogg") == 0 || extension.compare("oga") == 0)
        return eAF_ogg;
    else if( extension.compare("wav") == 0)
        return eAF_wav;
    else if (extension.compare("mp3") == 0)
        return eAF_mp3;
    else if (extension.compare("mid") == 0)
        return eAF_mid;

    AudioFormat format = eAF_unknown;

#ifdef ANDROID
    FileInfo info = AndroidGetAssetFD(filename.c_str());
    FILE *f = fdopen(info.fd, "rb");
    fseek(f, info.offset, 0);
#else
    FILE *f = fopen(filename.c_str(), "rb");
#endif

    int len = 35;
    char *bytes = (char*)calloc(len + 1, sizeof(char));

    if (f)
    {
        if (fread(bytes, 1, len, f))
        {
            fclose(f);
            format = determineFormatFromBytes((float*)bytes, len);
        }

        fclose(f);
    }

    delete bytes;
    return format;
}
示例#4
0
文件: Audio.cpp 项目: AlexYates/nme
bool loadWavSampleFromFile(const char *inFileURL, QuickVec<unsigned char> &outBuffer, int *channels, int *bitsPerSample, int* outSampleRate)
{
    //http://www.dunsanyinteractive.com/blogs/oliver/?p=72

    //Local Declarations
    FILE* f = NULL;
    WAVE_Format wave_format;
    RIFF_Header riff_header;
    WAVE_Data wave_data;
    unsigned char* data;

#ifdef ANDROID
    FileInfo info = AndroidGetAssetFD(inFileURL);
    f = fdopen(info.fd, "rb");
    fseek(f, info.offset, 0);
#else
    f = fopen(inFileURL, "rb");
#endif

    if (!f)
    {
        LOG_SOUND("FAILED to read sound file, file pointer as null?\n");
        return false;
    }

    // Read in the first chunk into the struct
    fread(&riff_header, sizeof(RIFF_Header), 1, f);
    //check for RIFF and WAVE tag in memeory
    if ((riff_header.chunkID[0] != 'R'  ||
            riff_header.chunkID[1] != 'I'  ||
            riff_header.chunkID[2] != 'F'  ||
            riff_header.chunkID[3] != 'F') ||
            (riff_header.format[0] != 'W'  ||
             riff_header.format[1] != 'A'  ||
             riff_header.format[2] != 'V'  ||
             riff_header.format[3] != 'E'))
    {
        LOG_SOUND("Invalid RIFF or WAVE Header!\n");
        return false;
    }

    //Read in the 2nd chunk for the wave info
    fread(&wave_format, sizeof(WAVE_Format), 1, f);

    //check for fmt tag in memory
    if (wave_format.subChunkID[0] != 'f' ||
            wave_format.subChunkID[1] != 'm' ||
            wave_format.subChunkID[2] != 't' ||
            wave_format.subChunkID[3] != ' ')
    {
        LOG_SOUND("Invalid Wave Format!\n");
        return false;
    }

    //check for extra parameters;
    if (wave_format.subChunkSize > 16)
    {
        fseek(f, sizeof(short), SEEK_CUR);
    }

    //Read in the the last byte of data before the sound file
    fread(&wave_data, sizeof(WAVE_Data), 1, f);

    //check for data tag in memory
    if (wave_data.subChunkID[0] != 'd' ||
            wave_data.subChunkID[1] != 'a' ||
            wave_data.subChunkID[2] != 't' ||
            wave_data.subChunkID[3] != 'a')
    {
        LOG_SOUND("Invalid Wav Data Header!\n");
        return false;
    }

    //Allocate memory for data
    data = new unsigned char[wave_data.subChunkSize];

    // Read in the sound data into the soundData variable
    if (!fread(data, wave_data.subChunkSize, 1, f))
    {
        LOG_SOUND("error loading WAVE data into struct!\n");
        return false;
    }

    //Store in the outbuffer
    outBuffer.Set(data, wave_data.subChunkSize);

    //Now we set the variables that we passed in with the
    //data from the structs
    *outSampleRate = (int)wave_format.sampleRate;

    //The format is worked out by looking at the number of
    //channels and the bits per sample.
    *channels = wave_format.numChannels;
    *bitsPerSample = wave_format.bitsPerSample;

    //clean up and return true if successful
    fclose(f);
    delete[] data;

    return true;
}
示例#5
0
   bool open(const std::string &path, int startTime)
   {
        int result;
        mPath = std::string(path.c_str());
        mIsValid = true;
        
        #ifdef ANDROID
        
        mInfo = AndroidGetAssetFD(path.c_str());
        oggFile = fdopen(mInfo.fd, "rb");
        fseek(oggFile, mInfo.offset, 0);
        
        ov_callbacks callbacks;
        callbacks.read_func = &nme::AudioStream_Ogg::read_func;
        callbacks.seek_func = &nme::AudioStream_Ogg::seek_func;
        callbacks.close_func = &nme::AudioStream_Ogg::close_func;
        callbacks.tell_func = &nme::AudioStream_Ogg::tell_func;
        
        #else
        
        oggFile = fopen(path.c_str(), "rb");
        
        #endif
        
        if(!oggFile) {
            //throw std::string("Could not open Ogg file.");
            LOG_SOUND("Could not open Ogg file.");
            mIsValid = false;
            return false;
        }
        
        oggStream = new OggVorbis_File();
        
        #ifdef ANDROID
        result = ov_open_callbacks(this, oggStream, NULL, 0, callbacks);
        #else
        result = ov_open(oggFile, oggStream, NULL, 0);
        #endif
         
        if(result < 0) {
         
            fclose(oggFile);
            oggFile = 0;
         
            //throw std::string("Could not open Ogg stream. ") + errorString(result);
            LOG_SOUND("Could not open Ogg stream.");
            //LOG_SOUND(errorString(result).c_str());
            mIsValid = false;
            return false;
        }

        vorbisInfo = ov_info(oggStream, -1);
        vorbisComment = ov_comment(oggStream, -1);

        mChannels = vorbisInfo->channels;
        mRate = vorbisInfo->rate;

        if (startTime != 0)
        {
          double seek = startTime * 0.001;
          ov_time_seek(oggStream, seek);
        }

        return true;
   }
示例#6
0
   void AudioStream_Ogg::open(const std::string &path, int startTime, int inLoops, const SoundTransform &inTransform) {
        
        if (openal_is_shutdown) return;
   
        int result;
        mPath = std::string(path.c_str());
        mStartTime = startTime;
        mLoops = inLoops;
        mIsValid = true;
        mSuspend = false;
        
        #ifdef ANDROID
        
        mInfo = AndroidGetAssetFD(path.c_str());
        oggFile = fdopen(mInfo.fd, "rb");
        fseek(oggFile, mInfo.offset, 0);
        
        ov_callbacks callbacks;
        callbacks.read_func = &nme::AudioStream_Ogg::read_func;
        callbacks.seek_func = &nme::AudioStream_Ogg::seek_func;
        callbacks.close_func = &nme::AudioStream_Ogg::close_func;
        callbacks.tell_func = &nme::AudioStream_Ogg::tell_func;
        
        #else
        
        oggFile = fopen(path.c_str(), "rb");
        
        #endif
        
        if(!oggFile) {
            //throw std::string("Could not open Ogg file.");
            LOG_SOUND("Could not open Ogg file.");
            mIsValid = false;
            return;
        }
        
        oggStream = new OggVorbis_File();
        
        #ifdef ANDROID
        result = ov_open_callbacks(this, oggStream, NULL, 0, callbacks);
        #else
        result = ov_open(oggFile, oggStream, NULL, 0);
        #endif
         
        if(result < 0) {
         
            fclose(oggFile);
            oggFile = 0;
         
            //throw std::string("Could not open Ogg stream. ") + errorString(result);
            LOG_SOUND("Could not open Ogg stream.");
            //LOG_SOUND(errorString(result).c_str());
            mIsValid = false;
            return;
        }

        vorbisInfo = ov_info(oggStream, -1);
        vorbisComment = ov_comment(oggStream, -1);

        if(vorbisInfo->channels == 1) {
            format = AL_FORMAT_MONO16;
        } else {
            format = AL_FORMAT_STEREO16;
        }
        
        if (startTime != 0)
        {
          double seek = startTime * 0.001;
          ov_time_seek(oggStream, seek);
        }
        
        alGenBuffers(2, buffers);
        check();
        alGenSources(1, &source);
        check();

        alSource3f(source, AL_POSITION,        0.0, 0.0, 0.0);
        alSource3f(source, AL_VELOCITY,        0.0, 0.0, 0.0);
        alSource3f(source, AL_DIRECTION,       0.0, 0.0, 0.0);
        alSourcef (source, AL_ROLLOFF_FACTOR,  0.0          );
        alSourcei (source, AL_SOURCE_RELATIVE, AL_TRUE      );
        
        setTransform(inTransform);
   } //open