/// <summary> /// Extracts the audio data from an AudioClip and sets it in a GATData object. /// Memory is allocated according to the specified GATDataAllocationMode. /// </summary> public static GATData ToGATData(this AudioClip clip, GATDataAllocationMode mode) { GATData data; float[] tempArray; tempArray = new float[clip.samples]; clip.GetData(tempArray, 0); #if UNITY_EDITOR if (Application.isPlaying == false) { mode = GATDataAllocationMode.Unmanaged; } #endif if (mode == GATDataAllocationMode.Managed) { data = GATManager.GetDataContainer(clip.samples); data.CopyFrom(tempArray, 0, 0, clip.samples); } else if (mode == GATDataAllocationMode.Fixed) { data = GATManager.GetFixedDataContainer(clip.samples, "ClipData: " + clip.name); data.CopyFrom(tempArray, 0, 0, clip.samples); } else { data = new GATData(tempArray); } return(data); }
/// <summary> /// Extracts the audio data from an AudioClip and sets it in a GATData object. /// Memory is allocated according to the specified GATDataAllocationMode. /// </summary> public static GATData ToGATData( this AudioClip clip, GATDataAllocationMode mode ) { GATData data; float[] tempArray; tempArray = new float[ clip.samples ]; clip.GetData( tempArray, 0 ); #if UNITY_EDITOR if( Application.isPlaying == false ) { mode = GATDataAllocationMode.Unmanaged; } #endif if( mode == GATDataAllocationMode.Managed ) { data = GATManager.GetDataContainer( clip.samples ); data.CopyFrom( tempArray, 0, 0, clip.samples ); } else if( mode == GATDataAllocationMode.Fixed ) { data = GATManager.GetFixedDataContainer( clip.samples, "ClipData: "+clip.name ); data.CopyFrom( tempArray, 0, 0, clip.samples ); } else { data = new GATData( tempArray ); } return data; }
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]); } }
public LoadingOperation(GATDataAllocationMode allocationMode, int numFiles, FileLoadedHandler handler, bool forceMono = false) { _allocationMode = allocationMode; _paths = new Queue <string>(numFiles); Status = LoadOperationStatus.Configuring; OnFileWasLoaded = handler; _forceMono = forceMono; }
IEnumerator LoadSampleFromResources(GATDataAllocationMode mode, GATSampleInfo info, Dictionary <string, GATData> loadedSamples) { AudioClip clip; /* #if UNITY_EDITOR * if( Application.isPlaying ) //GZComment: simplifies memory management when trying things out in the editor, where performance is not crucial. * { * clip = Resources.Load( info.PathInResources ) as AudioClip; * if( clip == null ) * { * Debug.LogError( "No Clip at path: " + info.PathInResources); * } * } * else * { * mode = GATDataAllocationMode.Unmanaged; * string assetPath = AssetDatabase.GUIDToAssetPath( info.GUID ); * clip = AssetDatabase.LoadAssetAtPath( assetPath, typeof( AudioClip ) ) as AudioClip; * } * #else */ clip = Resources.Load(info.PathInResources) as AudioClip; //#endif while (clip.loadState != AudioDataLoadState.Loaded) { yield return(null); } if (info.NumChannels == 1) { GATData data; data = clip.ToGATData(mode); loadedSamples.Add(info.Name, data); } else { GATData[] channelsData = clip.ExtractChannels(mode); for (int i = 0; i < info.NumChannels; i++) { loadedSamples.Add(string.Format("{0}_{1}", info.Name, i), channelsData[i]); //Debug.Log("loaded " + string.Format("{0}_{1}", info.Name, i)); } } clipsLoaded++; #if UNITY_EDITOR if (Application.isPlaying) { Resources.UnloadAsset(( Object )clip); } #else Resources.UnloadAsset(clip); #endif }
public Dictionary <string, GATData> LoadAll(GATDataAllocationMode allocationMode) { Dictionary <string, GATData> target = new Dictionary <string, GATData>(_sampleInfos.Count); int i; for (i = 0; i < _sampleInfos.Count; i++) { LoadSample(_sampleInfos[i], target, allocationMode); } return(target); }
void LoadSampleFromResources(GATDataAllocationMode mode, GATSampleInfo info, Dictionary <string, GATData> loadedSamples) { AudioClip clip; #if UNITY_EDITOR if (Application.isPlaying) //GZComment: simplifies memory management when trying things out in the editor, where performance is not crucial. { clip = Resources.Load(info.PathInResources) as AudioClip; if (clip == null) { Debug.LogError("No Clip at path: " + info.PathInResources); } } else { mode = GATDataAllocationMode.Unmanaged; string assetPath = AssetDatabase.GUIDToAssetPath(info.GUID); clip = AssetDatabase.LoadAssetAtPath(assetPath, typeof(AudioClip)) as AudioClip; } #else clip = Resources.Load(info.PathInResources) as AudioClip; #endif if (info.NumChannels == 1) { GATData data; data = clip.ToGATData(mode); loadedSamples.Add(info.Name, data); } else { GATData[] channelsData = clip.ExtractChannels(mode); for (int i = 0; i < info.NumChannels; i++) { loadedSamples.Add(string.Format("{0}_{1}", info.Name, i), channelsData[i]); } } #if UNITY_EDITOR if (Application.isPlaying) { Resources.UnloadAsset(( Object )clip); } #else Resources.UnloadAsset(clip); #endif }
/// <summary> /// The splitter will begin broadcasting it's /// sub streams immediately. /// </summary> public GATAudioThreadStreamSplitter(IGATAudioThreadStream stream, GATDataAllocationMode bufferAllocationMode) { int i; _sourceStreamChannels = stream.NbOfChannels; if (_sourceStreamChannels < 2) { Debug.LogWarning("source stream is mono: " + stream.StreamName); } IntPtr outputBufferPointer = IntPtr.Zero; _sharedBufferSize = stream.BufferSizePerChannel; if (bufferAllocationMode == GATDataAllocationMode.Unmanaged) { _sharedBufferArray = new float[_sharedBufferSize]; _sharedBuffer = new GATData(_sharedBufferArray); } else { if (bufferAllocationMode == GATDataAllocationMode.Fixed) { _sharedBuffer = GATManager.GetFixedDataContainer(_sharedBufferSize, "StreamSplitter buffer"); } else { _sharedBuffer = GATManager.GetDataContainer(_sharedBufferSize); } _sharedBufferArray = _sharedBuffer.ParentArray; outputBufferPointer = _sharedBuffer.GetPointer(); } _memOffset = _sharedBuffer.MemOffset; _streamProxies = new GATAudioThreadStreamProxy[_sourceStreamChannels]; for (i = 0; i < _sourceStreamChannels; i++) { _streamProxies[i] = new GATAudioThreadStreamProxy(_sharedBufferSize, 1, outputBufferPointer, _sharedBuffer.MemOffset, (stream.StreamName + " split " + i)); } stream.AddAudioThreadStreamClient(this); _sourceStream = stream; }
public IEnumerator LoadAll(GATDataAllocationMode allocationMode) { clipsLoaded = 0; Dictionary <string, GATData> target = new Dictionary <string, GATData>(_sampleInfos.Count); int i; for (i = 0; i < _sampleInfos.Count; i++) { LoadSample(_sampleInfos[i], target, allocationMode); } while (clipsLoaded != _sampleInfos.Count) { yield return(null); } loadFinished(target); }
/// <summary> /// The splitter will begin broadcasting it's /// sub streams immediately. /// </summary> public GATAudioThreadStreamSplitter( IGATAudioThreadStream stream, GATDataAllocationMode bufferAllocationMode ) { int i; _sourceStreamChannels = stream.NbOfChannels; if( _sourceStreamChannels < 2 ) { Debug.LogWarning( "source stream is mono: " + stream.StreamName ); } IntPtr outputBufferPointer = IntPtr.Zero; _sharedBufferSize = stream.BufferSizePerChannel; if( bufferAllocationMode == GATDataAllocationMode.Unmanaged ) { _sharedBufferArray = new float[ _sharedBufferSize ]; _sharedBuffer = new GATData( _sharedBufferArray ); } else { if( bufferAllocationMode == GATDataAllocationMode.Fixed ) { _sharedBuffer = GATManager.GetFixedDataContainer( _sharedBufferSize, "StreamSplitter buffer" ); } else { _sharedBuffer = GATManager.GetDataContainer( _sharedBufferSize ); } _sharedBufferArray = _sharedBuffer.ParentArray; outputBufferPointer = _sharedBuffer.GetPointer(); } _memOffset = _sharedBuffer.MemOffset; _streamProxies = new GATAudioThreadStreamProxy[ _sourceStreamChannels ]; for( i = 0; i < _sourceStreamChannels; i++ ) { _streamProxies[ i ] = new GATAudioThreadStreamProxy( _sharedBufferSize, 1, outputBufferPointer, _sharedBuffer.MemOffset, ( stream.StreamName + " split " + i ) ); } stream.AddAudioThreadStreamClient( this ); _sourceStream = stream; }
void LoadSample(GATSampleInfo info, Dictionary <string, GATData> target, GATDataAllocationMode allocationMode) { if (info.IsStreamingAsset) { #if UNITY_EDITOR if (Application.isPlaying == false) { LoadSampleFromResources(GATDataAllocationMode.Unmanaged, info, target); return; } #endif LoadSampleFromStreamingAssets(allocationMode, info, target); } else { LoadSampleFromResources(allocationMode, info, target); } }
/// <summary> /// Performs the same operation as ToGATData, but splits interleaved channels in seperate GATData /// instances. /// </summary> public static GATData[] ExtractChannels( this AudioClip clip, GATDataAllocationMode mode ) { GATData[] channelsData; float[] tempArray; int i; int length; tempArray = new float[ clip.samples * clip.channels ]; clip.GetData( tempArray, 0 ); channelsData = new GATData[ clip.channels ]; length = clip.samples; #if UNITY_EDITOR if( Application.isPlaying == false ) { mode = GATDataAllocationMode.Unmanaged; } #endif for( i = 0; i < clip.channels; i++ ) { if( mode == GATDataAllocationMode.Managed ) { channelsData[ i ] = GATManager.GetDataContainer( length ); } else if( mode == GATDataAllocationMode.Fixed ) { channelsData[ i ] = GATManager.GetFixedDataContainer( length, clip.name+" channel"+i+" data" ); } else { channelsData[ i ] = new GATData( new float[ length ] ); } channelsData[ i ].CopyFromInterlaced( tempArray, length, i, clip.channels ); } return channelsData; }
/// <summary> /// Performs the same operation as ToGATData, but splits interleaved channels in seperate GATData /// instances. /// </summary> public static GATData[] ExtractChannels(this AudioClip clip, GATDataAllocationMode mode) { GATData[] channelsData; float[] tempArray; int i; int length; tempArray = new float[clip.samples * clip.channels]; clip.GetData(tempArray, 0); channelsData = new GATData[clip.channels]; length = clip.samples; #if UNITY_EDITOR if (Application.isPlaying == false) { mode = GATDataAllocationMode.Unmanaged; } #endif for (i = 0; i < clip.channels; i++) { if (mode == GATDataAllocationMode.Managed) { channelsData[i] = GATManager.GetDataContainer(length); } else if (mode == GATDataAllocationMode.Fixed) { channelsData[i] = GATManager.GetFixedDataContainer(length, clip.name + " channel" + i + " data"); } else { channelsData[i] = new GATData(new float[length]); } channelsData[i].CopyFromInterlaced(tempArray, length, i, clip.channels); } return(channelsData); }
void LoadSample(GATSampleInfo info, Dictionary <string, GATData> target, GATDataAllocationMode allocationMode) { if (info.IsStreamingAsset) { #if UNITY_EDITOR if (Application.isPlaying == false) { Debug.Log("Editor only ResourcesAsset"); LoadSampleFromResources(GATDataAllocationMode.Unmanaged, info, target); return; } #endif Debug.Log("StreamingAsset"); LoadSampleFromStreamingAssets(allocationMode, info, target); } else { UnitySingleton <CoroutineMaster> .Instance.StartCoroutine(LoadSampleFromResources(allocationMode, info, target)); } }
/// <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; }
/// <summary> /// Gets a LoadingOperation object to which /// you may add files and callbacks. To start /// the operation, call EnqueueOperation. /// </summary> public AGATLoadingOperation NewOperation(int numFilesToLoad, GATDataAllocationMode allocationMode, FileLoadedHandler onFileWasLoaded, bool forceMono = false) { return(new LoadingOperation(allocationMode, numFilesToLoad, onFileWasLoaded, forceMono) as AGATLoadingOperation); }
/// <summary> /// Convenience method for loading files asynchronously /// to a sample bank. This method instantiates, configures and /// enqueues a AGATLoadingOperation. Use NewOperation if you need /// more control over progress callbacks. /// </summary> public void LoadFilesToSampleBank( string[] filePaths, PathRelativeType pathType, GATSampleBank targetBank, GATDataAllocationMode allocationMode, OperationCompletedHandler onOperationCompleted, bool forceMono = false ) { AGATLoadingOperation operation = new LoadingOperation( allocationMode, filePaths.Length, targetBank.AddLoadedFile, forceMono ); int i; for( i = 0; i < filePaths.Length; i++ ) { operation.AddFile( filePaths[ i ], pathType ); } operation.OnOperationCompleted = onOperationCompleted; EnqueueOperation( operation ); }
public Dictionary <string, GATData> LoadSamplesNamed(List <string> sampleNames, GATDataAllocationMode allocationMode) { int i; GATSampleInfo info; Dictionary <string, GATData> target = new Dictionary <string, GATData>(sampleNames.Count); for (i = 0; i < _sampleInfos.Count; i++) { info = _sampleInfos[i]; if (sampleNames.Contains(info.Name)) { LoadSample(info, target, allocationMode); } } return(target); }
/// <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); }
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 ] ); } }
void LoadSampleFromResources( GATDataAllocationMode mode, GATSampleInfo info, Dictionary< string, GATData > loadedSamples ) { AudioClip clip; #if UNITY_EDITOR if( Application.isPlaying ) //GZComment: simplifies memory management when trying things out in the editor, where performance is not crucial. { clip = Resources.Load( info.PathInResources ) as AudioClip; if( clip == null ) { Debug.LogError( "No Clip at path: " + info.PathInResources); } } else { mode = GATDataAllocationMode.Unmanaged; string assetPath = AssetDatabase.GUIDToAssetPath( info.GUID ); clip = AssetDatabase.LoadAssetAtPath( assetPath, typeof( AudioClip ) ) as AudioClip; } #else clip = Resources.Load( info.PathInResources ) as AudioClip; #endif if( info.NumChannels == 1 ) { GATData data; data = clip.ToGATData( mode ); loadedSamples.Add( info.Name, data ); } else { GATData[] channelsData = clip.ExtractChannels( mode ); for( int i = 0; i < info.NumChannels; i++ ) { loadedSamples.Add( string.Format( "{0}_{1}", info.Name, i ), channelsData[i] ); } } #if UNITY_EDITOR if( Application.isPlaying ) { Resources.UnloadAsset( ( Object )clip ); } #else Resources.UnloadAsset( clip ); #endif }
/// <summary> /// Convenience method for loading files asynchronously /// to a sample bank. All wav and ogg files of the specified folder /// will be loaded. /// This method instantiates, configures and enqueues a AGATLoadingOperation. /// Use the NewOperation method if you need more control over progress callbacks. /// </summary> public void LoadFolderToSampleBank(string folderPath, PathRelativeType pathType, GATSampleBank targetBank, GATDataAllocationMode allocationMode, OperationCompletedHandler onOperationCompleted, bool forceMono = false) { folderPath = GATPathsHelper.GetAbsolutePath(folderPath, pathType, false); if (Directory.Exists(folderPath) == false) { throw new GATException("No such directory!"); } string[] files = Directory.GetFiles(folderPath); if (files.Length == 0) { #if UNITY_EDITOR || GAT_DEBUG Debug.LogError("Directory exists but is empty"); #endif return; } LoadFilesToSampleBank(files, PathRelativeType.Absolute, targetBank, allocationMode, onOperationCompleted, forceMono); }
/// <summary> /// Gets a LoadingOperation object to which /// you may add files and callbacks. To start /// the operation, call EnqueueOperation. /// </summary> public AGATLoadingOperation NewOperation( int numFilesToLoad, GATDataAllocationMode allocationMode, FileLoadedHandler onFileWasLoaded, bool forceMono = false ) { return new LoadingOperation( allocationMode, numFilesToLoad, onFileWasLoaded, forceMono ) as AGATLoadingOperation; }
public Dictionary<string, GATData> LoadAll( GATDataAllocationMode allocationMode ) { Dictionary< string, GATData > target = new Dictionary<string, GATData>( _sampleInfos.Count ); int i; for( i = 0; i < _sampleInfos.Count; i++ ) { LoadSample( _sampleInfos[ i ], target, allocationMode ); } return target; }
public Dictionary<string, GATData> LoadSamplesNamed( List< string > sampleNames, GATDataAllocationMode allocationMode ) { int i; GATSampleInfo info; Dictionary< string, GATData > target = new Dictionary<string, GATData>( sampleNames.Count ); for( i = 0; i < _sampleInfos.Count; i++ ) { info = _sampleInfos[ i ]; if( sampleNames.Contains( info.Name ) ) { LoadSample( info, target, allocationMode ); } } return target; }
/// <summary> /// Convenience method for loading files asynchronously /// to a sample bank. All wav and ogg files of the specified folder /// will be loaded. /// This method instantiates, configures and enqueues a AGATLoadingOperation. /// Use the NewOperation method if you need more control over progress callbacks. /// </summary> public void LoadFolderToSampleBank( string folderPath, PathRelativeType pathType, GATSampleBank targetBank, GATDataAllocationMode allocationMode, OperationCompletedHandler onOperationCompleted, bool forceMono = false ) { folderPath = GATPathsHelper.GetAbsolutePath( folderPath, pathType, false ); if( Directory.Exists( folderPath ) == false ) { throw new GATException( "No such directory!" ); } string[] files = Directory.GetFiles( folderPath ); if( files.Length == 0 ) { #if UNITY_EDITOR || GAT_DEBUG Debug.LogError( "Directory exists but is empty" ); #endif return; } LoadFilesToSampleBank( files, PathRelativeType.Absolute, targetBank, allocationMode, onOperationCompleted, forceMono ); }
void LoadSample( GATSampleInfo info, Dictionary< string, GATData > target, GATDataAllocationMode allocationMode ) { if( info.IsStreamingAsset ) { #if UNITY_EDITOR if( Application.isPlaying == false ) { LoadSampleFromResources( GATDataAllocationMode.Unmanaged, info, target ); return; } #endif LoadSampleFromStreamingAssets( allocationMode, info, target ); } else { LoadSampleFromResources( allocationMode, info, target ); } }
public LoadingOperation( GATDataAllocationMode allocationMode, int numFiles, FileLoadedHandler handler, bool forceMono = false ) { _allocationMode = allocationMode; _paths = new Queue< string >( numFiles ); Status = LoadOperationStatus.Configuring; OnFileWasLoaded = handler; _forceMono = forceMono; }
/// <summary> /// Convenience method for loading files asynchronously /// to a sample bank. This method instantiates, configures and /// enqueues a AGATLoadingOperation. Use NewOperation if you need /// more control over progress callbacks. /// </summary> public void LoadFilesToSampleBank(string[] filePaths, PathRelativeType pathType, GATSampleBank targetBank, GATDataAllocationMode allocationMode, OperationCompletedHandler onOperationCompleted, bool forceMono = false) { AGATLoadingOperation operation = new LoadingOperation(allocationMode, filePaths.Length, targetBank.AddLoadedFile, forceMono); int i; for (i = 0; i < filePaths.Length; i++) { operation.AddFile(filePaths[i], pathType); } operation.OnOperationCompleted = onOperationCompleted; EnqueueOperation(operation); }