PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/AVFoundation/AVAudioRecorder.cs

https://github.com/kjpou1/maccore
C# | 133 lines | 94 code | 16 blank | 23 comment | 24 complexity | db347ac24ad555ef521f3eaf785583e3 MD5 | raw file
Possible License(s): Apache-2.0
  1. // Copyright 2009, Novell, Inc.
  2. // Copyright 2010, Novell, Inc.
  3. // Copyright 2011 Xamarin Inc
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining
  6. // a copy of this software and associated documentation files (the
  7. // "Software"), to deal in the Software without restriction, including
  8. // without limitation the rights to use, copy, modify, merge, publish,
  9. // distribute, sublicense, and/or sell copies of the Software, and to
  10. // permit persons to whom the Software is furnished to do so, subject to
  11. // the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be
  14. // included in all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. //
  24. using MonoMac.Foundation;
  25. using MonoMac.CoreFoundation;
  26. using MonoMac.AudioToolbox;
  27. using MonoMac.ObjCRuntime;
  28. using System;
  29. namespace MonoMac.AVFoundation {
  30. [Advice ("Use AudioSettings instead")]
  31. public class AVAudioRecorderSettings {
  32. public AVAudioRecorderSettings ()
  33. {
  34. }
  35. public AudioFormatType AudioFormat = AudioFormatType.LinearPCM;
  36. public float SampleRate = 44100f;
  37. public int NumberChannels = 1;
  38. public int LinearPcmBitDepth;
  39. public bool LinearPcmBigEndian;
  40. public bool LinearPcmFloat = false;
  41. public bool LinearPcmNonInterleaved;
  42. public AVAudioQuality AudioQuality = AVAudioQuality.High;
  43. public AVAudioQuality? SampleRateConverterAudioQuality;
  44. public int? EncoderBitRate;
  45. public int? EncoderBitRatePerChannel;
  46. public int? EncoderBitDepthHint;
  47. internal NSMutableDictionary ToDictionary ()
  48. {
  49. var dict = new NSMutableDictionary ();
  50. dict.SetObject (new NSNumber ((int) AudioFormat), AVAudioSettings.AVFormatIDKey);
  51. dict.SetObject (new NSNumber (SampleRate), AVAudioSettings.AVSampleRateKey);
  52. dict.SetObject (new NSNumber (NumberChannels), AVAudioSettings.AVNumberOfChannelsKey);
  53. if (AudioFormat == AudioFormatType.LinearPCM){
  54. IntPtr thandle = CFBoolean.True.Handle;
  55. IntPtr fhandle = CFBoolean.False.Handle;
  56. if (LinearPcmBitDepth != 0){
  57. if (LinearPcmBitDepth == 8 || LinearPcmBitDepth == 16 || LinearPcmBitDepth == 32 || LinearPcmBitDepth == 24)
  58. dict.SetObject (new NSNumber (LinearPcmBitDepth), AVAudioSettings.AVLinearPCMBitDepthKey);
  59. else
  60. throw new Exception ("Invalid value for LinearPcmBitDepth, must be one of 8, 16, 24 or 32");
  61. }
  62. dict.LowlevelSetObject (LinearPcmBigEndian ? thandle : fhandle, AVAudioSettings.AVLinearPCMIsBigEndianKey.Handle);
  63. dict.LowlevelSetObject (LinearPcmFloat ? thandle : fhandle, AVAudioSettings.AVLinearPCMIsFloatKey.Handle);
  64. dict.LowlevelSetObject (LinearPcmNonInterleaved ? thandle : fhandle, AVAudioSettings.AVLinearPCMIsNonInterleaved.Handle);
  65. }
  66. dict.SetObject (new NSNumber ((int) AudioQuality), AVAudioSettings.AVEncoderAudioQualityKey);
  67. if (EncoderBitRate.HasValue)
  68. dict.SetObject (new NSNumber ((int) EncoderBitRate.Value), AVAudioSettings.AVEncoderBitRateKey);
  69. if (EncoderBitRatePerChannel.HasValue)
  70. dict.SetObject (new NSNumber ((int) EncoderBitRatePerChannel.Value), AVAudioSettings.AVEncoderBitRatePerChannelKey);
  71. if (EncoderBitDepthHint.HasValue){
  72. var n = EncoderBitDepthHint.Value;
  73. if (n < 8 || n > 32)
  74. throw new Exception ("EncoderBitDepthHint should be a value between 8 and 32");
  75. dict.SetObject (new NSNumber ((int) EncoderBitDepthHint.Value), AVAudioSettings.AVEncoderBitDepthHintKey);
  76. }
  77. if (SampleRateConverterAudioQuality.HasValue)
  78. dict.SetObject (new NSNumber ((int) SampleRateConverterAudioQuality.Value), AVAudioSettings.AVSampleRateConverterAudioQualityKey);
  79. return dict;
  80. }
  81. }
  82. public partial class AVAudioRecorder {
  83. [Obsolete ("Use static Create method as this method had an invalid signature up to MonoMac 1.4.4", true)]
  84. public AVAudioRecorder (NSUrl url, NSDictionary settings, NSError outError)
  85. {
  86. throw new Exception ("This constructor is no longer supported, use the AVAudioRecorder.ToUrl factory method instead");
  87. }
  88. public static AVAudioRecorder Create (NSUrl url, AudioSettings settings, out NSError error)
  89. {
  90. if (settings == null)
  91. throw new ArgumentNullException ("settings");
  92. return ToUrl (url, settings.Dictionary, out error);
  93. }
  94. [Advice ("Use Create method")]
  95. public static AVAudioRecorder ToUrl (NSUrl url, AVAudioRecorderSettings settings, out NSError error)
  96. {
  97. if (settings == null)
  98. throw new ArgumentNullException ("settings");
  99. return ToUrl (url, settings.ToDictionary (), out error);
  100. }
  101. [Advice ("Use Create method")]
  102. public static AVAudioRecorder ToUrl (NSUrl url, NSDictionary settings, out NSError error)
  103. {
  104. unsafe {
  105. IntPtr errhandle;
  106. IntPtr ptrtohandle = (IntPtr) (&errhandle);
  107. var ap = new AVAudioRecorder (url, settings, ptrtohandle);
  108. if (ap.Handle == IntPtr.Zero){
  109. error = (NSError) Runtime.GetNSObject (errhandle);
  110. return null;
  111. } else
  112. error = null;
  113. return ap;
  114. }
  115. }
  116. }
  117. }