void PrepareExtAudioFile()
        {
            extAudioFile = ExtAudioFile.OpenUrl(url);
            CheckValue(extAudioFile, "ExtAudioFile.OpenUrl failed");

            srcFormat = extAudioFile.FileDataFormat;

            // This is how you say,“When you convert the data, this is the format I’d like to receive.”
            // The client data format must be PCM. In other words, you can’t use a single ExtAudioFile to convert between two compressed formats.
            extAudioFile.ClientDataFormat = dstFormat;

            // getting total frame
            TotalFrames = extAudioFile.FileLengthFrames;

            // Allocating AudioBufferList
            buffer = new AudioBuffers(srcFormat.ChannelsPerFrame);
            for (int i = 0; i < buffer.Count; ++i)
            {
                int size = (int)(sizeof(int) * TotalFrames);
                buffer.SetData(i, Marshal.AllocHGlobal(size), size);
            }
            numberOfChannels = srcFormat.ChannelsPerFrame;

            // Reading all frame into the buffer
            ExtAudioFileError status;

            extAudioFile.Read((uint)TotalFrames, buffer, out status);
            if (status != ExtAudioFileError.OK)
            {
                throw new ApplicationException();
            }
        }
Esempio n. 2
0
        public static ExtAudioFile GetExtAudioFile(NSUrl url, out AudioStreamBasicDescription audioDescription)
        {
            // Notice the following line that we can not pass a NSUrl to a CFUrl
            //ExtAudioFile ext = ExtAudioFile.OpenUrl(url);

            // Basic Descriptions
            AudioStreamBasicDescription fileFormat;
            AudioStreamBasicDescription outputFormat;

            // So now we create a CFUrl
            CFUrl curl = CFUrl.FromFile(url.Path);

            // Open the file
            ExtAudioFile ext = ExtAudioFile.OpenUrl(curl);

            // Get the audio format
            fileFormat = ext.FileDataFormat;

            // Don't know how to handle sounds with more than 2 channels (i.e. stereo)
            // Remember that OpenAL sound effects must be mono to be spatialized anyway.
            if (fileFormat.ChannelsPerFrame > 2)
            {
#if DEBUG
                Console.WriteLine("Unsupported Format: Channel count [0] is greater than stereo.", fileFormat.ChannelsPerFrame);
#endif
                audioDescription = new AudioStreamBasicDescription();
                return(null);
            }

            // The output format must be linear PCM because that's the only type OpenAL knows how to deal with.
            // Set the client format to 16 bit signed integer (native-endian) data because that is the most
            // optimal format on iPhone/iPod Touch hardware.
            // Maintain the channel count and sample rate of the original source format.
            outputFormat                  = new AudioStreamBasicDescription();  // Create our output format description to be converted to
            outputFormat.SampleRate       = fileFormat.SampleRate;              // Preserve the original sample rate
            outputFormat.ChannelsPerFrame = fileFormat.ChannelsPerFrame;        // Preserve the orignal number of channels
            outputFormat.Format           = AudioFormatType.LinearPCM;          // We want Linear PCM

            // IsBigEndian is causing some problems with distorted sounds on MacOSX
//			outputFormat.FormatFlags = AudioFormatFlags.IsBigEndian
//							| AudioFormatFlags.IsPacked
//							| AudioFormatFlags.IsSignedInteger;

            outputFormat.FormatFlags = AudioFormatFlags.IsPacked
                                       | AudioFormatFlags.IsSignedInteger;
            outputFormat.FramesPerPacket = 1;                                 // We know for linear PCM, the definition is 1 frame per packet
            outputFormat.BitsPerChannel  = 16;                                // We know we want 16-bit
            outputFormat.BytesPerPacket  = 2 * outputFormat.ChannelsPerFrame; // We know we are using 16-bit, so 2-bytes per channel per frame
            outputFormat.BytesPerFrame   = 2 * outputFormat.ChannelsPerFrame; // For PCM, since 1 frame is 1 packet, it is the same as mBytesPerPacket

            // Set the desired client (output) data format
            ext.ClientDataFormat = outputFormat;

            // Copy the output format to the audio description that was passed in so the
            // info will be returned to the user.
            audioDescription = outputFormat;

            return(ext);
        }
Esempio n. 3
0
        public void ClientDataFormat()
        {
            var path = Path.GetFullPath(Path.Combine("AudioToolbox", "1.caf"));

            using (var file = ExtAudioFile.OpenUrl(CFUrl.FromFile(path))) {
                var fmt = file.ClientDataFormat;
            }
        }
Esempio n. 4
0
        public void ClientDataFormat()
        {
            var path = NSBundle.MainBundle.PathForResource("1", "caf", "AudioToolbox");

            using (var file = ExtAudioFile.OpenUrl(CFUrl.FromFile(path))) {
                var fmt = file.ClientDataFormat;
            }
        }
        protected void LoadAudioFile(StreamInfoProvider info)
        {
            // get the path to the file
            string path;

            if (info.IsInternal)
            {
                path = NSBundle.MainBundle.PathForSoundResource(info.Uri);
            }
            else
            {
                // file path is the Uri for user sources
                path = info.Uri;
            }

            using (var url = CFUrl.FromFile(path))
            {
                using (var file = ExtAudioFile.OpenUrl(url))
                {
                    var clientFormat = file.FileDataFormat;
                    clientFormat.FormatFlags       = AudioStreamBasicDescription.AudioFormatFlagsNativeFloat;
                    clientFormat.ChannelsPerFrame  = 1;
                    clientFormat.FramesPerPacket   = 1;
                    clientFormat.BitsPerChannel    = 8 * sizeof(float);
                    clientFormat.BytesPerPacket    =
                        clientFormat.BytesPerFrame = clientFormat.ChannelsPerFrame * sizeof(float);

                    file.ClientDataFormat = clientFormat;

                    double rateRatio = Metronome.SampleRate / clientFormat.SampleRate;

                    var numFrames = file.FileLengthFrames;
                    numFrames = (uint)(numFrames * rateRatio);

                    TotalFrames = numFrames;

                    UInt32 samples  = (uint)(numFrames * clientFormat.ChannelsPerFrame);
                    var    dataSize = (int)(sizeof(uint) * samples);
                    Data = Marshal.AllocHGlobal(dataSize);

                    // set up a AudioBufferList to read data into
                    var bufList = new AudioBuffers(1);
                    bufList[0] = new AudioBuffer
                    {
                        NumberChannels = 1,
                        Data           = Data,
                        DataByteSize   = dataSize
                    };

                    ExtAudioFileError error;
                    file.Read((uint)numFrames, bufList, out error);
                    if (error != ExtAudioFileError.OK)
                    {
                        throw new ApplicationException();
                    }
                }
            }
        }
Esempio n. 6
0
        public void OpenCFUrlTest()
        {
            var path = Path.GetFullPath(Path.Combine("AudioToolbox", "1.caf"));
            ExtAudioFileError err;

            using (var file = ExtAudioFile.OpenUrl(CFUrl.FromFile(path), out err)) {
                Assert.IsTrue(err == ExtAudioFileError.OK, "OpenCFUrlTest");
                Assert.IsNotNull(file.AudioFile, "OpenCFUrlTest");
            }
        }
Esempio n. 7
0
        public void OpenCFUrlTest()
        {
            var path = NSBundle.MainBundle.PathForResource("1", "caf", "AudioToolbox");
            ExtAudioFileError err;

            using (var file = ExtAudioFile.OpenUrl(CFUrl.FromFile(path), out err)) {
                Assert.IsTrue(err == ExtAudioFileError.OK, "OpenCFUrlTest");
                Assert.IsNotNull(file.AudioFile, "OpenCFUrlTest");
            }
        }
Esempio n. 8
0
        public void WrapAudioFileID()
        {
            var path = Path.GetFullPath(Path.Combine("AudioToolbox", "1.caf"));

            using (var file = ExtAudioFile.OpenUrl(CFUrl.FromFile(path))) {
                Assert.IsNotNull(file.AudioFile, "#1");

                ExtAudioFile f2;
                Assert.AreEqual(ExtAudioFileError.OK, ExtAudioFile.WrapAudioFileID(file.AudioFile.Value, true, out f2));
            }
        }
Esempio n. 9
0
        public void WrapAudioFileID()
        {
            var path = NSBundle.MainBundle.PathForResource("1", "caf", "AudioToolbox");

            using (var file = ExtAudioFile.OpenUrl(CFUrl.FromFile(path))) {
                Assert.IsNotNull(file.AudioFile, "#1");

                ExtAudioFile f2;
                Assert.AreEqual(ExtAudioFileError.OK, ExtAudioFile.WrapAudioFileID(file.AudioFile.Value, true, out f2));
            }
        }
Esempio n. 10
0
        public void ClientDataFormat()
        {
#if MONOMAC
            var path = NSBundle.MainBundle.PathForResource("1", "caf", "AudioToolbox");
#else
            var path = Path.GetFullPath(Path.Combine("AudioToolbox", "1.caf"));
#endif
            using (var file = ExtAudioFile.OpenUrl(CFUrl.FromFile(path))) {
                var fmt = file.ClientDataFormat;
            }
        }
Esempio n. 11
0
        public void OpenCFUrlTest()
        {
#if MONOMAC
            var path = NSBundle.MainBundle.PathForResource("1", "caf", "AudioToolbox");
#else
            var path = Path.GetFullPath(Path.Combine("AudioToolbox", "1.caf"));
#endif
            ExtAudioFileError err;
            using (var file = ExtAudioFile.OpenUrl(CFUrl.FromFile(path), out err)) {
                Assert.IsTrue(err == ExtAudioFileError.OK, "OpenCFUrlTest");
                Assert.IsNotNull(file.AudioFile, "OpenCFUrlTest");
            }
        }
Esempio n. 12
0
        // load up audio data from the demo files into mSoundBuffer.data used in the render proc
        void LoadFiles()
        {
            const int FilesCount = 2;

            for (int i = 0; i < FilesCount; i++)
            {
                Debug.Print("Loading file #{0}", i);

                using (var file = ExtAudioFile.OpenUrl(sourceURL [i])) {
                    var clientFormat = file.FileDataFormat;
                    clientFormat.FormatFlags       = AudioStreamBasicDescription.AudioFormatFlagsNativeFloat;
                    clientFormat.ChannelsPerFrame  = 1;
                    clientFormat.FramesPerPacket   = 1;
                    clientFormat.BitsPerChannel    = 8 * sizeof(int);
                    clientFormat.BytesPerPacket    =
                        clientFormat.BytesPerFrame = clientFormat.ChannelsPerFrame * sizeof(int);

                    file.ClientDataFormat = clientFormat;

                    // set the client format to be what we want back
                    double rateRatio = GraphSampleRate / clientFormat.SampleRate;

                    var numFrames = file.FileLengthFrames;
                    numFrames = (uint)(numFrames * rateRatio);                     // account for any sample rate conversion
                    Debug.Print("Number of Sample Frames after rate conversion (if any): {0}", numFrames);

                    // set up our buffer
                    soundBuffer[i].TotalFrames = numFrames;

                    UInt32 samples   = (uint)(numFrames * clientFormat.ChannelsPerFrame);
                    var    data_size = (int)(sizeof(uint) * samples);
                    soundBuffer[i].Data = Marshal.AllocHGlobal(data_size);

                    // set up a AudioBufferList to read data into
                    var bufList = new AudioBuffers(1);
                    bufList [0] = new AudioBuffer {
                        NumberChannels = 1,
                        Data           = soundBuffer [i].Data,
                        DataByteSize   = data_size
                    };

                    ExtAudioFileError error;
                    file.Read((uint)numFrames, bufList, out error);
                    if (error != ExtAudioFileError.OK)
                    {
                        throw new ApplicationException();
                    }
                }
            }
        }
Esempio n. 13
0
        public void WrapAudioFileID()
        {
#if MONOMAC
            var path = NSBundle.MainBundle.PathForResource("1", "caf", "AudioToolbox");
#else
            var path = Path.GetFullPath(Path.Combine("AudioToolbox", "1.caf"));
#endif
            using (var file = ExtAudioFile.OpenUrl(CFUrl.FromFile(path))) {
                Assert.IsNotNull(file.AudioFile, "#1");

                ExtAudioFile f2;
                Assert.AreEqual(ExtAudioFileError.OK, ExtAudioFile.WrapAudioFileID(file.AudioFile.Value, true, out f2));
            }
        }
Esempio n. 14
0
        void prepareExtAudioFile()
        {
            // Opening Audio File
            _extAudioFile = ExtAudioFile.OpenUrl(_url);

            // Getting file data format
            _srcFormat = _extAudioFile.FileDataFormat;

            // Setting the channel number of the output format same to the input format
            _dstFormat              = AudioStreamBasicDescription.CreateLinearPCM(channelsPerFrame: (uint)_srcFormat.ChannelsPerFrame, bitsPerChannel: 32);
            _dstFormat.FormatFlags |= AudioFormatFlags.IsNonInterleaved;

            // setting reading format as audio unit cannonical format
            _extAudioFile.ClientDataFormat = _dstFormat;

            // getting total frame
            _totalFrames = _extAudioFile.FileLengthFrames;

            // Allocating AudioBufferList
            _buffer = new AudioBuffers(_srcFormat.ChannelsPerFrame);
            for (int i = 0; i < _buffer.Count; ++i)
            {
                int size = (int)(sizeof(uint) * _totalFrames);
                _buffer.SetData(i, Marshal.AllocHGlobal(size), size);
            }
            _numberOfChannels = _srcFormat.ChannelsPerFrame;

            // Reading all frame into the buffer
            ExtAudioFileError status;

            _extAudioFile.Read((uint)_totalFrames, _buffer, out status);
            if (status != ExtAudioFileError.OK)
            {
                throw new ApplicationException();
            }
        }