source: trunk/Packages/CoolAudio/Systems/DSP/AudioSystemDSP.pas

Last change on this file was 75, checked in by chronos, 8 months ago
  • Modified: Removed U prefix from CoolAudio package unit names.
File size: 4.9 KB
Line 
1unit AudioSystemDSP;
2
3{$I CoolAudioConfig.inc}
4
5interface
6
7{$IFDEF AudioSystemDSP}
8uses
9 Classes, SysUtils, AudioSystem, BaseUnix, WavFile, ExtCtrls;
10
11type
12
13 { TAudioSystemDSP }
14
15 TAudioSystemDSP = class(TAudioSystem)
16 private
17 FDeviceId: Integer;
18 public
19 Channels: Integer;
20 SampleRate: Integer; // Hz
21 BitsPerSample: Integer;
22 function GetMediaPlayerDriverClass: TMediaPlayerDriverClass; override;
23 procedure OpenDevice;
24 procedure CloseDevice;
25 constructor Create(AOwner: TComponent); override;
26 destructor Destroy; override;
27 end;
28
29 { TPlayerDSP }
30
31 TPlayerDSP = class(TMediaPlayerDriver)
32 private
33 FTimer: TTimer;
34 procedure TimerExecute(Sender: TObject);
35 public
36 WavFile: TWAVFile;
37 procedure Play; override;
38 procedure Pause; override;
39 procedure Stop; override;
40 procedure Open; override;
41 procedure Close; override;
42 constructor Create; override;
43 destructor Destroy; override;
44 end;
45
46const
47 BUFFERLEN = 1024;
48
49const
50 { Values obtained from a C program - These are complex (!) C macros }
51 SNDCTL_DSP_STEREO = -1073459197;
52 SNDCTL_DSP_RESET = 20480;
53 SNDCTL_DSP_SYNC = 20481;
54 SOUND_PCM_WRITE_BITS = -1073459195;
55 SOUND_PCM_WRITE_CHANNELS = -1073459194;
56 SOUND_PCM_WRITE_RATE = -1073459198;
57
58resourcestring
59 SPlaying = 'Playing : ';
60 SErrChannels = 'Error : Number of channels not supported: ';
61 SErrOpeningDevice = 'Could not open sound device';
62 SErrSettingStereo = 'Could not set stereo';
63 SErrResettingDevice = 'Could not reset DSP device';
64 SErrSetWriteBits = 'Could not set write bits to 16';
65 SErrSetChannels = 'Could not set channels';
66 SErrSetSampleRate = 'Could not set sync mode';
67 SErrSetSyncMode = 'Could not set sync mode';
68
69{$ENDIF}
70
71
72implementation
73
74{$IFDEF AudioSystemDSP}
75
76{ TAudioSystemDSP }
77
78function TAudioSystemDSP.GetMediaPlayerDriverClass: TMediaPlayerDriverClass;
79begin
80 Result := TPlayerDSP;
81end;
82
83procedure TAudioSystemDSP.OpenDevice;
84var
85 Stereo: Longint;
86begin
87 Write('Openning /dev/dsp');
88 FDeviceId := fpOpen('/dev/dsp', O_WRONLY, 0);
89 if FDeviceId < 0 then
90 raise Exception.Create(SErrOpeningDevice);
91 Stereo := 0;
92 if not (FpIOCtl(FDeviceId, SNDCTL_DSP_STEREO, @Stereo) <> -1) then
93 raise Exception.Create(SErrSettingStereo);
94 if not (FpIOCtl(FDeviceId, SNDCTL_DSP_RESET, nil) <> -1) then
95 raise Exception.Create(SErrResettingDevice);
96 if not (FpIOCtl(FDeviceId, SOUND_PCM_WRITE_BITS, @BitsPerSample) <> -1) then
97 raise Exception.Create(SErrSetWriteBits);
98 if not (FpIOCtl(FDeviceId, SOUND_PCM_WRITE_CHANNELS, @Channels) <> -1) then
99 raise Exception.Create(SErrSetChannels);
100 if not (FpIOCtl(FDeviceId, SOUND_PCM_WRITE_RATE, @SampleRate) <> -1) then
101 raise Exception.Create(SErrSetSampleRate);
102 if not (FpIOCtl(FDeviceId, SNDCTL_DSP_SYNC, nil) <> -1) then
103 raise Exception.Create(SErrSetSyncMode);
104 WriteLn(' device: ' + IntToStr(FDeviceId));
105end;
106
107procedure TAudioSystemDSP.CloseDevice;
108begin
109 if FDeviceId >= 0 then FpClose(FDeviceId);
110end;
111
112constructor TAudioSystemDSP.Create(AOwner: TComponent);
113begin
114 inherited;
115 BitsPerSample := 16;
116 Channels := 2;
117 SampleRate := 44000;
118end;
119
120destructor TAudioSystemDSP.Destroy;
121begin
122 inherited Destroy;
123end;
124
125{ TPlayerMAD }
126
127procedure TPlayerDSP.TimerExecute(Sender: TObject);
128var
129 Buffer: array of Byte;
130 Size: Integer;
131begin
132 SetLength(Buffer, 10000);
133 Size := WavFile.SourceFile.Read(PByte(Buffer)^, System.Length(Buffer));
134 Write('Buf: ' + IntToStr(Size) + ', ');
135 fpWrite(TAudioSystemDSP(AudioSystem).FDeviceId, PByte(Buffer)^, Size);
136 FTimer.Interval := 100;
137end;
138
139procedure TPlayerDSP.Play;
140begin
141 WriteLn('Stop');
142 Stop;
143 TAudioSystemDSP(AudioSystem).SampleRate := WavFile.SampleRate;
144 TAudioSystemDSP(AudioSystem).Channels := WavFile.ChannelModeID;
145 TAudioSystemDSP(AudioSystem).BitsPerSample := WavFile.BitsPerSample;
146 TAudioSystemDSP(AudioSystem).OpenDevice;
147 WriteLn('seek');
148 WavFile.SourceFile.Seek(SizeOf(TWAVRecord), soFromBeginning);
149 while WavFile.SourceFile.Position < WavFile.SourceFile.Size do
150 TimerExecute(nil);
151 WriteLn('int');
152 FTimer.Interval := 10;
153 FTimer.OnTimer := TimerExecute;
154 WriteLn('ena');
155 //FTimer.Enabled := True;
156 WriteLn('true');
157end;
158
159procedure TPlayerDSP.Pause;
160begin
161 if FActive then FTimer.Enabled := not FTimer.Enabled;
162end;
163
164procedure TPlayerDSP.Stop;
165begin
166 inherited Stop;
167 FTimer.Enabled := False;
168 TAudioSystemDSP(AudioSystem).CloseDevice;
169end;
170
171procedure TPlayerDSP.Open;
172begin
173 inherited;
174 WavFile.OpenFile(FFileName);
175end;
176
177procedure TPlayerDSP.Close;
178begin
179 inherited Close;
180end;
181
182constructor TPlayerDSP.Create;
183begin
184 inherited;
185 WavFile := TWAVFile.Create;
186 FTimer := TTimer.Create(nil);
187end;
188
189destructor TPlayerDSP.Destroy;
190begin
191 FreeAndNil(FTimer);
192 FreeAndNil(WavFile);
193 inherited Destroy;
194end;
195
196{$ENDIF}
197
198end.
Note: See TracBrowser for help on using the repository browser.