This repository was archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathAppDelegate.cs
144 lines (128 loc) · 3.63 KB
/
AppDelegate.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using Foundation;
using UIKit;
using AudioToolbox;
using AVFoundation;
namespace tone {
[Register ("AppDelegate")]
unsafe public partial class AppDelegate : UIApplicationDelegate {
double sampleRate;
const int numBuffers = 3;
bool alternate = false;
NSError error;
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
//
// Setup audio system
//
var session = AVAudioSession.SharedInstance ();
session.SetCategory (new NSString ("AVAudioSessionCategoryPlayback"), AVAudioSessionCategoryOptions.DefaultToSpeaker, out error);
if (error != null) {
Console.WriteLine (error);
}
//
// Format description, we generate LinearPCM as short integers
//
sampleRate = session.SampleRate;
var format = new AudioStreamBasicDescription {
SampleRate = sampleRate,
Format = AudioFormatType.LinearPCM,
FormatFlags = AudioFormatFlags.LinearPCMIsSignedInteger | AudioFormatFlags.LinearPCMIsPacked,
BitsPerChannel = 16,
ChannelsPerFrame = 1,
BytesPerFrame = 2,
BytesPerPacket = 2,
FramesPerPacket = 1,
};
//
// Create an output queue
//
var queue = new OutputAudioQueue (format);
var bufferByteSize = (sampleRate > 16000) ? 2176 : 512; // 40.5 Hz : 31.25 Hz
//
// Create three buffers, generate a tone, and output the tones
//
var buffers = new AudioQueueBuffer* [numBuffers];
for (int i = 0; i < numBuffers; i++) {
queue.AllocateBuffer (bufferByteSize, out buffers [i]);
GenerateTone (buffers [i]);
queue.EnqueueBuffer (buffers [i], null);
}
//
// Output callback: invoked when the audio system is done with the
// buffer, this implementation merely recycles it.
//
queue.BufferCompleted += (object sender, BufferCompletedEventArgs e) => {
if (alternate) {
outputWaveForm += 1;
if (outputWaveForm > WaveForm.Square)
outputWaveForm = WaveForm.Sine;
GenerateTone (e.UnsafeBuffer);
}
queue.EnqueueBuffer (e.UnsafeBuffer, null);
};
queue.Start ();
return true;
}
// Configuration options for the audio output
const float outputFrequency = 220;
enum WaveForm {
Sine,
Triangle,
Sawtooth,
Square
}
WaveForm outputWaveForm;
//
// Simple tone generator
//
void GenerateTone (AudioQueueBuffer* buffer)
{
// Make the buffer length a multiple of the wavelength for the output frequency.
uint sampleCount = buffer->AudioDataBytesCapacity / 2;
double bufferLength = sampleCount;
double wavelength = sampleRate / outputFrequency;
double repetitions = Math.Floor (bufferLength / wavelength);
if (repetitions > 0)
sampleCount = (uint) Math.Round (wavelength * repetitions);
double x, y;
double sd = 1.0 / sampleRate;
double amp = 0.9;
double max16bit = Int16.MaxValue;
int i;
short* p = (short*) buffer->AudioData;
for (i = 0; i < sampleCount; i++) {
x = i * sd * outputFrequency;
switch (outputWaveForm) {
case WaveForm.Sine:
y = Math.Sin (x * 2.0 * Math.PI);
break;
case WaveForm.Triangle:
x = x % 1.0;
if (x < 0.25)
y = x * 4.0; // up 0.0 to 1.0
else if (x < 0.75)
y = (1.0 - x) * 4.0 - 2.0; // down 1.0 to -1.0
else
y = (x - 1.0) * 4.0; // up -1.0 to 0.0
break;
case WaveForm.Sawtooth:
y = 0.8 - (x % 1.0) * 1.8;
break;
case WaveForm.Square:
y = ((x % 1.0) < 0.5) ? 0.7 : -0.7;
break;
default:
y = 0;
break;
}
p [i] = (short) (y * max16bit * amp);
}
buffer->AudioDataByteSize = sampleCount * 2;
}
static void Main (string [] args)
{
UIApplication.Main (args, null, "AppDelegate");
}
}
}