summaryrefslogtreecommitdiff
path: root/plugins/adplug/adplug/rol.cpp
blob: d0d25f7deb32d14e89c54412785d369e188e23a3 (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
/*
 * Adplug - Replayer for many OPL2/OPL3 audio file formats.
 * Copyright (C) 1999 - 2006 Simon Peter, <dn.tlp@gmx.net>, et al.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * rol.h - ROL Player by OPLx <oplx@yahoo.com>
 *
 * Visit:  http://tenacity.hispeed.com/aomit/oplx/
 */
#include <string.h>
#include "rol.h"
#include "debug.h"


int   const CrolPlayer::kSizeofDataRecord    =  30;
int   const CrolPlayer::kMaxTickBeat         =  60;
int   const CrolPlayer::kSilenceNote         = -12;
int   const CrolPlayer::kNumMelodicVoices    =  9;
int   const CrolPlayer::kNumPercussiveVoices = 11;
int   const CrolPlayer::kBassDrumChannel     =  6;
int   const CrolPlayer::kSnareDrumChannel    =  7;
int   const CrolPlayer::kTomtomChannel       =  8;
int   const CrolPlayer::kTomtomFreq          =  2;//4;
int   const CrolPlayer::kSnareDrumFreq       =  2;//kTomtomFreq + 7;
float const CrolPlayer::kDefaultUpdateTme    = 18.2f;
float const CrolPlayer::kPitchFactor         = 400.0f;

static const unsigned char drum_table[4] = {0x14, 0x12, 0x15, 0x11};

CrolPlayer::uint16 const CrolPlayer::kNoteTable[12] = 
{ 
    340, // C
    363, // C#
    385, // D
    408, // D#
    432, // E
    458, // F
    485, // F#
    514, // G
    544, // G#
    577, // A
    611, // A#
    647  // B
};

/*** public methods **************************************/

CPlayer *CrolPlayer::factory(Copl *newopl)
{
  return new CrolPlayer(newopl);
}
//---------------------------------------------------------
CrolPlayer::CrolPlayer(Copl *newopl)
: CPlayer         ( newopl )
  ,rol_header      ( NULL )
  ,mNextTempoEvent ( 0 )
  ,mCurrTick       ( 0 )
  ,mTimeOfLastNote ( 0 )
  ,mRefresh        ( kDefaultUpdateTme )
  ,bdRegister      ( 0 )
  ,n_voice_data(0)
  ,n_tempo_events(0)
  ,n_used_ins(0)
{
    int n;

    mTempoEvents = 0;
    voice_data = 0;
    ins_list = 0;

    memset(bxRegister,  0, sizeof(bxRegister) );
    memset(volumeCache, 0, sizeof(volumeCache) );
    memset(freqCache,   0, sizeof(freqCache) );

    for(n=0; n<11; n++)
      pitchCache[n]=1.0f;    
}
//---------------------------------------------------------
CrolPlayer::~CrolPlayer()
{
    if( rol_header != NULL )
    {
        delete rol_header;
        rol_header=NULL;
    }

    delete[] mTempoEvents;
    delete[] voice_data;
    delete[] ins_list;
}
//---------------------------------------------------------
bool CrolPlayer::load(const char *filename, const CFileProvider &fp)
{
    binistream *f = fp.open(filename); if(!f) return false;

    char fn[strlen(filename)+12];
    int i;
    const char * bnk_filename;

    AdPlug_LogWrite("*** CrolPlayer::load(f, \"%s\") ***\n", filename);
    strcpy(fn,filename);
    int len = strlen(fn)-1;
    for (i=len; i>=0; i--) {
        if (fn[i] == '/' || fn[i] == '\\') {
            break;
        }
    }
    strcpy(fn+i+1,"standard.bnk");
    bnk_filename = fn;
    AdPlug_LogWrite("bnk_filename = \"%s\"\n",bnk_filename);

    rol_header = new SRolHeader;
    memset( rol_header, 0, sizeof(SRolHeader) );

    rol_header->version_major = f->readInt( 2 );
    rol_header->version_minor = f->readInt( 2 );

    // Version check
    if(rol_header->version_major != 0 || rol_header->version_minor != 4) {
      AdPlug_LogWrite("Unsupported file version %d.%d or not a ROL file!\n",
	       rol_header->version_major, rol_header->version_minor);
      AdPlug_LogWrite("--- CrolPlayer::load ---\n");
      fp.close(f);
      return false;
    }

    f->seek( 40, binio::Add );

    rol_header->ticks_per_beat    = f->readInt( 2 );
    rol_header->beats_per_measure = f->readInt( 2 );
    rol_header->edit_scale_y      = f->readInt( 2 );
    rol_header->edit_scale_x      = f->readInt( 2 );

    f->seek( 1, binio::Add );

    rol_header->mode = f->readInt(1);

    f->seek( 90+38+15, binio::Add );

    rol_header->basic_tempo = f->readFloat( binio::Single );

    load_tempo_events( f );

    mTimeOfLastNote = 0;

    if( load_voice_data( f, bnk_filename, fp ) != true )
    {
      AdPlug_LogWrite("CrolPlayer::load_voice_data(f) failed!\n");
      AdPlug_LogWrite("--- CrolPlayer::load ---\n");

      fp.close( f );
      return false;
    }

    fp.close( f );

    rewind( 0 );
    AdPlug_LogWrite("--- CrolPlayer::load ---\n");

    return true;
}
//---------------------------------------------------------
bool CrolPlayer::update()
{
    if( mNextTempoEvent < n_tempo_events &&
        mTempoEvents[mNextTempoEvent].time == mCurrTick )
    {
        SetRefresh( mTempoEvents[mNextTempoEvent].multiplier );
        ++mNextTempoEvent;
    }


    int curr = 0;
    int end  = n_voice_data;

    while( curr < end )
    {
        UpdateVoice( curr, voice_data[curr] );
        ++curr;
    }

    ++mCurrTick;

    if( mCurrTick > mTimeOfLastNote )
    {
        return false;
    }

    return true;
    //return ( mCurrTick > mTimeOfLastNote ) ? false : true;
}
//---------------------------------------------------------
void CrolPlayer::rewind( int subsong )
{
    int curr = 0;
    while( curr < n_voice_data )
    {
        CVoiceData &voice = voice_data[curr];

        voice.Reset();
        ++curr;
    }

    memset(bxRegister,  0, sizeof(bxRegister) );
    memset(volumeCache, 0, sizeof(volumeCache) );

    bdRegister = 0;

    opl->init();        // initialize to melodic by default
    opl->write(1,0x20); // Enable waveform select (bit 5)

    if( rol_header->mode == 0 )
    {
        opl->write( 0xbd, 0x20 ); // select rhythm mode (bit 5)
        bdRegister = 0x20;

        SetFreq( kTomtomChannel,    24 );
        SetFreq( kSnareDrumChannel, 31 );
    }

    mNextTempoEvent = 0;
    mCurrTick       = 0;

    SetRefresh(1.0f);
}
//---------------------------------------------------------
inline float fmin( int const a, int const b )
{
    return static_cast<float>( a < b ? a : b );
}
//---------------------------------------------------------
void CrolPlayer::SetRefresh( float const multiplier )
{
    float const tickBeat = fmin(kMaxTickBeat, rol_header->ticks_per_beat);

    mRefresh =  (tickBeat*rol_header->basic_tempo*multiplier) / 60.0f;
}
//---------------------------------------------------------
float CrolPlayer::getrefresh()
{
    return mRefresh;
}
//---------------------------------------------------------
void CrolPlayer::UpdateVoice( int const voice, CVoiceData &voiceData )
{
    if( !voiceData.n_note_events || (voiceData.mEventStatus & CVoiceData::kES_NoteEnd) )
    {
        return; // no note data to process, don't bother doing anything.
    }

    if( !(voiceData.mEventStatus & CVoiceData::kES_InstrEnd ) &&
        voiceData.instrument_events[voiceData.next_instrument_event].time == mCurrTick )
    {
        if( voiceData.next_instrument_event < voiceData.n_instrument_events )
        {
            send_ins_data_to_chip( voice, voiceData.instrument_events[voiceData.next_instrument_event].ins_index );
            ++voiceData.next_instrument_event;
        }
        else
        {
            voiceData.mEventStatus |= CVoiceData::kES_InstrEnd;
        }
    }

    if( !(voiceData.mEventStatus & CVoiceData::kES_VolumeEnd ) &&
        voiceData.volume_events[voiceData.next_volume_event].time == mCurrTick )
    {
      SVolumeEvent const &volumeEvent = voiceData.volume_events[voiceData.next_volume_event];

        if(  voiceData.next_volume_event < voiceData.n_volume_events )
        {
            int const volume = (int)(63.0f*(1.0f - volumeEvent.multiplier));

            SetVolume( voice, volume );

            ++voiceData.next_volume_event; // move to next volume event
        }
        else
        {
            voiceData.mEventStatus |= CVoiceData::kES_VolumeEnd;
        }        
    }

    if( voiceData.mForceNote || voiceData.current_note_duration > voiceData.mNoteDuration-1 )
    {
        if( mCurrTick != 0 )
        {
            ++voiceData.current_note;
        }

        if( voiceData.current_note < voiceData.n_note_events )
        {
            SNoteEvent const &noteEvent = voiceData.note_events[voiceData.current_note];

            SetNote( voice, noteEvent.number );
            voiceData.current_note_duration = 0;
            voiceData.mNoteDuration         = noteEvent.duration;
            voiceData.mForceNote            = false;
        }
        else
        {
            SetNote( voice, kSilenceNote );
            voiceData.mEventStatus |= CVoiceData::kES_NoteEnd;
            return;
        }
    }

    if( !(voiceData.mEventStatus & CVoiceData::kES_PitchEnd ) &&
        voiceData.pitch_events[voiceData.next_pitch_event].time == mCurrTick )
    {
        if( voiceData.next_pitch_event < voiceData.n_pitch_events )
        {
            SetPitch(voice,voiceData.pitch_events[voiceData.next_pitch_event].variation);
            ++voiceData.next_pitch_event;
        }
        else
        {
            voiceData.mEventStatus |= CVoiceData::kES_PitchEnd;
        }
    }

    ++voiceData.current_note_duration;
}
//---------------------------------------------------------
void CrolPlayer::SetNote( int const voice, int const note )
{
    if( voice < kBassDrumChannel || rol_header->mode )
    {
        SetNoteMelodic( voice, note );
    }
    else
    {
        SetNotePercussive( voice, note );
    }
}
//---------------------------------------------------------
void CrolPlayer::SetNotePercussive( int const voice, int const note )
{
    int const bit_pos = 4-voice+kBassDrumChannel;

    bdRegister &= ~( 1<<bit_pos );
    opl->write( 0xbd, bdRegister );

    if( note != kSilenceNote )
    {
        switch( voice )
        {
            case kTomtomChannel:
                SetFreq( kSnareDrumChannel, note+7 );
            case kBassDrumChannel:
                SetFreq( voice, note );
                break;
        }

        bdRegister |= 1<<bit_pos;
        opl->write( 0xbd, bdRegister );
    }
}
//---------------------------------------------------------
void CrolPlayer::SetNoteMelodic( int const voice, int const note )
{
    opl->write( 0xb0+voice, bxRegister[voice] & ~0x20 );

    if( note != kSilenceNote )
    {
        SetFreq( voice, note, true );
    }
}
//---------------------------------------------------------
void CrolPlayer::SetPitch(int const voice, real32 const variation)
{
  pitchCache[voice] = variation;
  freqCache[voice] += (uint16)((((float)freqCache[voice])*(variation-1.0f)) / kPitchFactor);

  opl->write(0xa0+voice,freqCache[voice] & 0xff);
}
//---------------------------------------------------------
void CrolPlayer::SetFreq( int const voice, int const note, bool const keyOn )
{
    uint16 freq = kNoteTable[note%12] + ((note/12) << 10);
    freq += (uint16)((((float)freq)*(pitchCache[voice]-1.0f))/kPitchFactor);

    freqCache[voice] = freq;
    bxRegister[voice] = ((freq >> 8) & 0x1f);

    opl->write( 0xa0+voice, freq & 0xff );
    opl->write( 0xb0+voice, bxRegister[voice] | (keyOn ? 0x20 : 0x0) );
}
//---------------------------------------------------------
void CrolPlayer::SetVolume( int const voice, int const volume )
{
    volumeCache[voice] = (volumeCache[voice] &0xc0) | volume;

    int const op_offset = ( voice < kSnareDrumChannel || rol_header->mode ) ? 
                          op_table[voice]+3 : drum_table[voice-kSnareDrumChannel];

    opl->write( 0x40+op_offset, volumeCache[voice] );
}
//---------------------------------------------------------
void CrolPlayer::send_ins_data_to_chip( int const voice, int const ins_index )
{
    SRolInstrument &instrument = ins_list[ins_index].instrument;

    send_operator( voice, instrument.modulator, instrument.carrier );
}
//---------------------------------------------------------
void CrolPlayer::send_operator( int const voice, SOPL2Op const &modulator,  SOPL2Op const &carrier )
{
    if( voice < kSnareDrumChannel || rol_header->mode )
    {
        int const op_offset = op_table[voice];

        opl->write( 0x20+op_offset, modulator.ammulti  );
        opl->write( 0x40+op_offset, modulator.ksltl    );
        opl->write( 0x60+op_offset, modulator.ardr     );
        opl->write( 0x80+op_offset, modulator.slrr     );
        opl->write( 0xc0+voice    , modulator.fbc      );
        opl->write( 0xe0+op_offset, modulator.waveform );

        volumeCache[voice] = (carrier.ksltl & 0xc0) | volumeCache[voice] & 0x3f;

        opl->write( 0x23+op_offset, carrier.ammulti  );
        opl->write( 0x43+op_offset, volumeCache[voice]    );
        opl->write( 0x63+op_offset, carrier.ardr     );
        opl->write( 0x83+op_offset, carrier.slrr     );
//        opl->write( 0xc3+voice    , carrier.fbc      ); <- don't bother writing this.
        opl->write( 0xe3+op_offset, carrier.waveform );
    }
    else
    {
        int const op_offset = drum_table[voice-kSnareDrumChannel];

        volumeCache[voice] = (modulator.ksltl & 0xc0) | volumeCache[voice] & 0x3f;

        opl->write( 0x20+op_offset, modulator.ammulti  );
        opl->write( 0x40+op_offset, volumeCache[voice]      );
        opl->write( 0x60+op_offset, modulator.ardr     );
        opl->write( 0x80+op_offset, modulator.slrr     );
        opl->write( 0xc0+voice    , modulator.fbc      );
        opl->write( 0xe0+op_offset, modulator.waveform );
    }
}
//---------------------------------------------------------
void CrolPlayer::load_tempo_events( binistream *f )
{
    int16 const num_tempo_events = f->readInt( 2 );

    mTempoEvents = new STempoEvent[num_tempo_events];
    memset (mTempoEvents, 0, sizeof (STempoEvent) * num_tempo_events);

    for(int i=0; i<num_tempo_events; ++i)
    {
        STempoEvent event;

        event.time       = f->readInt( 2 );
        event.multiplier = f->readFloat( binio::Single );
        mTempoEvents[n_tempo_events++] = event;
    }
}
//---------------------------------------------------------
bool CrolPlayer::load_voice_data( binistream *f, const char *bnk_filename, const CFileProvider &fp )
{
    SBnkHeader bnk_header;
    memset (&bnk_header, 0, sizeof (bnk_header));
    binistream *bnk_file = fp.open( bnk_filename);

    if( bnk_file )
    {
        load_bnk_info( bnk_file, bnk_header );

        int const numVoices = rol_header->mode ? kNumMelodicVoices : kNumPercussiveVoices;
        voice_data = new CVoiceData[numVoices];

        for(int i=0; i<numVoices; ++i)
        {
            CVoiceData &voice = voice_data[n_voice_data++];

            load_note_events( f, voice );
            load_instrument_events( f, voice, bnk_file, bnk_header );
            load_volume_events( f, voice );
            load_pitch_events( f, voice );

        }

        fp.close(bnk_file);
        delete[] bnk_header.ins_name_list;

        return true;
    }

    return false;
}
//---------------------------------------------------------
void CrolPlayer::load_note_events( binistream *f, CVoiceData &voice )
{
    f->seek( 15, binio::Add );

    int16 const time_of_last_note = f->readInt( 2 );

    if( time_of_last_note != 0 )
    {
        int16 total_duration     = 0;

        do
        {
            SNoteEvent &event = voice.note_events[voice.n_note_events++];

            event.number   = f->readInt( 2 );
            event.duration = f->readInt( 2 );

            event.number += kSilenceNote; // adding -12

            total_duration += event.duration;
        } while( total_duration < time_of_last_note );

        if( time_of_last_note > mTimeOfLastNote )
        {
            mTimeOfLastNote = time_of_last_note;
        }
    }

    f->seek( 15, binio::Add );
}
//---------------------------------------------------------
void CrolPlayer::load_instrument_events( binistream *f, CVoiceData &voice,
                                         binistream *bnk_file, SBnkHeader const &bnk_header )
{
    int16 const number_of_instrument_events = f->readInt( 2 );
    voice.instrument_events = new SInstrumentEvent[number_of_instrument_events];
    memset (voice.instrument_events, 0, sizeof (SInstrumentEvent) * number_of_instrument_events);

    for(int i=0; i<number_of_instrument_events; ++i)
    {
        SInstrumentEvent &event = voice.instrument_events[voice.n_instrument_events++];
        event.time = f->readInt( 2 );
        f->readString( event.name, 9 );

	    const char * event_name = event.name;
        event.ins_index = load_rol_instrument( bnk_file, bnk_header, event_name );

        f->seek( 1+2, binio::Add );
    }

    f->seek( 15, binio::Add );
}
//---------------------------------------------------------
void CrolPlayer::load_volume_events( binistream *f, CVoiceData &voice )
{
    int16 const number_of_volume_events = f->readInt( 2 );
    voice.volume_events = new SVolumeEvent[number_of_volume_events];
    memset (voice.volume_events, 0, sizeof (SVolumeEvent) * number_of_volume_events);

    for(int i=0; i<number_of_volume_events; ++i)
    {
        SVolumeEvent &event = voice.volume_events[voice.n_volume_events++];
        event.time       = f->readInt( 2 );
        event.multiplier = f->readFloat( binio::Single );
    }

    f->seek( 15, binio::Add );
}
//---------------------------------------------------------
void CrolPlayer::load_pitch_events( binistream *f, CVoiceData &voice )
{
    int16 const number_of_pitch_events = f->readInt( 2 );
    voice.pitch_events = new SPitchEvent[number_of_pitch_events];
    memset (voice.pitch_events, 0, sizeof (SPitchEvent) * number_of_pitch_events);

    for(int i=0; i<number_of_pitch_events; ++i)
    {
        SPitchEvent &event = voice.pitch_events[voice.n_pitch_events++];
        event.time      = f->readInt( 2 );
        event.variation = f->readFloat( binio::Single );
    }
}
//---------------------------------------------------------
bool CrolPlayer::load_bnk_info( binistream *f, SBnkHeader &header )
{
  header.version_major = f->readInt(1);
  header.version_minor = f->readInt(1);
  f->readString( header.signature, 6 );

  header.number_of_list_entries_used  = f->readInt( 2 );
  header.total_number_of_list_entries = f->readInt( 2 );

  header.abs_offset_of_name_list = f->readInt( 4 );
  header.abs_offset_of_data      = f->readInt( 4 );

  f->seek( header.abs_offset_of_name_list, binio::Set );

  header.ins_name_list = new SInstrumentName[header.number_of_list_entries_used];
  memset (header.ins_name_list, 0, sizeof (SInstrumentName) * header.number_of_list_entries_used);

  for(int i=0; i<header.number_of_list_entries_used; ++i)
    {
      SInstrumentName &instrument = header.ins_name_list[header.n_ins_names++];

      instrument.index = f->readInt( 2 );
      instrument.record_used = f->readInt(1);
      f->readString( instrument.name, 9 );
    }

    // preallocate space for instruments
    ins_list = new SUsedList[header.number_of_list_entries_used * 2];
    memset (ins_list, 0, sizeof (SUsedList) * header.number_of_list_entries_used * 2);

  return true;
}
//---------------------------------------------------------
int CrolPlayer::load_rol_instrument( binistream *f, SBnkHeader const &header, const char * name )
{
    int const ins_index = get_ins_index( name );

    if( ins_index != -1 )
    {
        return ins_index;
    }

    if (n_used_ins >= header.n_ins_names * 2) {
        AdPlug_LogWrite("*** CrolPlayer::load_rol_instrument out of memory***\n");
        return -1;
    }

    // find instrument struct by name to get file pos
    int ipos;
    for (ipos = 0; ipos < header.n_ins_names; ipos++) {
        if (!stricmp (name, header.ins_name_list[ipos].name)) {
            break;
        }
    }
    if (ipos < header.n_ins_names && ipos >= 0) {
        int const seekOffs = header.abs_offset_of_data + (header.ins_name_list[ipos].index*kSizeofDataRecord);
        f->seek( seekOffs, binio::Set );
    }

    SUsedList &usedIns = ins_list[n_used_ins++];
    usedIns.name = name; // no strdup, pointing to SInstrumentEvent name

    if (ipos < header.n_ins_names && ipos >= 0) {
        read_rol_instrument( f, usedIns.instrument );
    }
    else {
        // set up default instrument data here
        memset( &usedIns.instrument, 0, sizeof(SRolInstrument) );
    }
    return n_used_ins-1;
}
//---------------------------------------------------------
int CrolPlayer::get_ins_index( const char * const &name ) const
{
    for(unsigned int i=0; i<n_used_ins; ++i)
    {
        if( stricmp(ins_list[i].name, name) == 0 )
        {
            return i;
        }
    }

    return -1;
}
//---------------------------------------------------------
void CrolPlayer::read_rol_instrument( binistream *f, SRolInstrument &ins )
{
  ins.mode = f->readInt(1);
  ins.voice_number = f->readInt(1);

  read_fm_operator( f, ins.modulator );
  read_fm_operator( f, ins.carrier );

  ins.modulator.waveform = f->readInt(1);
  ins.carrier.waveform = f->readInt(1);
}
//---------------------------------------------------------
void CrolPlayer::read_fm_operator( binistream *f, SOPL2Op &opl2_op )
{
  SFMOperator fm_op;

  fm_op.key_scale_level = f->readInt(1);
  fm_op.freq_multiplier = f->readInt(1);
  fm_op.feed_back = f->readInt(1);
  fm_op.attack_rate = f->readInt(1);
  fm_op.sustain_level = f->readInt(1);
  fm_op.sustaining_sound = f->readInt(1);
  fm_op.decay_rate = f->readInt(1);
  fm_op.release_rate = f->readInt(1);
  fm_op.output_level = f->readInt(1);
  fm_op.amplitude_vibrato = f->readInt(1);
  fm_op.frequency_vibrato = f->readInt(1);
  fm_op.envelope_scaling = f->readInt(1);
  fm_op.fm_type = f->readInt(1);

  opl2_op.ammulti = fm_op.amplitude_vibrato << 7 | fm_op.frequency_vibrato << 6 | fm_op.sustaining_sound << 5 | fm_op.envelope_scaling << 4 | fm_op.freq_multiplier;
  opl2_op.ksltl   = fm_op.key_scale_level   << 6 | fm_op.output_level;
  opl2_op.ardr    = fm_op.attack_rate       << 4 | fm_op.decay_rate;
  opl2_op.slrr    = fm_op.sustain_level     << 4 | fm_op.release_rate;
  opl2_op.fbc     = fm_op.feed_back         << 1 | (fm_op.fm_type ^ 1);
}