Class: Mpg123

Inherits:
Object
  • Object
show all
Defined in:
ext/mpg123/mpg123.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(filename) ⇒ Object



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
# File 'ext/mpg123/mpg123.c', line 15

VALUE rb_mpg123_new(VALUE klass, VALUE filename) {
  int err = MPG123_OK;
  mpg123_handle *mh;
  VALUE mpg123;
  long rate;
  int channels, encoding;

  Check_Type(filename, T_STRING);

  if ((mh = mpg123_new(NULL, &err)) == NULL) {
    rb_raise(rb_eStandardError, "%s", mpg123_plain_strerror(err));
  }

  mpg123_param(mh, MPG123_ADD_FLAGS, MPG123_FORCE_FLOAT, 0.);

  if (mpg123_open(mh, (char*) RSTRING_PTR(filename)) != MPG123_OK ||
      mpg123_getformat(mh, &rate, &channels, &encoding) != MPG123_OK) {
    rb_raise(rb_eStandardError, "%s", mpg123_strerror(mh));
  }

  if (encoding != MPG123_ENC_FLOAT_32) {
    rb_raise(rb_eStandardError, "bad encoding");
  }

  mpg123_format_none(mh);
  mpg123_format(mh, rate, channels, encoding);

  VALUE new_mpg123 = Data_Wrap_Struct(rb_cMpg123, 0, cleanup, mh);
  rb_iv_set(new_mpg123, "@file", filename);
  return new_mpg123;
}

Instance Method Details

#closeObject



52
53
54
55
56
# File 'ext/mpg123/mpg123.c', line 52

VALUE rb_mpg123_close(VALUE self)
{
  mpg123_close(DATA_PTR(self));
  return self;
}

#file(obj) ⇒ Object



47
48
49
50
# File 'ext/mpg123/mpg123.c', line 47

static VALUE rb_mpg123_file(VALUE self, VALUE obj)
{
  return rb_iv_get(self, "@file");
}

#lengthObject



84
85
86
87
88
89
90
91
92
93
94
95
# File 'ext/mpg123/mpg123.c', line 84

VALUE rb_mpg123_length(VALUE self)
{
  /*
   * mpg123_length() only returns an estimated duration
   * if the song hasn't previously been scanned.
   * This can be incorrect if, for example, the song is corrupted
   * and cannot be played after a certain point.
   * Run mpg123_scan() first to get an accurate length reading.
   */
  mpg123_scan(DATA_PTR(self));
  return INT2FIX(mpg123_length(DATA_PTR(self)));
}

#read(_size) ⇒ Object



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
# File 'ext/mpg123/mpg123.c', line 58

VALUE rb_mpg123_read(VALUE self, VALUE _size)
{
  int size = FIX2INT(_size);
  float *buffer = malloc(size * sizeof(float));
  VALUE result = rb_ary_new2(size);
  mpg123_handle *mh = NULL;
  size_t done = 0;
  int i;
  int err = MPG123_OK;

  Data_Get_Struct(self, mpg123_handle, mh);
  err = mpg123_read(mh, (unsigned char *) buffer, size * sizeof(float), &done);

  if (err == MPG123_OK || err == MPG123_DONE) {
    for (i = 0; i < size; i++) {
      rb_ary_store(result, i, rb_float_new(buffer[i]));
    }
    free(buffer);
    return result;
  }
  else {
    free(buffer);
    rb_raise(rb_eStandardError, "%s", mpg123_plain_strerror(err));
  }
}

#seek(offset) ⇒ Object



117
118
119
120
# File 'ext/mpg123/mpg123.c', line 117

VALUE rb_mpg123_seek(VALUE self, VALUE offset)
{
  return INT2FIX(mpg123_seek(DATA_PTR(self), FIX2INT(offset), SEEK_SET));
}

#seek_frame(offset) ⇒ Object



122
123
124
125
# File 'ext/mpg123/mpg123.c', line 122

VALUE rb_mpg123_seek_frame(VALUE self, VALUE offset)
{
  return INT2FIX(mpg123_seek_frame(DATA_PTR(self), FIX2INT(offset), SEEK_SET));
}

#spfObject



97
98
99
100
# File 'ext/mpg123/mpg123.c', line 97

VALUE rb_mpg123_spf(VALUE self)
{
  return rb_float_new(mpg123_spf(DATA_PTR(self)));
}

#tellObject



107
108
109
110
# File 'ext/mpg123/mpg123.c', line 107

VALUE rb_mpg123_tell(VALUE self)
{
  return INT2FIX(mpg123_tell(DATA_PTR(self)));
}

#tellframeObject



112
113
114
115
# File 'ext/mpg123/mpg123.c', line 112

VALUE rb_mpg123_tellframe(VALUE self)
{
  return INT2FIX(mpg123_tellframe(DATA_PTR(self)));
}

#timeframe(seconds) ⇒ Object



127
128
129
130
# File 'ext/mpg123/mpg123.c', line 127

VALUE rb_mpg123_timeframe(VALUE self, VALUE seconds)
{
  return INT2FIX(mpg123_timeframe(DATA_PTR(self), NUM2DBL(seconds)));
}

#tpfObject



102
103
104
105
# File 'ext/mpg123/mpg123.c', line 102

VALUE rb_mpg123_tpf(VALUE self)
{
  return rb_float_new(mpg123_tpf(DATA_PTR(self)));
}