-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer1.lpr
562 lines (460 loc) · 15.9 KB
/
player1.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
program Player1;
{$mode delphi} {Default to Delphi compatible syntax}
{$H+} {Default to AnsiString}
{$hints off}
{ Player file mp3 based Example 20 PWM Sound }
{ }
{ This example demonstrates using the PWM devices in Ultibo to play audio. }
{ }
{ The audio file is first loaded from the SD, decoded into a continuous stream }
{ of samples and then fed to the PWM FIFO using a DMA request. }
{ }
{Declare some units used by this example.}
uses
RaspberryPi3,
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 couple of PWM device references.}
var
Handle:THandle;
PWM0Device:PPWMDevice;
PWM1Device:PPWMDevice;
FMP3File: TMP3File;
Sample_Rate:Cardinal;
Sound_Channels:Integer;
Sound_Bits:Integer;
const
PWMSOUND_PWM_OSC_CLOCK = 19200000;
PWMSOUND_PWM_PLLD_CLOCK = 500000000;
function PWMSoundClockStart(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,'PWM Sound: PWM Clock Start');
{$ENDIF}
{Check Frequency}
if Frequency = 0 then Exit;
{Check Enabled}
if not BCM2710PWMClockEnabled(PWM) then
begin
{Get Divisors}
DivisorI:=PWMSOUND_PWM_PLLD_CLOCK div Frequency;
DivisorR:=PWMSOUND_PWM_PLLD_CLOCK mod Frequency;
DivisorF:=Trunc((DivisorR * 4096) / PWMSOUND_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,'PWM Sound: DivisorI=' + IntToStr(DivisorI));
if PWM_LOG_ENABLED then PWMLogDebug(PWM,'PWM Sound: DivisorF=' + IntToStr(DivisorF));
if PWM_LOG_ENABLED then PWMLogDebug(PWM,'PWM Sound: PWMCTL=' + IntToHex(PLongWord(BCM2837_CM_REGS_BASE + BCM2837_CM_PWMCTL)^,8));
if PWM_LOG_ENABLED then PWMLogDebug(PWM,'PWM Sound: 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 PWMSoundStart(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,'PWM Sound: 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 PWMSoundClockStart(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,'PWM Sound: CTL=' + IntToHex(PBCM2837PWMRegisters(PBCM2710PWMDevice(PWM).Address).CTL,8));
if PWM_LOG_ENABLED then PWMLogDebug(PWM,'PWM Sound: STA=' + IntToHex(PBCM2837PWMRegisters(PBCM2710PWMDevice(PWM).Address).STA,8));
if PWM_LOG_ENABLED then PWMLogDebug(PWM,'PWM Sound: DMAC=' + IntToHex(PBCM2837PWMRegisters(PBCM2710PWMDevice(PWM).Address).DMAC,8));
if PWM_LOG_ENABLED then PWMLogDebug(PWM,'PWM Sound: RNG1=' + IntToHex(PBCM2837PWMRegisters(PBCM2710PWMDevice(PWM).Address).RNG1,8));
if PWM_LOG_ENABLED then PWMLogDebug(PWM,'PWM Sound: DAT1=' + IntToHex(PBCM2837PWMRegisters(PBCM2710PWMDevice(PWM).Address).DAT1,8));
if PWM_LOG_ENABLED then PWMLogDebug(PWM,'PWM Sound: RNG2=' + IntToHex(PBCM2837PWMRegisters(PBCM2710PWMDevice(PWM).Address).RNG2,8));
if PWM_LOG_ENABLED then PWMLogDebug(PWM,'PWM Sound: DAT2=' + IntToHex(PBCM2837PWMRegisters(PBCM2710PWMDevice(PWM).Address).DAT2,8));
{$ENDIF}
{Memory Barrier}
DataMemoryBarrier; {After the Last Read}
{Return Result}
Result:=ERROR_SUCCESS;
end;
function PWMSoundSetFrequency(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,'PWM Sound: 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 PWMSoundClockStart(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,'PWM Sound: 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 PWMSoundPlaySample(PWM:PPWMDevice;Data:Pointer;Size,ChannelCount,BitCount:LongWord):LongWord;
var
Buffer:PByte;
Count:LongWord;
Value1:LongWord;
Value2:LongWord;
RangeBits:LongWord;
Output:PLongWord;
Samples:LongWord;
Current:LongWord;
DMAData:PDMAData;
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,'PWM Sound: 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;
{Allocate Output}
Output:=DMAAllocateBuffer(Samples * SizeOf(LongWord));
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;
{Get DMA data}
DMAData:=GetMem(SizeOf(TDMAData));
if DMAData = nil then Exit;
DMAData.Source:=Output;
DMAData.Dest:=PBCM2710PWMDevice(PWM).Address + BCM2837_PWM_FIF1;
DMAData.Size:=Samples * SizeOf(LongWord);
DMAData.Flags:=DMA_DATA_FLAG_DEST_NOINCREMENT or DMA_DATA_FLAG_DEST_DREQ or DMA_DATA_FLAG_LITE;
DMAData.StrideLength:=0;
DMAData.SourceStride:=0;
DMAData.DestStride:=0;
DMAData.Next:=nil;
{Enable DMA}
PBCM2837PWMRegisters(PBCM2710PWMDevice(PWM).Address).DMAC:=PBCM2837PWMRegisters(PBCM2710PWMDevice(PWM).Address).DMAC or BCM2837_PWM_DMAC_ENAB;
{Perform DMA transfer}
DMATransfer(DMAData,DMA_DIR_MEM_TO_DEV,DMA_DREQ_ID_PWM);
finally
DMAReleaseBuffer(Output);
end;
{Return Result}
Result:=ERROR_SUCCESS;
end;
function PWMSoundPlayMp3(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,'PWM Sound: 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 file mp3 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:=PWMSoundPlaySample(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;
begin
{Create a console window and display a welcome message}
Handle:=ConsoleWindowCreate(ConsoleDeviceGetDefault,CONSOLE_POSITION_FULL,True);
ConsoleWindowWriteLn(Handle,'Welcome to Player mp3 based Example 20 PWM Sound');
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 devices
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}
PWM0Device:=PWMDeviceFindByName('PWM0');
PWM1Device:=PWMDeviceFindByName('PWM1');
if (PWM0Device <> nil) and (PWM1Device <> 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:=PWMSoundStart;
PWM0Device.DeviceSetFrequency:=PWMSoundSetFrequency;
PWM1Device.DeviceStart:=PWMSoundStart;
PWM1Device.DeviceSetFrequency:=PWMSoundSetFrequency;
{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);
{Setup PWM device 1}
{Use exactly the same settings as PWM0 except the GPIO is 41}
PWMDeviceSetGPIO(PWM1Device,GPIO_PIN_41);
PWMDeviceSetRange(PWM1Device,(CLOCK_RATE + (SAMPLE_RATE div 2)) div SAMPLE_RATE);
PWMDeviceSetMode(PWM1Device,PWM_MODE_BALANCED);
PWMDeviceSetFrequency(PWM1Device,CLOCK_RATE);
ConsoleWindowWriteLn(Handle,'Range = ' + IntToStr(PWM0Device.Range));
{Start the PWM devices}
if (PWMDeviceStart(PWM0Device) = ERROR_SUCCESS) and (PWMDeviceStart(PWM1Device) = 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 PWMSoundPlayMp3(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 devices}
PWMDeviceStop(PWM0Device);
PWMDeviceStop(PWM1Device);
end
else
begin
ConsoleWindowWriteLn(Handle,'Error: Failed to start PWM devices 0 and 1');
end;
end
else
begin
ConsoleWindowWriteLn(Handle,'Error: Failed to locate PWM devices 0 and 1');
end;
{Turn on the LED to indicate completion}
ActivityLEDEnable;
ActivityLEDOn;
FMP3File.Free;
{Halt the thread if we return}
ThreadHalt(0);
end.