summaryrefslogtreecommitdiff
path: root/src/adplug/core/sng.cc
blob: e34d65e4fafb563364c8e05f646c25f0cad2cdb1 (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
/*
 * Adplug - Replayer for many OPL2/OPL3 audio file formats.
 * Copyright (C) 1999 - 2002 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * sng.cpp - SNG Player by Simon Peter <dn.tlp@gmx.net>
 */

#include <cstring>
#include "sng.h"

CPlayer *CsngPlayer::factory(Copl *newopl)
{
  return new CsngPlayer(newopl);
}

bool CsngPlayer::load(const std::string &filename, const CFileProvider &fp)
{
  binistream *f = fp.open(filename); if(!f) return false;
  int i;

  // load header
  f->readString(header.id, 4);
  header.length = f->readInt(2); header.start = f->readInt(2);
  header.loop = f->readInt(2); header.delay = f->readInt(1);
  header.compressed = f->readInt(1) ? true : false;

  // file validation section
  if(strncmp(header.id,"ObsM",4)) { fp.close(f); return false; }

  // load section
  header.length /= 2; header.start /= 2; header.loop /= 2;
  data = new Sdata [header.length];
  for(i = 0; i < header.length; i++) {
    data[i].val = f->readInt(1);
    data[i].reg = f->readInt(1);
  }

  rewind(0);
  fp.close(f);
  return true;
}

bool CsngPlayer::update()
{
  if(header.compressed && del) {
    del--;
    return !songend;
  }

  while(data[pos].reg) {
    opl->write(data[pos].reg, data[pos].val);
    pos++;
    if(pos >= header.length) {
      songend = true;
      pos = header.loop;
    }
  }

  if(!header.compressed)
    opl->write(data[pos].reg, data[pos].val);

  if(data[pos].val) { del = data[pos].val - 1; pos++; }
  if(pos >= header.length) { songend = true; pos = header.loop; }
  return !songend;
}

void CsngPlayer::rewind(int subsong)
{
  pos = header.start; del = header.delay; songend = false;
  opl->init(); opl->write(1,32);        // go to OPL2 mode
}