Base class for reading ogg and uncompressed wav files, from disk or memory streams. Use OpenAudioFileAtPath to retrieve an instance. GATPathsHelper provides handy methods for converting relative paths to absolute paths. Don't forget to call Dispose() to close the file and release resources!
Inheritance: IDisposable
Exemple #1
0
        void LoadSampleFromStreamingAssets(GATDataAllocationMode mode, GATSampleInfo info, Dictionary <string, GATData> loadedSamples)
        {
            AGATAudioFile file;
            string        path;

            GATData[] loadedChannels;
            int       i;

            path = info.GetStreamingAssetFullPath();

            using (file = AGATAudioFile.OpenAudioFileAtPath(path))
            {
                loadedChannels = GATAudioLoader.SharedInstance.LoadSync(file, mode);
            }

            if (loadedChannels.Length == 1)
            {
                loadedSamples.Add(info.Name, loadedChannels[0]);
                return;
            }

            for (i = 0; i < loadedChannels.Length; i++)
            {
                loadedSamples.Add(string.Format("{0}_{1}", info.Name, i), loadedChannels[i]);
            }
        }
Exemple #2
0
        /// <summary>
        /// Blocking load of wav and ogg files.
        /// </summary>
        public GATData[] LoadSync( AGATAudioFile file, GATDataAllocationMode allocationMode )
        {
            GATData[] loadedChannels;
            int i;
            bool didFail = false;

            loadedChannels = new GATData[ file.Channels ];

            for( i = 0; i < file.Channels; i++ )
            {
                if( allocationMode == GATDataAllocationMode.Fixed ) //GZComment: allocation fail leaks
                {
                    try{ loadedChannels[ i ] = GATManager.GetFixedDataContainer( file.NumFrames, file.FileName ); }
                    catch( System.Exception ex )
                    {
                        didFail = true;
                        #if UNITY_EDITOR
                        Debug.LogException( ex );
                        #endif
                    }
                }
                else if( allocationMode == GATDataAllocationMode.Managed ) //GZComment: allocation fail leaks
                {
                    try
                    {
                        loadedChannels[ i ] = GATManager.GetDataContainer( file.NumFrames );
                    }
                    catch( System.Exception ex )
                    {
                        didFail = true;
                        #if UNITY_EDITOR
                        Debug.LogException( ex );
                        #endif
                    }
                }
                else
                {
                    loadedChannels[ i ] = new GATData( new float[ file.NumFrames ] );
                }
            }

            if( didFail )
            {
                for( i = 0; i < loadedChannels.Length; i++ )
                {
                    if( loadedChannels[ i ] != null )
                        loadedChannels[ i ].Release();
                }

                return null;
            }

            if( file.Channels == 1 )
            {
                file.ReadNextChunk( loadedChannels[ 0 ].ParentArray, loadedChannels[ 0 ].MemOffset, file.NumFrames );
                return loadedChannels;
            }

            int framesPerRead;
            int framesRead;
            int totalFramesRead = 0;

            framesPerRead = _buffer.Length / file.Channels;

            while( true )
            {
                framesRead = file.ReadNextChunk( _buffer, 0, framesPerRead );

                for( i = 0; i < file.Channels; i++ )
                {
                    loadedChannels[ i ].CopyFromInterlaced( _buffer, framesRead, totalFramesRead, i, file.Channels );
                }

                totalFramesRead += framesRead;

                if( framesRead < framesPerRead )
                    break;
            }

            return loadedChannels;
        }
Exemple #3
0
        /// <summary>
        /// Blocking load of wav and ogg files.
        /// </summary>
        public GATData[] LoadSync(AGATAudioFile file, GATDataAllocationMode allocationMode)
        {
            GATData[] loadedChannels;
            int       i;
            bool      didFail = false;

            loadedChannels = new GATData[file.Channels];

            for (i = 0; i < file.Channels; i++)
            {
                if (allocationMode == GATDataAllocationMode.Fixed)                  //GZComment: allocation fail leaks
                {
                    try{ loadedChannels[i] = GATManager.GetFixedDataContainer(file.NumFrames, file.FileName); }
                    catch (System.Exception ex)
                    {
                        didFail = true;
                                                #if UNITY_EDITOR
                        Debug.LogException(ex);
                                                #endif
                    }
                }
                else if (allocationMode == GATDataAllocationMode.Managed)                  //GZComment: allocation fail leaks
                {
                    try
                    {
                        loadedChannels[i] = GATManager.GetDataContainer(file.NumFrames);
                    }
                    catch (System.Exception ex)
                    {
                        didFail = true;
                                                #if UNITY_EDITOR
                        Debug.LogException(ex);
                                                #endif
                    }
                }
                else
                {
                    loadedChannels[i] = new GATData(new float[file.NumFrames]);
                }
            }

            if (didFail)
            {
                for (i = 0; i < loadedChannels.Length; i++)
                {
                    if (loadedChannels[i] != null)
                    {
                        loadedChannels[i].Release();
                    }
                }

                return(null);
            }

            if (file.Channels == 1)
            {
                file.ReadNextChunk(loadedChannels[0].ParentArray, loadedChannels[0].MemOffset, file.NumFrames);
                return(loadedChannels);
            }

            int framesPerRead;
            int framesRead;
            int totalFramesRead = 0;

            framesPerRead = _buffer.Length / file.Channels;

            while (true)
            {
                framesRead = file.ReadNextChunk(_buffer, 0, framesPerRead);

                for (i = 0; i < file.Channels; i++)
                {
                    loadedChannels[i].CopyFromInterlaced(_buffer, framesRead, totalFramesRead, i, file.Channels);
                }

                totalFramesRead += framesRead;

                if (framesRead < framesPerRead)
                {
                    break;
                }
            }

            return(loadedChannels);
        }
Exemple #4
0
            public GATData[] LoadNext(BackgroundWorker worker, float[] deInterleaveBuffer)
            {
                GATData[] containers;
                int       i;
                int       channels;

                if (_paths.Count == 0)
                {
                    Status = LoadOperationStatus.Done;
                    return(null);
                }

                AGATAudioFile file;

                try
                {
                    file = AGATAudioFile.OpenAudioFileAtPath(_paths.Dequeue());
                }
                catch (System.Exception e)
                {
#if UNITY_EDITOR
                    Debug.LogException(e);
#endif
                    FailReason = LoadOperationFailReason.CannotOpenFile;
                    Status     = LoadOperationStatus.Failed;
                    return(null);
                }

                using ( file )
                {
                    channels        = file.Channels;
                    CurrentFileName = file.FileName;
                    if (!_forceMono && channels > 1)
                    {
                        containers = new GATData[channels];
                    }
                    else
                    {
                        containers = new GATData[1];
                    }

                    for (i = 0; i < containers.Length; i++)
                    {
                        if (_allocationMode == GATDataAllocationMode.Fixed)                          //GZComment: allocation fail leaks
                        {
                            try{ containers[i] = GATManager.GetFixedDataContainer(file.NumFrames, file.FileName); }
                            catch (System.Exception ex)
                            {
                                ReleaseContainers(containers);
                                Status     = LoadOperationStatus.Failed;
                                FailReason = LoadOperationFailReason.OutOfPreAllocatedMemory;
                                                                #if UNITY_EDITOR
                                Debug.LogException(ex);
                                                                #endif
                                return(null);
                            }
                        }
                        else if (_allocationMode == GATDataAllocationMode.Managed)                          //GZComment: allocation fail leaks
                        {
                            try
                            {
                                containers[i] = GATManager.GetDataContainer(file.NumFrames);
                            }
                            catch (System.Exception ex)
                            {
                                ReleaseContainers(containers);
                                Status     = LoadOperationStatus.Failed;
                                FailReason = LoadOperationFailReason.NoLargeEnoughChunkInAllocator;
                                                                #if UNITY_EDITOR
                                Debug.LogException(ex);
                                                                #endif
                                return(null);
                            }
                        }
                        else
                        {
                            containers[i] = new GATData(new float[file.NumFrames]);
                        }
                    }

                    int framesPerRead;
                    int framesRead;
                    int totalFramesRead = 0;

                    if (channels > 1)
                    {
                        framesPerRead = deInterleaveBuffer.Length / channels;

                        while (true)
                        {
                            if (worker.CancellationPending)
                            {
                                ReleaseContainers(containers);

                                return(null);
                            }

                            framesRead = file.ReadNextChunk(deInterleaveBuffer, 0, framesPerRead);

                            if (_forceMono)                              // Only supported for stereo files
                            {
                                int numSamples = framesRead * channels;

                                for (i = 0; i < numSamples; i += channels)
                                {
                                    deInterleaveBuffer[i] += deInterleaveBuffer[i + 1];
                                }

                                containers[0].CopyFromInterlaced(deInterleaveBuffer, framesRead, totalFramesRead, 0, channels);
                            }
                            else
                            {
                                for (i = 0; i < channels; i++)
                                {
                                    containers[i].CopyFromInterlaced(deInterleaveBuffer, framesRead, totalFramesRead, i, channels);
                                }
                            }

                            totalFramesRead += framesRead;

                            if (_reportsProgress)
                            {
                                worker.ReportProgress(0, new ProgressInfo(( float )totalFramesRead / file.NumFrames, file.FileName));
                            }

                            if (framesRead < framesPerRead)
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        int framesLeft;

                        while (totalFramesRead < file.NumFrames)
                        {
                            if (worker.CancellationPending)
                            {
                                ReleaseContainers(containers);
                                return(null);
                            }

                            framesLeft       = file.NumFrames - totalFramesRead;
                            framesPerRead    = (framesLeft < 16384 ? framesLeft : 16384);
                            framesRead       = file.ReadNextChunk(containers[0].ParentArray, containers[0].MemOffset + totalFramesRead, framesPerRead);
                            totalFramesRead += framesRead;

                            if (_reportsProgress)
                            {
                                worker.ReportProgress(0, new ProgressInfo(( float )totalFramesRead / file.NumFrames, file.FileName));
                            }
                        }
                    }
                }


                return(containers);
            }