1 | unit AudioSystemDSP;
|
---|
2 |
|
---|
3 | {$I CoolAudioConfig.inc}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | {$IFDEF AudioSystemDSP}
|
---|
8 | uses
|
---|
9 | Classes, SysUtils, AudioSystem, BaseUnix, WavFile, ExtCtrls;
|
---|
10 |
|
---|
11 | type
|
---|
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 |
|
---|
46 | const
|
---|
47 | BUFFERLEN = 1024;
|
---|
48 |
|
---|
49 | const
|
---|
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 |
|
---|
58 | resourcestring
|
---|
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 |
|
---|
72 | implementation
|
---|
73 |
|
---|
74 | {$IFDEF AudioSystemDSP}
|
---|
75 |
|
---|
76 | { TAudioSystemDSP }
|
---|
77 |
|
---|
78 | function TAudioSystemDSP.GetMediaPlayerDriverClass: TMediaPlayerDriverClass;
|
---|
79 | begin
|
---|
80 | Result := TPlayerDSP;
|
---|
81 | end;
|
---|
82 |
|
---|
83 | procedure TAudioSystemDSP.OpenDevice;
|
---|
84 | var
|
---|
85 | Stereo: Longint;
|
---|
86 | begin
|
---|
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));
|
---|
105 | end;
|
---|
106 |
|
---|
107 | procedure TAudioSystemDSP.CloseDevice;
|
---|
108 | begin
|
---|
109 | if FDeviceId >= 0 then FpClose(FDeviceId);
|
---|
110 | end;
|
---|
111 |
|
---|
112 | constructor TAudioSystemDSP.Create(AOwner: TComponent);
|
---|
113 | begin
|
---|
114 | inherited;
|
---|
115 | BitsPerSample := 16;
|
---|
116 | Channels := 2;
|
---|
117 | SampleRate := 44000;
|
---|
118 | end;
|
---|
119 |
|
---|
120 | destructor TAudioSystemDSP.Destroy;
|
---|
121 | begin
|
---|
122 | inherited Destroy;
|
---|
123 | end;
|
---|
124 |
|
---|
125 | { TPlayerMAD }
|
---|
126 |
|
---|
127 | procedure TPlayerDSP.TimerExecute(Sender: TObject);
|
---|
128 | var
|
---|
129 | Buffer: array of Byte;
|
---|
130 | Size: Integer;
|
---|
131 | begin
|
---|
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;
|
---|
137 | end;
|
---|
138 |
|
---|
139 | procedure TPlayerDSP.Play;
|
---|
140 | begin
|
---|
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');
|
---|
157 | end;
|
---|
158 |
|
---|
159 | procedure TPlayerDSP.Pause;
|
---|
160 | begin
|
---|
161 | if FActive then FTimer.Enabled := not FTimer.Enabled;
|
---|
162 | end;
|
---|
163 |
|
---|
164 | procedure TPlayerDSP.Stop;
|
---|
165 | begin
|
---|
166 | inherited Stop;
|
---|
167 | FTimer.Enabled := False;
|
---|
168 | TAudioSystemDSP(AudioSystem).CloseDevice;
|
---|
169 | end;
|
---|
170 |
|
---|
171 | procedure TPlayerDSP.Open;
|
---|
172 | begin
|
---|
173 | inherited;
|
---|
174 | WavFile.OpenFile(FFileName);
|
---|
175 | end;
|
---|
176 |
|
---|
177 | procedure TPlayerDSP.Close;
|
---|
178 | begin
|
---|
179 | inherited Close;
|
---|
180 | end;
|
---|
181 |
|
---|
182 | constructor TPlayerDSP.Create;
|
---|
183 | begin
|
---|
184 | inherited;
|
---|
185 | WavFile := TWAVFile.Create;
|
---|
186 | FTimer := TTimer.Create(nil);
|
---|
187 | end;
|
---|
188 |
|
---|
189 | destructor TPlayerDSP.Destroy;
|
---|
190 | begin
|
---|
191 | FreeAndNil(FTimer);
|
---|
192 | FreeAndNil(WavFile);
|
---|
193 | inherited Destroy;
|
---|
194 | end;
|
---|
195 |
|
---|
196 | {$ENDIF}
|
---|
197 |
|
---|
198 | end.
|
---|