~ubuntu-branches/ubuntu/dapper/fpc/dapper

« back to all changes in this revision

Viewing changes to packages/extra/sndfile/sfplay.pp

  • Committer: Bazaar Package Importer
  • Author(s): Carlos Laviola
  • Date: 2004-08-12 16:29:37 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20040812162937-moo8ulvysp1ln771
Tags: 1.9.4-5
fp-compiler: needs ld, adding dependency on binutils.  (Closes: #265265)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
program sfplay;
 
2
 
 
3
{$mode objfpc}
 
4
{$h+}
 
5
 
 
6
uses sndfile,linux;
 
7
 
 
8
Const
 
9
  BUFFERLEN = 1024;
 
10
 
 
11
Const
 
12
  { Values obtained from a C program - These are complex (!) C macros }
 
13
  SNDCTL_DSP_STEREO = -1073459197;
 
14
  SNDCTL_DSP_RESET = 20480;
 
15
  SNDCTL_DSP_SYNC = 20481;
 
16
  SOUND_PCM_WRITE_BITS = -1073459195;
 
17
  SOUND_PCM_WRITE_CHANNELS = -1073459194;
 
18
  SOUND_PCM_WRITE_RATE = -1073459198;
 
19
 
 
20
ResourceString
 
21
  SPlaying = 'Playing : ';
 
22
  SErrChannels = 'Error : Number of channels not supported: ';
 
23
  SErrOpeningDevice = 'Could not open sound device';
 
24
  SErrSettingStereo = 'Could not set stereo';
 
25
  SErrResettingDevice = 'Could not reset DSP device';
 
26
  SErrSetWriteBits = 'Could not set write bits to 16';
 
27
  SErrSetChannels = 'Could not set channels';
 
28
  SErrSetSampleRate = 'Could not set sync mode';
 
29
  SErrSetSyncMode = 'Could not set sync mode';
 
30
 
 
31
Procedure PlayError(Msg : String);
 
32
 
 
33
begin
 
34
  Writeln(stderr,Msg);
 
35
  Halt(1);
 
36
end;
 
37
 
 
38
Function OpenDSPDevice(Channels,Samplerate : LongInt) : LongInt; forward;
 
39
 
 
40
procedure PlayFile(FileName : String);
 
41
 
 
42
Var
 
43
  Buffer : Array[0..BUFFERLEN-1] of word;
 
44
  SoundFile : PSndFile;
 
45
  Info  : SF_INFO;
 
46
  k, m, AudioDevice, readcount : Longint;
 
47
  ScaleData : Boolean;
 
48
 
 
49
begin
 
50
  Writeln(SPlaying,FileName);
 
51
  SoundFile:=sf_open_read(pChar(FileName),@Info);
 
52
  If (SoundFile=Nil) then
 
53
    begin
 
54
    sf_perror(Nil);
 
55
    exit;
 
56
    end;
 
57
  If not (Info.Channels in [1,2]) then
 
58
    PlayError(SerrChannels);
 
59
  AudioDevice:=OpenDSPDevice(Info.channels, Info.samplerate);
 
60
  ScaleData:=(Info.pcmbitwidth < 16);
 
61
  readcount:=sf_read_short(SoundFile,@Buffer,BUFFERLEN);
 
62
  While ReadCount<>0 do
 
63
    begin
 
64
    If ScaleData then
 
65
      For m:=0 to BufferLen-1 do
 
66
        Buffer[m]:=buffer[m] * 256;
 
67
    fdwrite (AudioDevice, buffer, readcount * sizeof (word)) ;
 
68
    readcount:=sf_read_short(SoundFile,@Buffer,BUFFERLEN);
 
69
    end;
 
70
  sf_close (Soundfile) ;
 
71
  fdclose (AudioDevice) ;
 
72
end;
 
73
 
 
74
Function OpenDSPDevice (channels,SampleRate : LongInt) : Longint;
 
75
 
 
76
var
 
77
 fd, stereo, temp, error : longint ;
 
78
 
 
79
begin
 
80
  fd:=fdOpen('/dev/dsp',OPEN_WRONLY,0);
 
81
  if fd<0 then
 
82
    PlayError(SErrOpeningDevice);
 
83
  Stereo:=0;
 
84
  if Not ioctl(fd, SNDCTL_DSP_STEREO  , @stereo) then
 
85
    PlayError(SErrSettingStereo);
 
86
  if Not ioctl (fd, SNDCTL_DSP_RESET, Nil) then
 
87
    PlayError(SErrResettingDevice);
 
88
  temp := 16 ;
 
89
  If not ioctl (fd, SOUND_PCM_WRITE_BITS, @temp) then
 
90
    PlayError(SErrSetWriteBits);
 
91
  If not  ioctl (fd, SOUND_PCM_WRITE_CHANNELS, @channels) then
 
92
    PlayError(SErrSetChannels);
 
93
  If Not ioctl (fd, SOUND_PCM_WRITE_RATE, @SampleRate) then
 
94
    PlayError(SErrSetSampleRate);
 
95
  If not ioctl (fd, SNDCTL_DSP_SYNC, Nil) then
 
96
    PlayError(SErrSetSyncMode);
 
97
  OpenDSPDevice:=Fd;
 
98
end;
 
99
 
 
100
Var
 
101
  I : Integer;
 
102
 
 
103
begin
 
104
  For I:=1 to ParamCount do
 
105
    PlayFile(Paramstr(i));
 
106
end.