~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/maccore/src/AudioToolbox/AudioFileStream.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
//    Miguel de Icaza (miguel@novell.com)
6
6
//     
7
7
// Copyright 2009 Novell, Inc
 
8
// Copyright 2011, 2012 Xamarin Inc.
8
9
//
9
10
// Permission is hereby granted, free of charge, to any person obtaining
10
11
// a copy of this software and associated documentation files (the
31
32
using System.Collections.Generic;
32
33
using System.Runtime.InteropServices;
33
34
using MonoMac.CoreFoundation;
 
35
using MonoMac.Foundation;
34
36
 
35
37
using OSStatus = System.Int32;
36
38
using AudioFileStreamID = System.IntPtr;
164
166
                        AudioFileType fileTypeHint,
165
167
                        out IntPtr file_id);
166
168
 
167
 
                static AudioFileStream_PacketsProc dInPackets;
168
 
                static AudioFileStream_PropertyListenerProc dPropertyListener;
169
 
 
170
 
                static AudioFileStream ()
171
 
                {
172
 
                        dInPackets = InPackets;
173
 
                        dPropertyListener = PropertyListener;
174
 
                }
 
169
                static readonly AudioFileStream_PacketsProc dInPackets = InPackets;
 
170
                static readonly AudioFileStream_PropertyListenerProc dPropertyListener = PropertyListener;
175
171
 
176
172
                [MonoPInvokeCallback(typeof(AudioFileStream_PacketsProc))]
177
173
                static void InPackets (IntPtr clientData, int numberBytes, int numberPackets, IntPtr inputData, IntPtr packetDescriptions)
232
228
                
233
229
                public AudioFileStreamStatus ParseBytes (int size, IntPtr data, bool discontinuity)
234
230
                {
235
 
                        return AudioFileStreamParseBytes (handle, size, data, discontinuity ? (uint) 1 : (uint) 0);
 
231
                        return LastError = AudioFileStreamParseBytes (handle, size, data, discontinuity ? (uint) 1 : (uint) 0);
236
232
                }
237
233
 
238
234
                public AudioFileStreamStatus ParseBytes (byte [] bytes, bool discontinuity)
241
237
                                throw new ArgumentNullException ("bytes");
242
238
                        unsafe {
243
239
                                fixed (byte *bp = &bytes[0]){
244
 
                                        return AudioFileStreamParseBytes (handle, bytes.Length, (IntPtr) bp, discontinuity ? (uint) 1 : (uint) 0);
 
240
                                        return LastError = AudioFileStreamParseBytes (handle, bytes.Length, (IntPtr) bp, discontinuity ? (uint) 1 : (uint) 0);
245
241
                                }
246
242
                        }
247
243
                }
259
255
                        
260
256
                        unsafe {
261
257
                                fixed (byte *bp = &bytes[0]){
262
 
                                        return AudioFileStreamParseBytes (handle, count, (IntPtr) (bp + offset) , discontinuity ? (uint) 1 : (uint) 0);
 
258
                                        return LastError = AudioFileStreamParseBytes (handle, count, (IntPtr) (bp + offset) , discontinuity ? (uint) 1 : (uint) 0);
263
259
                                }
264
260
                        }
265
261
                }
273
269
                public AudioFileStreamStatus Seek (long packetOffset, out long dataByteOffset, out bool isEstimate)
274
270
                {
275
271
                        int v = 0;
276
 
                        var code = AudioFileStreamSeek (handle, packetOffset, out dataByteOffset, ref v);
277
 
                        if (code != AudioFileStreamStatus.Ok){
 
272
                        LastError = AudioFileStreamSeek (handle, packetOffset, out dataByteOffset, ref v);
 
273
                        if (LastError != AudioFileStreamStatus.Ok){
278
274
                                isEstimate = false;
279
 
                                return code;
 
275
                        } else {
 
276
                                isEstimate = (v & 1) == 1;
280
277
                        }
281
 
                        isEstimate = (v & 1) == 1;
282
 
                        return code;
 
278
 
 
279
                        return LastError;
283
280
                }
284
281
                
285
282
                [DllImport (Constants.AudioToolboxLibrary)]
286
 
                extern static OSStatus AudioFileStreamGetPropertyInfo(  
 
283
                extern static AudioFileStreamStatus AudioFileStreamGetPropertyInfo(     
287
284
                        AudioFileStreamID inAudioFileStream,
288
285
                        AudioFileStreamProperty inPropertyID,
289
286
                        out int outPropertyDataSize,
290
287
                        out int isWritable);
291
288
                
292
289
                [DllImport (Constants.AudioToolboxLibrary)]
293
 
                extern static OSStatus AudioFileStreamGetProperty(      
 
290
                extern static AudioFileStreamStatus AudioFileStreamGetProperty( 
294
291
                        AudioFileStreamID inAudioFileStream,
295
292
                        AudioFileStreamProperty inPropertyID,
296
293
                        ref int ioPropertyDataSize,
305
302
                {
306
303
                        int writable;
307
304
 
308
 
                        var r = AudioFileStreamGetPropertyInfo (handle, property, out size, out writable);
309
 
                        if (r != 0)
 
305
                        LastError = AudioFileStreamGetPropertyInfo (handle, property, out size, out writable);
 
306
                        if (LastError != 0)
310
307
                                return IntPtr.Zero;
311
308
 
312
309
                        var buffer = Marshal.AllocHGlobal (size);
313
310
                        if (buffer == IntPtr.Zero)
314
311
                                return IntPtr.Zero;
315
312
 
316
 
                        r = AudioFileStreamGetProperty (handle, property, ref size, buffer);
317
 
                        if (r == 0)
 
313
                        LastError = AudioFileStreamGetProperty (handle, property, ref size, buffer);
 
314
                        if (LastError == 0)
318
315
                                return buffer;
319
316
                        Marshal.FreeHGlobal (buffer);
320
317
                        return IntPtr.Zero;
325
322
                        unsafe {
326
323
                                int val = 0;
327
324
                                int size = 4;
328
 
                                if (AudioFileStreamGetProperty (handle, property, ref size, (IntPtr) (&val)) == 0)
 
325
                                LastError = AudioFileStreamGetProperty (handle, property, ref size, (IntPtr) (&val));
 
326
                                if (LastError == 0)
329
327
                                        return val;
330
328
                                return 0;
331
329
                        }
336
334
                        unsafe {
337
335
                                double val = 0;
338
336
                                int size = 8;
339
 
                                if (AudioFileStreamGetProperty (handle, property, ref size, (IntPtr) (&val)) == 0)
 
337
                                LastError = AudioFileStreamGetProperty (handle, property, ref size, (IntPtr) (&val));
 
338
                                if (LastError == 0)
340
339
                                        return val;
341
340
                                return 0;
342
341
                        }
347
346
                        unsafe {
348
347
                                long val = 0;
349
348
                                int size = 8;
350
 
                                if (AudioFileStreamGetProperty (handle, property, ref size, (IntPtr) (&val)) == 0)
 
349
                                LastError = AudioFileStreamGetProperty (handle, property, ref size, (IntPtr) (&val));
 
350
                                if (LastError == 0)
351
351
                                        return val;
352
352
                                return 0;
353
353
                        }
354
354
                }
355
355
 
356
 
                T GetProperty<T> (AudioFileStreamProperty property)
 
356
                unsafe T? GetProperty<T> (AudioFileStreamProperty property) where T : struct
357
357
                {
358
358
                        int size, writable;
359
359
 
360
 
                        if (AudioFileStreamGetPropertyInfo (handle, property, out size, out writable) != 0)
361
 
                                return default (T);
 
360
                        LastError = AudioFileStreamGetPropertyInfo (handle, property, out size, out writable);
 
361
                        if (LastError != 0)
 
362
                                return null;
362
363
                        var buffer = Marshal.AllocHGlobal (size);
363
364
                        if (buffer == IntPtr.Zero)
364
 
                                return default(T);
 
365
                                return null;
365
366
                        try {
366
 
                                var r = AudioFileStreamGetProperty (handle, property, ref size, buffer);
367
 
                                if (r == 0)
368
 
                                        return (T) Marshal.PtrToStructure (buffer, typeof (T));
 
367
                                LastError = AudioFileStreamGetProperty (handle, property, ref size, buffer);
 
368
                                if (LastError == 0){
 
369
                                        T result = *(T*) buffer;
 
370
                                        return result;
 
371
                                }
369
372
 
370
 
                                return default(T);
 
373
                                return null;
371
374
                        } finally {
372
375
                                Marshal.FreeHGlobal (buffer);
373
376
                        }
374
377
                }
375
378
                
376
379
                [DllImport (Constants.AudioToolboxLibrary)]
377
 
                extern static OSStatus AudioFileStreamSetProperty(      
 
380
                extern static AudioFileStreamStatus AudioFileStreamSetProperty( 
378
381
                        AudioFileStreamID inAudioFileStream,
379
382
                        AudioFileStreamProperty inPropertyID,
380
383
                        int inPropertyDataSize,
382
385
 
383
386
                public bool SetProperty (AudioFileStreamProperty property, int dataSize, IntPtr propertyData)
384
387
                {
385
 
                        return AudioFileStreamSetProperty (handle, property, dataSize, propertyData) == 0;
 
388
                        LastError = AudioFileStreamSetProperty (handle, property, dataSize, propertyData);
 
389
                        return LastError == 0;
386
390
                }      
387
391
 
388
392
                [DllImport (Constants.AudioToolboxLibrary)]
389
 
                extern static OSStatus AudioFileStreamClose(AudioFileStreamID inAudioFileStream);
 
393
                extern static AudioFileStreamStatus AudioFileStreamClose(AudioFileStreamID inAudioFileStream);
390
394
 
391
395
                public bool ReadyToProducePackets {
392
396
                        get {
400
404
                        }
401
405
                }
402
406
 
 
407
                [Advice ("Use DataFormat")]
403
408
                public AudioStreamBasicDescription StreamBasicDescription {
404
409
                        get {
405
 
                                return GetProperty<AudioStreamBasicDescription> (AudioFileStreamProperty.DataFormat);
406
 
                        }
407
 
                }
408
 
 
409
 
                // TODO: FormatList
410
 
                // TODO: PacketTableInfo=0x706e666f,
 
410
                                return DataFormat;
 
411
                        }
 
412
                }
 
413
 
 
414
                public AudioStreamBasicDescription DataFormat {
 
415
                        get {
 
416
                                return GetProperty<AudioStreamBasicDescription> (AudioFileStreamProperty.DataFormat) ?? default (AudioStreamBasicDescription);
 
417
                        }
 
418
                }
 
419
 
 
420
                public unsafe AudioFormat [] FormatList {
 
421
                        get {
 
422
                                int size;
 
423
                                var r = GetProperty (AudioFileStreamProperty.FormatList, out size);
 
424
                                if (r == IntPtr.Zero)
 
425
                                        return null;
 
426
 
 
427
                                var records = (AudioFormat *) r;
 
428
                                int itemSize = sizeof (AudioFormat);
 
429
                                int items = size/itemSize;
 
430
                                var ret = new AudioFormat [items];
 
431
                                        
 
432
                                for (int i = 0; i < items; i++)
 
433
                                        ret [i] = records [i];
 
434
 
 
435
                                Marshal.FreeHGlobal (r);
 
436
                                return ret;
 
437
                        }
 
438
                }
 
439
 
 
440
                public AudioFilePacketTableInfo? PacketTableInfo {
 
441
                        get {
 
442
                                return GetProperty<AudioFilePacketTableInfo> (AudioFileStreamProperty.PacketTableInfo);
 
443
                        }
 
444
                }
411
445
 
412
446
                public byte [] MagicCookie {
413
447
                        get {
470
504
 
471
505
                        unsafe {
472
506
                                AudioFramePacketTranslation *p = &buffer;
473
 
                                int size = Marshal.SizeOf (buffer);
474
 
                                if (AudioFileStreamGetProperty (handle, AudioFileStreamProperty.PacketToFrame, ref size, (IntPtr) p) == 0)
 
507
                                int size = sizeof (AudioFramePacketTranslation);
 
508
                                LastError = AudioFileStreamGetProperty (handle, AudioFileStreamProperty.PacketToFrame, ref size, (IntPtr) p);
 
509
                                if (LastError == 0)
475
510
                                        return buffer.Frame;
476
511
                                return -1;
477
512
                        }
484
519
 
485
520
                        unsafe {
486
521
                                AudioFramePacketTranslation *p = &buffer;
487
 
                                int size = Marshal.SizeOf (buffer);
488
 
                                if (AudioFileStreamGetProperty (handle, AudioFileStreamProperty.FrameToPacket, ref size, (IntPtr) p) == 0){
 
522
                                int size = sizeof (AudioFramePacketTranslation);
 
523
                                LastError = AudioFileStreamGetProperty (handle, AudioFileStreamProperty.FrameToPacket, ref size, (IntPtr) p);
 
524
                                if (LastError == 0){
489
525
                                        frameOffsetInPacket = buffer.FrameOffsetInPacket;
490
526
                                        return buffer.Packet;
491
527
                                }
501
537
 
502
538
                        unsafe {
503
539
                                AudioBytePacketTranslation *p = &buffer;
504
 
                                int size = Marshal.SizeOf (buffer);
505
 
                                if (AudioFileStreamGetProperty (handle, AudioFileStreamProperty.PacketToByte, ref size, (IntPtr) p) == 0){
506
 
                                        isEstimate = (buffer.Flags & 1) == 1;
 
540
                                int size = sizeof (AudioBytePacketTranslation);
 
541
                                LastError = AudioFileStreamGetProperty (handle, AudioFileStreamProperty.PacketToByte, ref size, (IntPtr) p);
 
542
                                if (LastError == 0){
 
543
                                        isEstimate = (buffer.Flags & BytePacketTranslationFlags.IsEstimate) != 0;
507
544
                                        return buffer.Byte;
508
545
                                }
509
546
                                isEstimate = false;
518
555
 
519
556
                        unsafe {
520
557
                                AudioBytePacketTranslation *p = &buffer;
521
 
                                int size = Marshal.SizeOf (buffer);
522
 
                                if (AudioFileStreamGetProperty (handle, AudioFileStreamProperty.ByteToPacket, ref size, (IntPtr) p) == 0){
523
 
                                        isEstimate = (buffer.Flags & 1) == 1;
 
558
                                int size = sizeof (AudioBytePacketTranslation);
 
559
                                LastError = AudioFileStreamGetProperty (handle, AudioFileStreamProperty.ByteToPacket, ref size, (IntPtr) p);
 
560
                                if (LastError == 0){
 
561
                                        isEstimate = (buffer.Flags & BytePacketTranslationFlags.IsEstimate) != 0;
524
562
                                        byteOffsetInPacket = buffer.ByteOffsetInPacket;
525
563
                                        return buffer.Packet;
526
564
                                }
547
585
                                return GetDouble (AudioFileStreamProperty.AverageBytesPerPacket);
548
586
                        }
549
587
                }
 
588
 
 
589
                public AudioFileStreamStatus LastError { get; private set; }
550
590
        }
551
 
}
 
 
b'\\ No newline at end of file'
 
591
}