-
Notifications
You must be signed in to change notification settings - Fork 1
/
AudioEngine.cpp
executable file
·591 lines (462 loc) · 17.8 KB
/
AudioEngine.cpp
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
#include "AudioEngine.h"
#include <IOKit/IOLib.h>
#include <IOKit/IOFilterInterruptEventSource.h>
#include <IOKit/pci/IOPCIDevice.h>
#include "regs.h"
#include "misc.h"
#define INITIAL_SAMPLE_RATE 44100
#define FREQUENCIES 13
static const ULONG Frequencies[ FREQUENCIES ] =
{
8000,
9600,
11025,
12000,
16000,
22050,
24000,
32000,
44100, // CD
48000,
64000,
88200,
96000
};
static const ULONG FrequencyBits[ FREQUENCIES ] =
{
6,
3,
10,
2,
5,
9,
1,
4,
8,
0,
15,
11,
7
};
#define SPDIF_FREQUENCIES 7
static const ULONG SPDIF_Frequencies[ SPDIF_FREQUENCIES ] =
{
32000,
44100, // CD
48000,
88200,
96000,
176400,
192000
};
static const ULONG SPDIF_FrequencyBits[ SPDIF_FREQUENCIES ] =
{
3,
0,
2,
4,
5,
7,
6
};
#define super IOAudioEngine
OSDefineMetaClassAndStructors(Envy24AudioEngine, IOAudioEngine)
bool Envy24AudioEngine::init(struct CardData* i_card)
{
bool result = false;
DBGPRINT("Envy24AudioEngine[%p]::init(%p)\n", this, i_card);
if (!super::init(NULL)) {
goto Done;
}
if (!i_card) {
goto Done;
}
card = i_card;
result = true;
Done:
return result;
}
bool Envy24AudioEngine::initHardware(IOService *provider)
{
bool result = false;
IOAudioSampleRate initialSampleRate;
IOAudioStream *audioStream;
IOWorkLoop *workLoop;
DBGPRINT("Envy24AudioEngine[%p]::initHardware(%p)\n", this, provider);
if (!super::initHardware(provider)) {
goto Done;
}
// Setup the initial sample rate for the audio engine
initialSampleRate.whole = INITIAL_SAMPLE_RATE;
initialSampleRate.fraction = 0;
//setDescription("Envy24 Audio Engine");
setSampleRate(&initialSampleRate);
// Set the number of sample frames in each buffer
setNumSampleFramesPerBuffer(NUM_SAMPLE_FRAMES);
setSampleOffset(48);
workLoop = getWorkLoop();
if (!workLoop) {
IOLog("Couldn't get workloop!\n");
goto Done;
}
// Create an interrupt event source through which to receive interrupt callbacks
// In this case, we only want to do work at primary interrupt time, so
// we create an IOFilterInterruptEventSource which makes a filtering call
// from the primary interrupt who's purpose is to determine if
// our secondary interrupt handler is to be called. In our case, we
// can do the work in the filter routine and then return false to
// indicate that we do not want our secondary handler called
// Allocate our input and output buffers
mach_vm_address_t physicalMask;
physicalMask = 0x0FFFFFFCULL;
outputBuffer = (SInt32 *)IOMallocContiguousWithLimit(card->Specific.BufferSize, physicalMask, &physicalAddressOutput);
if (!outputBuffer) {
IOLog("Couldn't allocate memory for output buffer using IOMallocContiguousWithLimit(), trying IOMallocContiguous()!\n");
outputBuffer = (SInt32 *)IOMallocContiguous(card->Specific.BufferSize, 4, &physicalAddressOutput);
if (outputBuffer == NULL)
{
IOLog("Couldn't allocate memory for output buffer using malloc()!");
goto Done;
}
}
inputBuffer = (SInt32 *)IOMallocContiguousWithLimit(card->Specific.BufferSizeRec, physicalMask, &physicalAddressInput);
if (!inputBuffer) {
IOLog("Couldn't allocate memory for input buffer using IOMallocContiguousWithLimit(), trying IOMallocContiguous()!\n");
inputBuffer = (SInt32 *)IOMallocContiguous(card->Specific.BufferSizeRec, 4, &physicalAddressInput);
if (inputBuffer == NULL)
{
IOLog("Couldn't allocate memory for input buffer using malloc()!");
goto Done;
}
}
if (physicalAddressOutput > 0x0FFFFFFF)
{
IOLog("Address of output is above 256 MB!");
}
if (physicalAddressInput > 0x0FFFFFFF)
{
IOLog("Address of input is above 256 MB!");
}
card->pci_dev->ioWrite32(MT_DMA_PB_ADDRESS, physicalAddressOutput, card->mtbase);
card->pci_dev->ioWrite32(MT_DMA_REC_ADDRESS, physicalAddressInput, card->mtbase);
card->pci_dev->ioWrite8(MT_SAMPLERATE, 8, card->mtbase); // initialize to 44100 Hz
// Create an IOAudioStream for each buffer and add it to this audio engine
audioStream = createNewAudioStream(kIOAudioStreamDirectionOutput, outputBuffer, card->Specific.BufferSize, 0, 10);
if (!audioStream) {
goto Done;
}
addAudioStream(audioStream);
audioStream->release();
audioStream = createNewAudioStream(kIOAudioStreamDirectionInput, inputBuffer, card->Specific.BufferSizeRec, 1, 12);
if (!audioStream) {
goto Done;
}
addAudioStream(audioStream);
audioStream->release();
// the interruptEventSource needs to be enabled here, else IRQ sharing doesn't work
// In order to allow the interrupts to be received, the interrupt event source must be
// added to the IOWorkLoop
// Additionally, interrupts will not be firing until the interrupt event source is
// enabled by calling interruptEventSource->enable() - this probably doesn't need to
// be done until performAudioEngineStart() is called, and can probably be disabled
// when performAudioEngineStop() is called and the audio engine is no longer running
// Although this really depends on the specific hardware
interruptEventSource = IOFilterInterruptEventSource::filterInterruptEventSource(this,
Envy24AudioEngine::interruptHandler,
Envy24AudioEngine::interruptFilter,
audioDevice->getProvider());
if (!interruptEventSource) {
IOLog("Error: no interrupt event source!\n");
goto Done;
}
interruptEventSource->enable();
workLoop->addEventSource(interruptEventSource);
result = true;
Done:
if (result == false)
{
IOLog("Something failed in the engine!\n");
}
return result;
}
void Envy24AudioEngine::free()
{
DBGPRINT("Envy24AudioEngine[%p]::free()\n", this);
// We need to free our resources when we're going away
if (interruptEventSource) {
interruptEventSource->disable();
interruptEventSource->release();
interruptEventSource = NULL;
}
if (outputBuffer) {
IOFreeContiguous(outputBuffer, card->Specific.BufferSize);
outputBuffer = NULL;
}
if (inputBuffer) {
IOFreeContiguous(inputBuffer, card->Specific.BufferSizeRec);
inputBuffer = NULL;
}
super::free();
}
IOAudioStream *Envy24AudioEngine::createNewAudioStream(IOAudioStreamDirection direction,
void *sampleBuffer,
UInt32 sampleBufferSize,
UInt32 channel,
UInt32 channels)
{
IOAudioStream *audioStream;
// For this sample device, we are only creating a single format and allowing 44.1KHz and 48KHz
audioStream = new IOAudioStream;
if (audioStream) {
if (!audioStream->initWithAudioEngine(this, direction, 1)) {
IOLog("initWithAudioEngine failed\n");
IOSleep(3000);
audioStream->release();
} else {
IOAudioSampleRate rate;
IOAudioStreamFormat format = {
channels, // num channels
kIOAudioStreamSampleFormatLinearPCM, // sample format
kIOAudioStreamNumericRepresentationSignedInt, // numeric format
24, // bit depth
24, // bit width
kIOAudioStreamAlignmentHighByte, // high byte aligned - unused because bit depth == bit width
kIOAudioStreamByteOrderBigEndian, // little endian
true, // format is mixable
channel // number of channel
};
IOAudioStreamFormat format16 = {
channels, // num channels
kIOAudioStreamSampleFormatLinearPCM, // sample format
kIOAudioStreamNumericRepresentationSignedInt, // numeric format
16, // bit depth
16, // bit width
kIOAudioStreamAlignmentHighByte, // high byte aligned - unused because bit depth == bit width
kIOAudioStreamByteOrderBigEndian, // little endian
true, // format is mixable
channel // number of channel
};
// As part of creating a new IOAudioStream, its sample buffer needs to be set
// It will automatically create a mix buffer should it be needed
audioStream->setSampleBuffer(sampleBuffer, sampleBufferSize);
// This device only allows a single format and a choice of 2 different sample rates
rate.fraction = 0;
for (int i = 0; i < FREQUENCIES; i++)
{
rate.whole = Frequencies[i];
audioStream->addAvailableFormat(&format, &rate, &rate, NULL, 0);
audioStream->addAvailableFormat(&format16, &rate, &rate, NULL, 0);
}
// Finally, the IOAudioStream's current format needs to be indicated
audioStream->setFormat(&format, false);
}
}
else
{
IOLog("Couldn't allocate IOAudioStream\n");
IOSleep(3000);
}
return audioStream;
}
void Envy24AudioEngine::stop(IOService *provider)
{
DBGPRINT("Envy24AudioEngine[%p]::stop(%p)\n", this, provider);
// When our device is being stopped and torn down, we should go ahead and remove
// the interrupt event source from the IOWorkLoop
// Additionally, we'll go ahead and release the interrupt event source since it isn't
// needed any more
if (interruptEventSource) {
IOWorkLoop *wl;
wl = getWorkLoop();
if (wl) {
wl->removeEventSource(interruptEventSource);
}
interruptEventSource->release();
interruptEventSource = NULL;
}
// Add code to shut down hardware (beyond what is needed to simply stop the audio engine)
// There may be nothing needed here
super::stop(provider);
}
IOReturn Envy24AudioEngine::performAudioEngineStart()
{
DBGPRINT("Envy24AudioEngine[%p]::performAudioEngineStart()\n", this);
IOPCIDevice *dev = card->pci_dev;
ClearMask8(dev, card->mtbase, MT_DMA_CONTROL, MT_PLAY_START | MT_REC_START); // stop first
ClearMask8(dev, card->mtbase, MT_INTR_MASK_STATUS, MT_PLAY_MASK); // | MT_REC_MASK);
// Play
clearAllSampleBuffers();
UInt32 BufferSize32 = (card->Specific.BufferSize / 4) - 1;
UInt16 BufferSize16 = BufferSize32 & 0xFFFF;
dev->ioWrite16(MT_DMA_PB_LENGTH, BufferSize16, card->mtbase);
dev->ioWrite16(MT_DMA_PB_INTLEN, BufferSize16, card->mtbase);
//IOLog("Buffer size = %ld (%lx), BufferSize16 = %u\n", card->Specific.BufferSize, card->Specific.BufferSize, BufferSize16);
// REC
BufferSize16 = (card->Specific.BufferSizeRec / 4) - 1;
dev->ioWrite16(MT_DMA_REC_LENGTH, BufferSize16, card->mtbase);
dev->ioWrite16(MT_DMA_REC_INTLEN, BufferSize16, card->mtbase);
//dumpRegisters();
//IOLog("START\n");
// When performAudioEngineStart() gets called, the audio engine should be started from the beginning
// of the sample buffer. Because it is starting on the first sample, a new timestamp is needed
// to indicate when that sample is being read from/written to. The function takeTimeStamp()
// is provided to do that automatically with the current time.
// By default takeTimeStamp() will increment the current loop count in addition to taking the current
// timestamp. Since we are starting a new audio engine run, and not looping, we don't want the loop count
// to be incremented. To accomplish that, false is passed to takeTimeStamp().
takeTimeStamp(false);
// Add audio - I/O start code here
WriteMask8(card->pci_dev, card->mtbase, MT_DMA_CONTROL, MT_PLAY_START | MT_REC_START);
return kIOReturnSuccess;
}
IOReturn Envy24AudioEngine::performAudioEngineStop()
{
DBGPRINT("Envy24AudioEngine[%p]::performAudioEngineStop()\n", this);
// Add audio - I/O stop code here
ClearMask8(card->pci_dev, card->mtbase, MT_DMA_CONTROL, MT_PLAY_START | MT_REC_START);
WriteMask8(card->pci_dev, card->mtbase, MT_INTR_MASK_STATUS, MT_PLAY_MASK | MT_REC_MASK);
return kIOReturnSuccess;
}
UInt32 Envy24AudioEngine::getCurrentSampleFrame()
{
// In order for the erase process to run properly, this function must return the current location of
// the audio engine - basically a sample counter
// It doesn't need to be exact, but if it is inexact, it should err towards being before the current location
// rather than after the current location. The erase head will erase up to, but not including the sample
// frame returned by this function. If it is too large a value, sound data that hasn't been played will be
// erased.
// Change to return the real value
const UInt32 div = 10 * (32 / 8);
UInt32 current_address = card->pci_dev->ioRead32(MT_DMA_PB_ADDRESS, card->mtbase);
UInt32 diff = (current_address - ((UInt32) physicalAddressOutput)) / div;
//IOLog("diff = %lu\n", diff);
return diff;
}
IOReturn Envy24AudioEngine::performFormatChange(IOAudioStream *audioStream, const IOAudioStreamFormat *newFormat, const IOAudioSampleRate *newSampleRate)
{
DBGPRINT("Envy24AudioEngine[%p]::peformFormatChange(%p, %p, %p)\n", this, audioStream, newFormat, newSampleRate);
if (newSampleRate)
{
currentSampleRate = newSampleRate->whole;
}
else
{
currentSampleRate = 44100;
}
UInt32 FreqBits = lookUpFrequencyBits(currentSampleRate, Frequencies, FrequencyBits, FREQUENCIES, 0x08);
card->pci_dev->ioWrite8(MT_SAMPLERATE, FreqBits, card->mtbase);
IOLog("Freq = %x\n", FreqBits);
/*UInt32 SPDIFBits = lookUpFrequencyBits(currentSampleRate, SPDIF_Frequencies, SPDIF_FrequencyBits, SPDIF_FREQUENCIES, 1000);
ClearMask8(card->pci_dev, card->iobase, CCS_SPDIF_CONFIG, CCS_SPDIF_INTEGRATED);
if (SPDIFBits != 1000)
{
card->pci_dev->ioWrite16(MT_SPDIF_TRANSMIT, 0x04 | 1 << 5 | (SPDIFBits << 12), card->mtbase);
WriteMask8(card->pci_dev, card->iobase, CCS_SPDIF_CONFIG, CCS_SPDIF_INTEGRATED);
//IOLog("Enabled SPDIF %lu\n", SPDIFBits);
}
card->SPDIF_RateSupported = (SPDIFBits != 1000);*/
return kIOReturnSuccess;
}
void Envy24AudioEngine::interruptHandler(OSObject * owner, IOInterruptEventSource* source, int /*count*/)
{
}
bool Envy24AudioEngine::interruptFilter(OSObject *owner, IOFilterInterruptEventSource *source)
{
Envy24AudioEngine *audioEngine = OSDynamicCast(Envy24AudioEngine, owner);
// We've cast the audio engine from the owner which we passed in when we created the interrupt
// event source
if (audioEngine) {
// Then, filterInterrupt() is called on the specified audio engine
audioEngine->filterInterrupt(source->getIntIndex());
}
return false;
}
void Envy24AudioEngine::filterInterrupt(int index)
{
// In the case of our simple device, we only get interrupts when the audio engine loops to the
// beginning of the buffer. When that happens, we need to take a timestamp and increment
// the loop count. The function takeTimeStamp() does both of those for us. Additionally,
// if a different timestamp is to be used (other than the current time), it can be passed
// in to takeTimeStamp()
UInt8 intreq;
if ( ( intreq = card->pci_dev->ioRead8(CCS_INTR_STATUS, card->iobase) ) != 0 )
{
card->pci_dev->ioWrite8(CCS_INTR_STATUS, intreq, card->iobase); // clear it
if (intreq & CCS_INTR_PRO_MACRO)
{
UInt8 mtstatus = card->pci_dev->ioRead8(MT_INTR_MASK_STATUS, card->mtbase);
card->pci_dev->ioWrite8(MT_INTR_MASK_STATUS, mtstatus, card->mtbase); // clear interrupt
if (mtstatus & MT_PLAY_STATUS)
{
takeTimeStamp();
}
}
}
return;
}
UInt32 Envy24AudioEngine::lookUpFrequencyBits(UInt32 Frequency,
const UInt32* FreqList,
const UInt32* FreqBitList,
UInt32 ListSize,
UInt32 Default)
{
UInt32 FreqBit = Default;
for (UInt32 i = 0; i < ListSize; i++)
{
if (FreqList[i] == Frequency)
{
return FreqBitList[i];
}
}
return FreqBit;
}
void * Envy24AudioEngine::IOMallocContiguousWithLimit(vm_size_t size,
mach_vm_address_t physicalMask,
IOPhysicalAddress * physicalAddress)
{
mach_vm_address_t address = 0;
if (size == 0)
return 0;
{
IOBufferMemoryDescriptor * bmd;
bmd = IOBufferMemoryDescriptor::inTaskWithPhysicalMask(
kernel_task, kIOMemoryPhysicallyContiguous, size, physicalMask);
if (!bmd)
{
IOLog("Failed to allocate memory within the first 256MB address space!\n");
return NULL;
}
IOLog("Memory allocated OK!\n");
address = (mach_vm_address_t) bmd->getBytesNoCopy();
*physicalAddress = bmd->getPhysicalAddress();
IOLog("phys = %lx, virt = %lx\n", bmd->getPhysicalAddress(), address);
}
return (void *) address;
}
void Envy24AudioEngine::dumpRegisters()
{
DBGPRINT("Envy24AudioEngine[%p]::dumpRegisters()\n", this);
int i;
DBGPRINT("config 0x60 = %x\n", card->pci_dev->configRead8(0x60));
DBGPRINT("config 0x61 = %x\n", card->pci_dev->configRead8(0x61));
DBGPRINT("config 0x62 = %x\n", card->pci_dev->configRead8(0x62));
DBGPRINT("config 0x63 = %x\n", card->pci_dev->configRead8(0x63));
DBGPRINT("---\n");
for (i = 0; i <= 0x1F; i++)
{
DBGPRINT("CCS %02x: %02x\n", i, card->pci_dev->ioRead8(i, card->iobase));
}
IOSleep(100);
DBGPRINT("---\n");
for (i = 0; i <= 0x31; i++)
{
DBGPRINT("CCI %02x: %02x\n", i, ReadCCI(card, i));
}
IOSleep(100);
DBGPRINT("---\n");
for (i = 0; i <= 0x3F; i++)
{
DBGPRINT("MT %02x: %02x\n", i, card->pci_dev->ioRead8(i, card->mtbase));
}
IOSleep(1000);
}