/** * call-seq: * tracks() -> array * tracks() {|offset, length| block } * * Returns an array of <tt>[offset, length]</tt> tuples for each track. * * Offset and length are both integer values representing sectors. * If a block is given this method returns +nil+ and instead iterates over the * block calling the block with two arguments <tt>|offset, length|</tt>. * * Returns always +nil+ if no ID was yet read. The block won't be called in * this case. * * You may want to use the method track_details instead of this method to * retrieve more detailed information about the tracks. */ static VALUE mb_discid_tracks(VALUE self) { if (rb_iv_get(self, "@read") == Qfalse) return Qnil; else { DiscId *disc; /* Pointer to the disc struct */ VALUE result = rb_ary_new(); /* Array of all [offset, length] tuples */ VALUE tuple; /* Array to store one [offset, length] tuple. */ int track; /* Counter for the track number to process. */ Data_Get_Struct(self, DiscId, disc); track = discid_get_first_track_num(disc); /* First track number */ while (track <= discid_get_last_track_num(disc)) { tuple = rb_ary_new3(2, INT2FIX(discid_get_track_offset(disc, track)), INT2FIX(discid_get_track_length(disc, track)) ); if (rb_block_given_p()) rb_yield(tuple); else rb_ary_push(result, tuple); track++; } if (rb_block_given_p()) return Qnil; else return result; } }
/** * call-seq: * first_track_num() -> int or nil * * Return the number of the first track on this disc (usually 1). * * Returns +nil+ if no ID was yet read. */ static VALUE mb_discid_first_track_num(VALUE self) { if (rb_iv_get(self, "@read") == Qfalse) return Qnil; else { DiscId *disc; Data_Get_Struct(self, DiscId, disc); return INT2FIX(discid_get_first_track_num(disc)); } }
int main(int argc, char *argv[]) { int i, first_track, last_track; char *device = NULL; char time_str[14]; int sectors; DiscId *disc; disc = discid_new(); /* If we have an argument, use it as the device name */ if (argc > 1) { device = argv[1]; } else { /* this will use discid_get_default_device() internally */ device = NULL; } if (discid_read_sparse(disc, device, 0) == 0) { fprintf(stderr, "Error: %s\n", discid_get_error_msg(disc)); discid_free(disc); return 1; } printf("DiscID : %s\n", discid_get_id(disc)); printf("FreeDB DiscID : %s\n", discid_get_freedb_id(disc)); first_track = discid_get_first_track_num(disc); last_track = discid_get_last_track_num(disc); printf("First track : %d\n", first_track); printf("Last track : %d\n", last_track); sectors = discid_get_sectors(disc); sectors_to_time(sectors, ROUND_SECONDS, time_str, sizeof time_str); printf("Length : %d sectors (%s)\n", sectors, time_str); for ( i = first_track; i <= last_track; i++ ) { sectors = discid_get_track_length(disc, i); sectors_to_time(sectors, ROUND_SECONDS, time_str, sizeof time_str); printf("Track %-2d : %8d %8d (%s)\n", i, discid_get_track_offset(disc, i), sectors, time_str); } printf("Submit via : %s\n", discid_get_submission_url(disc)); discid_free(disc); return 0; }