-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer2.lpr
574 lines (473 loc) · 15.9 KB
/
player2.lpr
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
program player2;
{$mode delphi} {Default to Delphi compatible syntax}
{$H+} {Default to AnsiString}
{$hints off}
{ Player file mp3 based Advanced example - PWM PCM }
{ }
{ Unlike Example 20 which uses DMA to play the sound this example feeds the }
{ samples directly to the PWM FIFO buffer using the CPU. }
{ }
uses
RaspberryPi3, {Change this to RaspberryPi or RaspberryPi2 as required for your model}
GlobalConfig,
GlobalConst,
GlobalTypes,
Platform,
Threads,
Console,
Classes,
SysUtils,
uminimp3,
PWM, {Include the PWM unit to allow access to the functions}
BCM2710, {Include the BCM2710 and BCM2837 units for access to the PWM device}
BCM2837; {and PWM register values and constants.}
{We'll need a window handle and a PWM device reference.}
var
Handle:THandle;
PWM0Device:PPWMDevice;
FMP3File: TMP3File;
Sample_Rate:Cardinal;
Sound_Channels:Integer;
Sound_Bits:Integer;
const
PWMPCM_PWM_OSC_CLOCK = 19200000;
PWMPCM_PWM_PLLD_CLOCK = 500000000;
function PWMPCMClockStart(PWM:PPWMDevice;Frequency:LongWord):LongWord;
var
DivisorI:LongWord;
DivisorR:LongWord;
DivisorF:LongWord;
begin
{}
Result:=ERROR_INVALID_PARAMETER;
{Check PWM}
if PWM = nil then Exit;
{$IF DEFINED(BCM2710_DEBUG) or DEFINED(PWM_DEBUG)}
if PWM_LOG_ENABLED then PWMLogDebug(PWM,'PWMPCM: PWM Clock Start');
{$ENDIF}
{Check Frequency}
if Frequency = 0 then Exit;
{Check Enabled}
if not BCM2710PWMClockEnabled(PWM) then
begin
{Get Divisors}
DivisorI:=PWMPCM_PWM_PLLD_CLOCK div Frequency;
DivisorR:=PWMPCM_PWM_PLLD_CLOCK mod Frequency;
DivisorF:=Trunc((DivisorR * 4096) / PWMPCM_PWM_PLLD_CLOCK);
if DivisorI > 4095 then DivisorI:=4095;
{Memory Barrier}
DataMemoryBarrier; {Before the First Write}
{Set Dividers}
PLongWord(BCM2837_CM_REGS_BASE + BCM2837_CM_PWMDIV)^:=BCM2837_CM_PASSWORD or (DivisorI shl 12) or DivisorF;
{Delay}
MicrosecondDelay(10);
{Set Source}
PLongWord(BCM2837_CM_REGS_BASE + BCM2837_CM_PWMCTL)^:=BCM2837_CM_PASSWORD or BCM2837_CM_CTL_SRC_PLLD;
{Delay}
MicrosecondDelay(10);
{Start Clock}
PLongWord(BCM2837_CM_REGS_BASE + BCM2837_CM_PWMCTL)^:=BCM2837_CM_PASSWORD or PLongWord(BCM2837_CM_REGS_BASE + BCM2837_CM_PWMCTL)^ or BCM2837_CM_CTL_ENAB;
{Delay}
MicrosecondDelay(110);
{$IF DEFINED(BCM2710_DEBUG) or DEFINED(PWM_DEBUG)}
if PWM_LOG_ENABLED then PWMLogDebug(PWM,'PWMPCM: DivisorI=' + IntToStr(DivisorI));
if PWM_LOG_ENABLED then PWMLogDebug(PWM,'PWMPCM: DivisorF=' + IntToStr(DivisorF));
if PWM_LOG_ENABLED then PWMLogDebug(PWM,'PWMPCM: PWMCTL=' + IntToHex(PLongWord(BCM2837_CM_REGS_BASE + BCM2837_CM_PWMCTL)^,8));
if PWM_LOG_ENABLED then PWMLogDebug(PWM,'PWMPCM: PWMDIV=' + IntToHex(PLongWord(BCM2837_CM_REGS_BASE + BCM2837_CM_PWMDIV)^,8));
{$ENDIF}
{Memory Barrier}
DataMemoryBarrier; {After the Last Read}
end;
{Return Result}
Result:=ERROR_SUCCESS;
end;
function PWMPCMStart(PWM:PPWMDevice):LongWord;
begin
{}
Result:=ERROR_INVALID_PARAMETER;
{Check PWM}
if PWM = nil then Exit;
{$IF DEFINED(BCM2710_DEBUG) or DEFINED(PWM_DEBUG)}
if PWM_LOG_ENABLED then PWMLogDebug(PWM,'PWMPCM: PWM Start');
{$ENDIF}
{Check Settings}
if PWM.Range = 0 then Exit;
if PWM.Frequency = 0 then Exit;
{Check GPIO}
if PWM.GPIO = GPIO_PIN_UNKNOWN then
begin
{Check Channel}
case PBCM2710PWMDevice(PWM).Channel of
0:begin
{Set GPIO 18}
if BCM2710PWMSetGPIO(PWM,GPIO_PIN_18) <> ERROR_SUCCESS then Exit;
end;
1:begin
{Set GPIO 19}
if BCM2710PWMSetGPIO(PWM,GPIO_PIN_19) <> ERROR_SUCCESS then Exit;
end;
else
begin
Exit;
end;
end;
end;
{Start Clock}
if PWMPCMClockStart(PWM,PWM.Frequency) <> ERROR_SUCCESS then Exit;
{Memory Barrier}
DataMemoryBarrier; {Before the First Write}
{Check Channel}
case PBCM2710PWMDevice(PWM).Channel of
0:begin
{PWM0 (PWM Channel 1)}
{Enable PWEN, USEF and CLRF}
PBCM2837PWMRegisters(PBCM2710PWMDevice(PWM).Address).CTL:=PBCM2837PWMRegisters(PBCM2710PWMDevice(PWM).Address).CTL or BCM2837_PWM_CTL_PWEN1 or BCM2837_PWM_CTL_USEF1 or BCM2837_PWM_CTL_CLRF1;
end;
1:begin
{PWM1 (PWM Channel 2)}
{Enable PWEN, USEF and CLRF}
PBCM2837PWMRegisters(PBCM2710PWMDevice(PWM).Address).CTL:=PBCM2837PWMRegisters(PBCM2710PWMDevice(PWM).Address).CTL or BCM2837_PWM_CTL_PWEN2 or BCM2837_PWM_CTL_USEF2 or BCM2837_PWM_CTL_CLRF1;
end;
else
begin
Exit;
end;
end;
{Clear Status}
PBCM2837PWMRegisters(PBCM2710PWMDevice(PWM).Address).STA:=LongWord(-1);
{$IF DEFINED(BCM2710_DEBUG) or DEFINED(PWM_DEBUG)}
if PWM_LOG_ENABLED then PWMLogDebug(PWM,'PWMPCM: CTL=' + IntToHex(PBCM2837PWMRegisters(PBCM2710PWMDevice(PWM).Address).CTL,8));
if PWM_LOG_ENABLED then PWMLogDebug(PWM,'PWMPCM: STA=' + IntToHex(PBCM2837PWMRegisters(PBCM2710PWMDevice(PWM).Address).STA,8));
if PWM_LOG_ENABLED then PWMLogDebug(PWM,'PWMPCM: DMAC=' + IntToHex(PBCM2837PWMRegisters(PBCM2710PWMDevice(PWM).Address).DMAC,8));
if PWM_LOG_ENABLED then PWMLogDebug(PWM,'PWMPCM: RNG1=' + IntToHex(PBCM2837PWMRegisters(PBCM2710PWMDevice(PWM).Address).RNG1,8));
if PWM_LOG_ENABLED then PWMLogDebug(PWM,'PWMPCM: DAT1=' + IntToHex(PBCM2837PWMRegisters(PBCM2710PWMDevice(PWM).Address).DAT1,8));
if PWM_LOG_ENABLED then PWMLogDebug(PWM,'PWMPCM: RNG2=' + IntToHex(PBCM2837PWMRegisters(PBCM2710PWMDevice(PWM).Address).RNG2,8));
if PWM_LOG_ENABLED then PWMLogDebug(PWM,'PWMPCM: DAT2=' + IntToHex(PBCM2837PWMRegisters(PBCM2710PWMDevice(PWM).Address).DAT2,8));
{$ENDIF}
{Memory Barrier}
DataMemoryBarrier; {After the Last Read}
{Return Result}
Result:=ERROR_SUCCESS;
end;
function PWMPCMSetFrequency(PWM:PPWMDevice;Frequency:LongWord):LongWord;
begin
{}
Result:=ERROR_INVALID_PARAMETER;
{Check PWM}
if PWM = nil then Exit;
{$IF DEFINED(BCM2710_DEBUG) or DEFINED(PWM_DEBUG)}
if PWM_LOG_ENABLED then PWMLogDebug(PWM,'PWMPCM: PWM Set Frequency (Frequency=' + IntToStr(Frequency) + ')');
{$ENDIF}
{Check Frequency}
if Frequency = 0 then Exit;
{Check Pair}
if PBCM2710PWMDevice(PWM).Pair <> nil then
begin
{Check Enabled}
if PBCM2710PWMDevice(PWM).Pair.PWM.PWMState = PWM_STATE_ENABLED then Exit;
end;
{Stop Clock}
if BCM2710PWMClockStop(PWM) <> ERROR_SUCCESS then Exit;
{Check Enabled}
if PWM.PWMState = PWM_STATE_ENABLED then
begin
{Start Clock}
if PWMPCMClockStart(PWM,Frequency) <> ERROR_SUCCESS then Exit;
end;
{Update Scaler}
PBCM2710PWMDevice(PWM).Scaler:=NANOSECONDS_PER_SECOND div Frequency;
{$IF DEFINED(BCM2710_DEBUG) or DEFINED(PWM_DEBUG)}
if PWM_LOG_ENABLED then PWMLogDebug(PWM,'PWMPCM: Scaler=' + IntToStr(PBCM2710PWMDevice(PWM).Scaler));
{$ENDIF}
{Update Properties}
PWM.Frequency:=Frequency;
PWM.Properties.Frequency:=Frequency;
{Check Pair}
if PBCM2710PWMDevice(PWM).Pair <> nil then
begin
{Update Scaler}
PBCM2710PWMDevice(PWM).Pair.Scaler:=NANOSECONDS_PER_SECOND div Frequency;
{Update Properties}
PBCM2710PWMDevice(PWM).Pair.PWM.Frequency:=Frequency;
PBCM2710PWMDevice(PWM).Pair.PWM.Properties.Frequency:=Frequency;
end;
{Return Result}
Result:=ERROR_SUCCESS;
end;
function PWMPCMPlaySample(PWM:PPWMDevice;Data:Pointer;Size,ChannelCount,BitCount:LongWord):LongWord;
var
Buffer:PByte;
Count:LongWord;
Value1:LongWord;
Value2:LongWord;
RangeBits:LongWord;
Output:PWord;
Samples:LongWord;
Current:LongWord;
Status:LongWord;
FullCount:LongWord;
EmptyCount:LongWord;
GAPCount:LongWord;
begin
{}
Result:=ERROR_INVALID_PARAMETER;
{Check PWM}
if PWM = nil then Exit;
{$IF DEFINED(BCM2710_DEBUG) or DEFINED(PWM_DEBUG)}
if PWM_LOG_ENABLED then PWMLogDebug(PWM,'PWMPCM: PWM Play Sample');
{$ENDIF}
{Check Parameters}
if Size = 0 then Exit;
if (ChannelCount <> 1) and (ChannelCount <> 2) then Exit;
if (BitCount <> 8) and (BitCount <> 16) then Exit;
ConsoleWindowWriteLn(Handle,'Playing ' + IntToStr(Size) + ' bytes on ' + IntToStr(ChannelCount) + ' channel(s) at ' + IntToStr(BitCount) + ' bits per channel');
{Calculate Range Bits}
RangeBits:=0;
Count:=2;
while Count < 16 do
begin
if PWM.Range < (1 shl Count) then
begin
RangeBits:=Count - 1;
Break;
end;
Inc(Count);
end;
ConsoleWindowWriteLn(Handle,'Range = ' + IntToStr(PWM.Range));
ConsoleWindowWriteLn(Handle,'Range Bits = ' + IntToStr(RangeBits));
{Get Sample Count}
Samples:=0;
if BitCount = 8 then
begin
Samples:=Size;
if ChannelCount = 1 then
begin
Samples:=Samples * 2;
end;
end
else if BitCount = 16 then
begin
Samples:=Size div 2;
if ChannelCount = 1 then
begin
Samples:=Samples * 2;
end;
end;
if Samples = 0 then Exit;
Output:=GetMem(Samples * SizeOf(Word));
if Output = nil then Exit;
try
ConsoleWindowWriteLn(Handle,'Total Samples = ' + IntToStr(Samples));
{Convert Sound}
Buffer:=Data;
Count:=0;
Current:=0;
while Count < Size do
begin
{Get channel 1}
Value1:=Buffer[Count];
Inc(Count);
if BitCount > 8 then
begin
{Get 16 bit sample}
Value1:=Value1 or (Buffer[Count] shl 8);
Inc(Count);
{Convert to unsigned}
Value1:=(Value1 + $8000) and ($FFFF);
end;
if BitCount >= RangeBits then
begin
Value1:=Value1 shr (BitCount - RangeBits);
end
else
begin
Value1:=Value1 shl (RangeBits - BitCount);
end;
{Get channel 2}
Value2:=Value1;
if ChannelCount = 2 then
begin
Value2:=Buffer[Count];
Inc(Count);
if BitCount > 8 then
begin
{Get 16 bit sample}
Value2:=Value2 or (Buffer[Count] shl 8);
Inc(Count);
{Convert to unsigned}
Value2:=(Value2 + $8000) and ($FFFF);
end;
if BitCount >= RangeBits then
begin
Value2:=Value2 shr (BitCount - RangeBits);
end
else
begin
Value2:=Value2 shl (RangeBits - BitCount);
end;
end;
{Store Sample}
Output[Current]:=Value1;
Output[Current + 1]:=Value2;
Inc(Current,2);
end;
{Play Samples}
Count:=0;
FullCount:=0;
EmptyCount:=0;
GAPCount:=0;
while Count < Samples do
begin
{Check FIFO}
Status:=PBCM2837PWMRegisters(PBCM2710PWMDevice(PWM).Address).STA;
while ((Status and BCM2837_PWM_STA_FULL1) = 0) and (Count < Samples) do
begin
{Check for FIFO empty}
if (Status and BCM2837_PWM_STA_EMPT1) <> 0 then
begin
Inc(EmptyCount);
end;
PBCM2837PWMRegisters(PBCM2710PWMDevice(PWM).Address).FIF1:=Output[Count];
Inc(Count);
{Skip channel 2}
Inc(Count);
Status:=PBCM2837PWMRegisters(PBCM2710PWMDevice(PWM).Address).STA;
end;
{Check for FIFO full}
if (Status and BCM2837_PWM_STA_FULL1) <> 0 then
begin
Inc(FullCount);
MicrosecondDelay(10);
end;
{Check for GAP1 error}
if (Status and BCM2837_PWM_STA_GAPO1) <> 0 then
begin
PBCM2837PWMRegisters(PBCM2710PWMDevice(PWM).Address).STA:=BCM2837_PWM_STA_GAPO1;
Inc(GAPCount);
end;
end;
finally
FreeMem(Output);
end;
ConsoleWindowWriteLn(Handle,'FIFO Full count = ' + IntToStr(FullCount));
ConsoleWindowWriteLn(Handle,'FIFO Empty count = ' + IntToStr(EmptyCount));
ConsoleWindowWriteLn(Handle,'GAP error count = ' + IntToStr(GAPCount));
{Return Result}
Result:=ERROR_SUCCESS;
end;
function PWMPCMPlayMp3(PWM:PPWMDevice;ChannelCount,BitCount:LongWord):LongWord;
var
Buffer:Pointer;
AudioStream : TMemoryStream;
begin
{}
Result:=ERROR_INVALID_PARAMETER;
{Check PWM}
if PWM = nil then Exit;
{$IF DEFINED(BCM2710_DEBUG) or DEFINED(PWM_DEBUG)}
if PWM_LOG_ENABLED then PWMLogDebug(PWM,'PWMPCM: PWM Play File');
{$ENDIF}
{Check Parameters}
if (ChannelCount <> 1) and (ChannelCount <> 2) then Exit;
if (BitCount <> 8) and (BitCount <> 16) then Exit;
ConsoleWindowWriteLn(Handle,'Playing on ' + IntToStr(ChannelCount) + ' channel(s) at ' + IntToStr(BitCount) + ' bits per channel');
{Create AudioStream}
AudioStream := TMemoryStream.Create();
try
{Decode File mp3 to Audiostream}
FMP3File.DecodeTo(AudioStream);
AudioStream.Position := 0;
Buffer:=GetMem(AudioStream.Size);
try
AudioStream.Read(Buffer^,AudioStream.Size);
Result:=PWMPCMPlaySample(PWM,Buffer,AudioStream.Size,ChannelCount,BitCount);
if Result <> ERROR_SUCCESS then Exit;
finally
FreeMem(Buffer);
end;
finally
AudioStream.Free;
end;
{Return Result}
Result:=ERROR_SUCCESS;
end;
procedure WaitForSDDrive;
begin
while not DirectoryExists ('C:\') do sleep (500);
end;
const
CLOCK_RATE = 250000000; {For Raspberry Pi or Raspberry Pi 2 change this to 125000000}
begin
{Create a console window and display a welcome message}
Handle:=ConsoleWindowCreate(ConsoleDeviceGetDefault,CONSOLE_POSITION_FULL,True);
ConsoleWindowWriteLn(Handle,'Welcome to Player mp3 based PWM PCM advanced example');
ConsoleWindowWriteLn(Handle,'Make sure you have a the Raspberry Pi audio jack connected to the AUX input of an amplifier, TV or other audio device');
WaitForSDDrive;
ConsoleWindowWriteLn(Handle,'Load file test.mp3');
FMP3File := TMP3File.Create();
FMP3File.LoadFromFile('C:\test.mp3');
Sample_Rate := FMP3File.FMP3Hz;
Sound_Channels := FMP3File.FMP3Channels;
Sound_Bits := cBitsPerSample;
ConsoleWindowWriteLn(Handle,'====== Info File mp3 ======================');
ConsoleWindowWriteLn(Handle,'Channels = ' + Inttostr(Sound_Channels) );
ConsoleWindowWriteLn(Handle,'Frequency (hz) = ' + Inttostr(Sample_Rate) );
ConsoleWindowWriteLn(Handle,'Layers = ' + Inttostr(FMP3File.FMP3Layer) );
ConsoleWindowWriteLn(Handle,'Bitrate (kps) = ' + Inttostr(FMP3File.FMP3Bitrate_kbps) );
ConsoleWindowWriteLn(Handle,'===========================================');
{First locate the PWM device
The Raspberry Pi has two PWM channels which will normally end up with
the names PWM0 and PWM1 when the driver is included in an application.
For this example we only need PWM0, you can setup both channels to play
stereo audio and the decoding function above includes 2 channel support
but we'll leave that as an exercise for you to experiment with}
PWM0Device:=PWMDeviceFindByName('PWM0');
if PWM0Device <> nil then
begin
{Modify PWM device functions.
This allows us to change the behaviour of the PWM driver so we can
use a different clock source and enable the FIFO for audio output}
PWM0Device.DeviceStart:=PWMPCMStart;
PWM0Device.DeviceSetFrequency:=PWMPCMSetFrequency;
{Setup PWM device 0}
{Set the GPIO}
PWMDeviceSetGPIO(PWM0Device,GPIO_PIN_40);
{Set the range}
PWMDeviceSetRange(PWM0Device,(CLOCK_RATE + (SAMPLE_RATE div 2)) div SAMPLE_RATE);
{And the mode to PWM_MODE_BALANCED}
PWMDeviceSetMode(PWM0Device,PWM_MODE_BALANCED);
{Finally set the frequency}
PWMDeviceSetFrequency(PWM0Device,CLOCK_RATE);
{Start the PWM device}
if PWMDeviceStart(PWM0Device) = ERROR_SUCCESS then
begin
{Play the Sound Sample.
If you change the file to play a different sample make sure you adjust
the sample rate, channel count and number of bits to match your sample}
if PWMPCMPlayMp3(PWM0Device,SOUND_CHANNELS,SOUND_BITS) <> ERROR_SUCCESS then
begin
ConsoleWindowWriteLn(Handle,'Error: Failed to play sound file');
end
else
begin
ConsoleWindowWriteLn(Handle,'Finished playing sound sample');
end;
{Stop the PWM device}
PWMDeviceStop(PWM0Device);
end
else
begin
ConsoleWindowWriteLn(Handle,'Error: Failed to start PWM device 0');
end;
end
else
begin
ConsoleWindowWriteLn(Handle,'Error: Failed to locate PWM device 0');
end;
{Turn on the LED to indicate completion}
ActivityLEDEnable;
ActivityLEDOn;
FMP3File.Free;
{Halt the thread if we return}
ThreadHalt(0);
end.