Class: AudioToolbox::MusicTrack

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/music_player.rb,
ext/music_player/music_player.c

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObject



429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# File 'ext/music_player/music_player.c', line 429

static VALUE
track_init (int argc, VALUE *argv, VALUE self)
{
    VALUE rb_seq, rb_options;
    rb_scan_args(argc, argv, "11", &rb_seq, &rb_options);
    
    rb_iv_set(self, "@sequence", rb_seq);
    
    if (T_HASH == TYPE(rb_options)) {
        VALUE loop_info = rb_hash_aref(rb_options, rb_sLoopInfo),
              mute      = rb_hash_aref(rb_options, rb_sMute),
              solo      = rb_hash_aref(rb_options, rb_sSolo),
              length    = rb_hash_aref(rb_options, rb_sLength);
        
        if (!NIL_P(loop_info))
            rb_funcall(self, rb_intern("loop_info="), 1, loop_info);
        if (!NIL_P(mute))
            rb_funcall(self, rb_intern("mute="), 1, mute);
        if (!NIL_P(solo))
            rb_funcall(self, rb_intern("solo="), 1, solo);
        if (!NIL_P(length))
            rb_funcall(self, rb_intern("length="), 1, length);
    }
    
    return self;
}

Class Method Details

.newObject



466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
# File 'ext/music_player/music_player.c', line 466

static VALUE
track_new (int argc, VALUE *argv, VALUE class)
{
    VALUE rb_seq, rb_options, rb_track, init_argv[2];
    MusicSequence *seq;
    MusicTrack *track;
    OSStatus err;
    
    rb_scan_args(argc, argv, "11", &rb_seq, &rb_options);
    Data_Get_Struct(rb_seq, MusicSequence, seq);
    
    rb_track = Data_Make_Struct(rb_cMusicTrack, MusicTrack, 0, track_free, track);
    require_noerr( err = MusicSequenceNewTrack(*seq, track), fail );
    init_argv[0] = rb_seq;
    init_argv[1] = rb_options;
    rb_obj_call_init(rb_track, 2, init_argv);
    return rb_track;

    fail:
    RAISE_OSSTATUS(err, "MusicSequenceNewTrack()");
}

Instance Method Details

#add(time, message) ⇒ Object



64
65
66
# File 'lib/music_player.rb', line 64

def add(time, message)
  message.add(time, self)
end

#add_extended_tempo_eventObject



522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
# File 'ext/music_player/music_player.c', line 522

static VALUE
track_add_extended_tempo_event (VALUE self, VALUE rb_at, VALUE rb_bpm)
{
    MusicTrack *track;
    MusicTimeStamp ts;
    Float64 bpm;
    OSStatus err;
    
    Data_Get_Struct(self, MusicTrack, track);
    
    if (PRIM_NUM_P(rb_at))
        ts = NUM2DBL(rb_at);
    else
        rb_raise(rb_eArgError, "Expected first arg to be a number.");
    
    if (PRIM_NUM_P(rb_bpm))
        bpm = NUM2DBL(rb_bpm);
    else
        rb_raise(rb_eArgError, "Expected second arg to be a number.");
    
    require_noerr( err = MusicTrackNewExtendedTempoEvent(*track, ts, bpm), fail );
    return Qnil;
    
    fail:
    RAISE_OSSTATUS(err, "MusicTrackNewExtendedTempoEvent()");
}

#add_midi_channel_messageObject



505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# File 'ext/music_player/music_player.c', line 505

static VALUE
track_add_midi_channel_message (VALUE self, VALUE rb_at, VALUE rb_msg)
{
    MusicTrack *track;
    MIDIChannelMessage *msg;
    MusicTimeStamp ts = (MusicTimeStamp) NUM2DBL(rb_at);
    OSStatus err;
    
    Data_Get_Struct(self, MusicTrack, track);
    Data_Get_Struct(rb_msg, MIDIChannelMessage, msg);
    require_noerr( err = MusicTrackNewMIDIChannelEvent(*track, ts, msg), fail );
    return Qnil;
    
    fail:
    RAISE_OSSTATUS(err, "MusicTrackNewMIDIChannelEvent()");
}

#add_midi_note_messageObject



488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# File 'ext/music_player/music_player.c', line 488

static VALUE
track_add_midi_note_message (VALUE self, VALUE rb_at, VALUE rb_msg)
{
    MusicTrack *track;
    MIDINoteMessage *msg;
    MusicTimeStamp ts = (MusicTimeStamp) NUM2DBL(rb_at);
    OSStatus err;
    
    Data_Get_Struct(self, MusicTrack, track);
    Data_Get_Struct(rb_msg, MIDINoteMessage, msg);
    require_noerr( err = MusicTrackNewMIDINoteEvent(*track, ts, msg), fail );
    return Qnil;

    fail:
    RAISE_OSSTATUS(err, "MusicTrackNewMIDINoteEvent()");
}

#eachObject



74
75
76
77
78
79
80
81
# File 'lib/music_player.rb', line 74

def each
  i = iterator
  while i.current?
    yield i.event
    i.next
  end
  nil
end

#each_with_timeObject



83
84
85
86
87
88
89
90
# File 'lib/music_player.rb', line 83

def each_with_time
  i = iterator
  while i.current?
    yield [i.event, i.time]
    i.next
  end
  nil
end

#iteratorObject



68
69
70
# File 'lib/music_player.rb', line 68

def iterator
  MusicEventIterator.new(self)
end

#lengthObject



713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
# File 'ext/music_player/music_player.c', line 713

static VALUE
track_get_length (VALUE self)
{
    MusicTrack *track;
    MusicTimeStamp length;
    UInt32 sz;
    OSStatus err;
    Data_Get_Struct(self, MusicTrack, track);
    
    require_noerr(
        err = MusicTrackGetProperty(*track, kSequenceTrackProperty_TrackLength,
                                    &length, &sz),
        fail);
    
    if (sz == sizeof(MusicTimeStamp))
        return rb_float_new(length);
    else
        return Qnil;
    
    fail:
    RAISE_OSSTATUS(err, "MusicTrackGetProperty()");
}

#length=Object



736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
# File 'ext/music_player/music_player.c', line 736

static VALUE
track_set_length (VALUE self, VALUE rb_length)
{
    if (!PRIM_NUM_P(rb_length))
        rb_raise(rb_eTypeError, "Expected length to be a number.");
    MusicTrack *track;
    MusicTimeStamp length = NUM2DBL(rb_length);
    OSStatus err;
    Data_Get_Struct(self, MusicTrack, track);
    
    require_noerr(
        err = MusicTrackSetProperty(*track, kSequenceTrackProperty_TrackLength,
                                    &length, sizeof(MusicTimeStamp)),
        fail);
    
    return Qnil;
    
    fail:
    RAISE_OSSTATUS(err, "MusicTrackGetProperty()");
}

#loop_infoObject



549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
# File 'ext/music_player/music_player.c', line 549

static VALUE
track_get_loop_info (VALUE self)
{
    MusicTrack *track;
    UInt32 sz;
    MusicTrackLoopInfo loop_info;
    OSStatus err;
    Data_Get_Struct(self, MusicTrack, track);
    require_noerr( err = MusicTrackGetProperty(*track, kSequenceTrackProperty_LoopInfo, &loop_info, &sz), fail );

    if (sz == sizeof(MusicTrackLoopInfo)) {
        VALUE rb_loop_info = rb_hash_new();
        rb_hash_aset(rb_loop_info, rb_sDuration, rb_float_new(loop_info.loopDuration));
        rb_hash_aset(rb_loop_info, rb_sNumber, INT2NUM(loop_info.numberOfLoops));
        return rb_loop_info;
    } else {
        return Qnil;
    }
    
    fail:
    RAISE_OSSTATUS(err, "MusicTrackGetProperty()");
}

#loop_info=Object



572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
# File 'ext/music_player/music_player.c', line 572

static VALUE
track_set_loop_info (VALUE self, VALUE rb_loop_info)
{
    Check_Type(rb_loop_info, T_HASH);
    MusicTrack *track;
    MusicTrackLoopInfo loop_info;
    OSStatus err;
    Data_Get_Struct(self, MusicTrack, track);
    loop_info.loopDuration = NUM2DBL(rb_hash_aref(rb_loop_info, rb_sDuration));
    loop_info.numberOfLoops = NUM2DBL(rb_hash_aref(rb_loop_info, rb_sNumber));
    
    require_noerr(
        err = MusicTrackSetProperty(*track, kSequenceTrackProperty_LoopInfo,
                                    &loop_info, sizeof(MusicTrackLoopInfo)),
        fail);
    
    return Qnil;
    
    fail:
    RAISE_OSSTATUS(err, "MusicTrackSetProperty()");
}

#muteObject



634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
# File 'ext/music_player/music_player.c', line 634

static VALUE
track_get_mute (VALUE self)
{
    MusicTrack *track;
    UInt32 sz;
    Boolean status;
    OSStatus err;
    Data_Get_Struct(self, MusicTrack, track);
    require_noerr( err = MusicTrackGetProperty(*track, kSequenceTrackProperty_MuteStatus, &status, &sz), fail );
    
    if (sz == sizeof(Boolean))
        return status ? Qtrue : Qfalse;
    else
        return Qnil;
    
    fail:
    RAISE_OSSTATUS(err, "MusicTrackGetProperty()");
}

#mute=Object



653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
# File 'ext/music_player/music_player.c', line 653

static VALUE
track_set_mute (VALUE self, VALUE rb_status)
{
    MusicTrack *track;
    Boolean status = RTEST(rb_status);
    OSStatus err;
    Data_Get_Struct(self, MusicTrack, track);
    
    require_noerr(
        err = MusicTrackSetProperty(*track, kSequenceTrackProperty_MuteStatus,
                                    &status, sizeof(Boolean)),
        fail);
    
    return Qnil;
    
    fail:
    RAISE_OSSTATUS(err, "MusicTrackSetProperty()");
}

#offsetObject



594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
# File 'ext/music_player/music_player.c', line 594

static VALUE
track_get_offset (VALUE self)
{
    MusicTrack *track;
    UInt32 sz;
    MusicTimeStamp offset;
    OSStatus err;
    Data_Get_Struct(self, MusicTrack, track);
    require_noerr( err = MusicTrackGetProperty(*track, kSequenceTrackProperty_OffsetTime, &offset, &sz), fail );
    
    if (sz == sizeof(MusicTimeStamp))
        return rb_float_new(offset);
    else
        return Qnil;
    
    fail:
    RAISE_OSSTATUS(err, "MusicTrackGetProperty()");
}

#offset=Object



613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
# File 'ext/music_player/music_player.c', line 613

static VALUE
track_set_offset (VALUE self, VALUE rb_offset)
{
    if (!PRIM_NUM_P(rb_offset))
        rb_raise(rb_eTypeError, "Expected offset to be a number.");
    MusicTrack *track;
    MusicTimeStamp offset = NUM2DBL(rb_offset);
    OSStatus err;
    Data_Get_Struct(self, MusicTrack, track);
    
    require_noerr(
        err = MusicTrackSetProperty(*track, kSequenceTrackProperty_OffsetTime,
                                    &offset, sizeof(MusicTimeStamp)),
        fail);
    
    return Qnil;
    
    fail:
    RAISE_OSSTATUS(err, "MusicTrackSetProperty()");
}

#resolutionObject



757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
# File 'ext/music_player/music_player.c', line 757

static VALUE
track_get_resolution (VALUE self)
{
    MusicTrack *track;
    SInt16 res;
    UInt32 sz;
    OSStatus err;
    Data_Get_Struct(self, MusicTrack, track);
    
    require_noerr(
        err = MusicTrackGetProperty(*track, kSequenceTrackProperty_TimeResolution, &res, &sz),
        fail);
    
    if (sz == sizeof(SInt16))
        return INT2FIX(res);
    else
        return Qnil;

    fail:
    if (paramErr == err)
        rb_raise(rb_eArgError, "Resolution is only available to the tempo track.");
    else
        RAISE_OSSTATUS(err, "MusicTrackGetProperty()");
}

#soloObject



672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
# File 'ext/music_player/music_player.c', line 672

static VALUE
track_get_solo (VALUE self)
{
    MusicTrack *track;
    UInt32 sz;
    Boolean status;
    OSStatus err;
    Data_Get_Struct(self, MusicTrack, track);
    require_noerr(
        err = MusicTrackGetProperty(*track, kSequenceTrackProperty_SoloStatus,
                                    &status, &sz),
        fail );
    
    if (sz == sizeof(Boolean))
        return status ? Qtrue : Qfalse;
    else
        return Qnil;
    
    fail:
    RAISE_OSSTATUS(err, "MusicTrackGetProperty()");
}

#solo=Object



694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
# File 'ext/music_player/music_player.c', line 694

static VALUE
track_set_solo (VALUE self, VALUE rb_status)
{
    MusicTrack *track;
    Boolean status = RTEST(rb_status);
    OSStatus err;
    Data_Get_Struct(self, MusicTrack, track);
    
    require_noerr(
        err = MusicTrackSetProperty(*track, kSequenceTrackProperty_SoloStatus,
                                    &status, sizeof(Boolean)),
        fail);
    
    return Qnil;
    
    fail:
    RAISE_OSSTATUS(err, "MusicTrackSetProperty()");
}