Class: Roctave::CU8FileReader
- Inherits:
-
Object
- Object
- Roctave::CU8FileReader
- Defined in:
- lib/roctave/cu8_file_reader.rb,
ext/roctave/cu8_file_reader.c
Class Method Summary collapse
-
.open(filename) ⇒ Object
Creates a new CU8FileReader with the given file name, yields it, then close it and return the result of the block.
-
.read(filename) ⇒ Array<Float>
Read the content of the file as couples of real and imaginary unsigned chars, and return an array of complex numbers whose real and imaginary parts are floating point numbers in the range [-1.0, 1.0].
Instance Method Summary collapse
-
#close ⇒ Object
Close the opened file.
-
#closed? ⇒ Boolean
Whether the CU8FileReader has an opened file or not.
-
#eof? ⇒ Boolean?
If the end of the file has been reached,
nil
if it is closed. - #initialize(filename) ⇒ Object constructor
-
#length ⇒ Integer
The number of complex numbers in the file.
-
#normalized_position ⇒ Float
The file position indicator divided by the file length.
- #open(filename) ⇒ Object
-
#position ⇒ Integer
The file position indicator in terms of complex numbers.
- #read(*args) ⇒ Object
- #rewind ⇒ Object
Constructor Details
#initialize(filename) ⇒ Object
139 140 141 142 |
# File 'ext/roctave/cu8_file_reader.c', line 139
static VALUE cu8_file_reader_initialize(VALUE self, VALUE filename)
{
return cu8_file_reader_open(self, filename);
}
|
Class Method Details
.open(filename) ⇒ Object
Creates a new CU8FileReader with the given file name, yields it, then close it and return the result of the block.
37 38 39 40 41 42 |
# File 'lib/roctave/cu8_file_reader.rb', line 37 def self.open (filename) reader = Roctave::CU8FileReader.new(filename) res = yield(reader) reader.close return res end |
.read(filename) ⇒ Array<Float>
Read the content of the file as couples of real and imaginary unsigned chars, and return an array of complex numbers whose real and imaginary parts are floating point numbers in the range [-1.0, 1.0]
26 27 28 29 30 31 |
# File 'lib/roctave/cu8_file_reader.rb', line 26 def self.read (filename) reader = Roctave::CU8FileReader.new(filename) arr = reader.read reader.close return arr end |
Instance Method Details
#close ⇒ Object
Close the opened file
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'ext/roctave/cu8_file_reader.c', line 82
static VALUE cu8_file_reader_close(VALUE self)
{
struct cu8_file_reader *fr;
TypedData_Get_Struct(self, struct cu8_file_reader, &cu8_file_reader_type, fr);
if (fr->file != NULL) {
fclose(fr->file);
fr->file = NULL;
}
return self;
}
|
#closed? ⇒ Boolean
Returns whether the CU8FileReader has an opened file or not.
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'ext/roctave/cu8_file_reader.c', line 99
static VALUE cu8_file_reader_closed(VALUE self)
{
struct cu8_file_reader *fr;
VALUE res = Qtrue;
TypedData_Get_Struct(self, struct cu8_file_reader, &cu8_file_reader_type, fr);
if (fr->file != NULL)
res = Qfalse;
return res;
}
|
#eof? ⇒ Boolean?
Returns if the end of the file has been reached, nil
if it is closed.
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'ext/roctave/cu8_file_reader.c', line 164
static VALUE cu8_file_reader_eof(VALUE self)
{
struct cu8_file_reader *fr;
VALUE res = Qfalse;
long pos, size;
TypedData_Get_Struct(self, struct cu8_file_reader, &cu8_file_reader_type, fr);
if (fr->file == NULL)
return Qnil;
cu8_file_reader_get_size_and_position(fr, &pos, &size);
if (pos >= size)
res = Qtrue;
return res;
}
|
#length ⇒ Integer
Returns the number of complex numbers in the file.
201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'ext/roctave/cu8_file_reader.c', line 201
static VALUE cu8_file_reader_length(VALUE self)
{
struct cu8_file_reader *fr;
long size;
TypedData_Get_Struct(self, struct cu8_file_reader, &cu8_file_reader_type, fr);
if (fr->file == NULL)
return Qnil;
cu8_file_reader_get_size_and_position(fr, NULL, &size);
return LONG2NUM(size);
}
|
#normalized_position ⇒ Float
Returns the file position indicator divided by the file length.
237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'ext/roctave/cu8_file_reader.c', line 237
static VALUE cu8_file_reader_normalized_position(VALUE self)
{
struct cu8_file_reader *fr;
long pos, size;
TypedData_Get_Struct(self, struct cu8_file_reader, &cu8_file_reader_type, fr);
if (fr->file == NULL)
return Qnil;
cu8_file_reader_get_size_and_position(fr, &pos, &size);
return DBL2NUM((double)pos / (double)size);
}
|
#open(filename) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'ext/roctave/cu8_file_reader.c', line 115
static VALUE cu8_file_reader_open(VALUE self, VALUE filename)
{
struct cu8_file_reader *fr;
VALUE expanded_filename;
const char *expfname;
TypedData_Get_Struct(self, struct cu8_file_reader, &cu8_file_reader_type, fr);
cu8_file_reader_close(self);
if (rb_class_of(filename) != rb_cString)
rb_raise(rb_eArgError, "Expecting the file path as a string");
expanded_filename = rb_funcallv(rb_cFile, rb_intern("expand_path"), 1, &filename);
expfname = StringValueCStr(expanded_filename);
if ((fr->file = fopen(expfname, "rb")) == NULL)
rb_raise(rb_eRuntimeError, "Cannot open file \"%s\": %s", expfname, strerror(errno));
return self;
}
|
#position ⇒ Integer
Returns the file position indicator in terms of complex numbers.
219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'ext/roctave/cu8_file_reader.c', line 219
static VALUE cu8_file_reader_position(VALUE self)
{
struct cu8_file_reader *fr;
long pos;
TypedData_Get_Struct(self, struct cu8_file_reader, &cu8_file_reader_type, fr);
if (fr->file == NULL)
return Qnil;
cu8_file_reader_get_size_and_position(fr, &pos, NULL);
return LONG2NUM(pos);
}
|
#read ⇒ Array<Float> #read(len) ⇒ Array<Float>
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 |
# File 'ext/roctave/cu8_file_reader.c', line 260
static VALUE cu8_file_reader_read(int argc, VALUE *argv, VALUE self)
{
struct cu8_file_reader *fr;
VALUE rblen;
long pos, size;
int len, r, to_read, readden, i;
unsigned char *bufc;
VALUE res;
rb_scan_args(argc, argv, "01", &rblen);
TypedData_Get_Struct(self, struct cu8_file_reader, &cu8_file_reader_type, fr);
if (fr->file == NULL)
return Qnil;
cu8_file_reader_get_size_and_position(fr, &pos, &size);
len = (int)(size - pos);
if (rblen != Qnil) {
if (NUM2INT(rblen) < 0)
rb_raise(rb_eArgError, "Requested length cannot be less than 0");
if (len > NUM2INT(rblen))
len = NUM2INT(rblen);
}
if (len <= 0)
return rb_ary_new_capa(0);
bufc = malloc(len*2*sizeof(unsigned char));
if (bufc == NULL)
rb_raise(rb_eRuntimeError, "Failed to allocate %d bytes for internal buffer", len);
to_read = len*2;
readden = 0;
do {
r = fread(bufc + readden, sizeof(unsigned char), to_read, fr->file);
if (r < 0)
rb_raise(rb_eRuntimeError, "Error reading file: %s", strerror(errno));
readden += r;
to_read -= r;
} while (to_read > 0 && r > 0);
len = readden / 2;
res = rb_ary_new_capa(len);
for (i = 0; i < len; i++)
rb_ary_store(res, i, rb_complex_raw(DBL2NUM(((double)bufc[2*i+0] - 128.0)/128.0), DBL2NUM(((double)bufc[2*i+1] - 128.0)/128.0)));
free(bufc);
return res;
}
|
#rewind ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'ext/roctave/cu8_file_reader.c', line 184
static VALUE cu8_file_reader_rewind(VALUE self)
{
struct cu8_file_reader *fr;
TypedData_Get_Struct(self, struct cu8_file_reader, &cu8_file_reader_type, fr);
if (fr->file == NULL)
return Qnil;
rewind(fr->file);
return self;
}
|