aboutsummaryrefslogtreecommitdiff
path: root/src/InputFile.cpp
blob: d34e8a29a34a6d9e87da9969df6cefc750f725af (plain)
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
#include "InputFile.h"
#include <iostream.h>
#include <string.h>

InputFile::InputFile () {
  fd = 0;
  format = -1;
  recover = false;
}

InputFile::~InputFile () {
}

// returns the file type, either WAV, MP3, OGG, etc. see input.h
int InputFile::Open (const char *pathname) {
  char buf[18];
  filename = pathname;
  
  fd = open (pathname, O_RDONLY);
  
  if (fd == -1) {
    // error opening the file, no dice
    return -1;
  }
  
  int bytesread = read (fd, buf, 16);
  
  if (bytesread < 4) {
    // fill is too fucking small dude
    close (fd);
    return -1;
  }
  

  if (!strncmp (buf, ".snd", 4))
    {
      //rewind the stream
      if ((lseek (fd, 0, SEEK_SET)) == -1)
	return -1;
      return format = FORMAT_NEXT;	//, bigendian = 1;
    }
  else if (!strncmp (buf, "dns.", 4))
    {
      //rewind the stream
      if ((lseek (fd, 0, SEEK_SET)) == -1)
	return -1;
      return format = FORMAT_NEXT;	//, bigendian = 0;
    }
  else if (!strncmp (buf, "RIFF", 4))
    {
      if (bytesread < 12 || strncmp (buf + 8, "WAVE", 4))
	{
	  cout << "bad header ?" << endl;
	  return -1;
	}
      //rewind the stream
      if ((lseek (fd, 0, SEEK_SET)) == -1)
	return -1;
      return format = FORMAT_WAVE;	//, bigendian = 0;
    }
  else if (!strncmp (buf, "FORM", 4))
    {
      if (bytesread < 12 || strncmp (buf + 8, "AIFF", 4))
	return -1;	//goto badheader;
      //rewind the stream
      if ((lseek (fd, 0, SEEK_SET)) == -1)
	return -1;
      return format = FORMAT_AIFF;	//, bigendian = 1;
    }
  
  else if (!strncmp (buf, "OggS", 4)) {
    //rewind the stream
    if ((lseek (fd, 0, SEEK_SET)) == -1)
      return -1;
#ifdef READ_VORBIS
    return format = FORMAT_VORBIS;
#else 
      return -1;
#endif
    }
  
  else if (!strncmp (buf, "ID3", 3))
    {
      //rewind the stream
      if ((lseek (fd, 0, SEEK_SET)) == -1)
	return -1;
#ifdef READ_MAD
      return format = FORMAT_MAD;
#else 
      return -1;
#endif
    }
  
  else if (!strncasecmp (buf, "FLAC", 4))
    {
      // } else if( !strncasecmp(thefile+strlen(thefile)-4,".fla",4) ) {
      //rewind the stream
      if ((lseek (fd, 0, SEEK_SET)) == -1)
	return -1;
#ifdef READ_FLAC
      return format = FORMAT_FLAC;
#else 
      return -1;
#endif
    }
  else
    {
      unsigned int sync;
      sync = (unsigned char) buf[0];
      sync = sync << 3;
      sync |= ((unsigned char) buf[1] & 0xE0) >> 5;
      if (sync == 0x7FF)
	{
	  //rewind the stream
	  if ((lseek (fd, 0, SEEK_SET)) == -1)
	    return -1;
#ifdef READ_MAD
	  return format = FORMAT_MAD;
#else 
	  return -1;
#endif
	}
      else if (!strncasecmp
	       (pathname + strlen (pathname) - 4, ".mp3", 4))
	{
	  //trust that its mp3
	  //rewind the stream
	  if ((lseek (fd, 0, SEEK_SET)) == -1)
	    return -1;
	  cout << "doesnt seem like its an mp3, but if you say so" << endl;
#ifdef READ_MAD
	  return format = FORMAT_MAD;
#else 
	  return -1;
#endif  
	}
    }
  
  
  
  return -1;
  
}

int
InputFile::Close ()
{
	return close (fd);
}

int
InputFile::Read (void *buf, unsigned int count)
{
	return read (fd, buf, count);
}

long
InputFile::SeekSet (long offset)
{
	return lseek (fd, offset, SEEK_SET);
}

long
InputFile::SeekCur (long offset)
{
	return lseek (fd, offset, SEEK_CUR);
}

long
InputFile::SeekEnd (long offset)
{
	return lseek (fd, offset, SEEK_END);
}