Skip to content

Commit

Permalink
Merge pull request #41 from DhrBaksteen/Fixes_for_1.4.4
Browse files Browse the repository at this point in the history
Fixes for 1.4.4
  • Loading branch information
DhrBaksteen authored Feb 25, 2019
2 parents 4d1792f + 97afc1f commit 2c6496c
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 95 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This repository contains the OPL2 Audio Board library for Arduino, Teensy, Raspb
* Use the board as a MIDI synthesizer (Teensy++ 2.0 and later example included)
* Emulation with DosBox; you can use the board to output MIDI music (Teensy++ 2.0 and later)

Current library version is 1.4.4
Current library version is 1.4.5

To obtain your own OPL2 Audio Board visit the [Tindie store](https://www.tindie.com/products/DhrBaksteen/opl2-audio-board/).

Expand Down
4 changes: 2 additions & 2 deletions build
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ echo "\033[1;36m / | \\ | / /_/ | | ( /_/ ) | | ( /_/ ) __ \\|
echo "\033[1;34m \\____|__ /____/\\____ | |__|\\___/ |______ /\\___(____ /__| \\____ | "
echo "\033[1;34m \\/ \\/ \\/ \\/ \\/ \033[0m"
echo "Installation script for Raspberry Pi and compatibles"
echo "Library version 1.4.4, 18th of November 2018"
echo "Copyright (c) 2016-2018 Maarten Janssen, Cheerful"
echo "Library version 1.4.5, 25th of February 2019"
echo "Copyright (c) 2016-2019 Maarten Janssen, Cheerful"
echo ""

echo -n "Checking for SPI devices... "
Expand Down
7 changes: 5 additions & 2 deletions examples/PlayDRO/PlayDRO.ino
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,13 @@ void loop() {

while (songLength > 0) {
int wait = playDroSong();
wait -= (millis() - time); // Take into account time that was spent on IO.

if (wait > 0) {
delay(wait);
// Take into account time that was spent on IO.
unsigned long ioTime = millis() - time;
if (ioTime < wait) {
delay(wait - ioTime);
}
time = millis();
}

Expand Down
7 changes: 5 additions & 2 deletions examples/PlayIMF/PlayIMF.ino
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,13 @@ void loop() {

while (songLength > 0) {
int wait = playImfSong();
wait -= (millis() - time); // Take into account time that was spent on IO.

if (wait > 0) {
delay(wait);
// Take into account time that was spent on IO.
unsigned long ioTime = millis() - time;
if (ioTime < wait) {
delay(wait - ioTime);
}
time = millis();
}

Expand Down
7 changes: 5 additions & 2 deletions examples/Teensy/PlayDRO/PlayDRO.ino
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,13 @@ void loop() {

while (songLength > 0) {
int wait = playDroSong();
wait -= (millis() - time); // Take into account time that was spent on IO.

if (wait > 0) {
delay(wait);
// Take into account time that was spent on IO.
unsigned long ioTime = millis() - time;
if (ioTime < wait) {
delay(wait - ioTime);
}
time = millis();
}

Expand Down
7 changes: 5 additions & 2 deletions examples/Teensy/PlayIMF/PlayIMF.ino
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,13 @@ void loop() {

while (songLength > 0) {
int wait = playImfSong();
wait -= (millis() - time); // Take into account time that was spent on IO.

if (wait > 0) {
delay(wait);
// Take into account time that was spent on IO.
unsigned long ioTime = millis() - time;
if (ioTime < wait) {
delay(wait - ioTime);
}
time = millis();
}

Expand Down
157 changes: 79 additions & 78 deletions examples_pi/opl2play/opl2play.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ int main(int argc, char **argv) {
FILE *tempFile = fopen("./temp.vgm", "w");
int result = inflate(oplFile, tempFile);
fclose(tempFile);

if (result == 0) {
fclose(oplFile);
oplFile = fopen("./temp.vgm", "rb");
Expand All @@ -82,7 +82,7 @@ int main(int argc, char **argv) {
playVgmMusic(vgm);
} else {
printf("Decompression error %d\n", result);
}
}
}

fclose(oplFile);
Expand Down Expand Up @@ -123,8 +123,8 @@ void playDroMusic(DRO dro) {
if (registerCode == dro.codeShortDelay) {
delay(value);
} else if (registerCode == dro.codeLongDelay) {
delay((value) << 8);
} else {
delay(value << 8);
} else if (registerCode < 128) {
reg = dro.registerMap[registerCode];
opl2.write(reg, value);
}
Expand Down Expand Up @@ -213,17 +213,17 @@ void playVgmMusic(VGM vgm) {
for (unsigned long i = 0; i < dataSize; i ++) {
fileData[i] = fileRead(vgm.file);
}

// Play!
unsigned long offset = 0;
unsigned char command;
unsigned char reg;
unsigned char data;
unsigned int sampleDelay;

while(offset < dataSize) {
command = fileData[offset ++];

switch (command) {
// YM3812 command.
case 0x5A: {
Expand All @@ -232,27 +232,27 @@ void playVgmMusic(VGM vgm) {
opl2.write(reg, data);
break;
}

// Sample delay long.
case 0x61: {
sampleDelay = fileData[offset ++];
sampleDelay += fileData[offset ++] << 8;
delay(sampleDelay * 0.023);
break;
}

// 60 Hz delay.
case 0x62: {
delay(17);
break;
}

// 50 Hz delay.
case 0x63: {
delay(20);
break;
}

// End of song data. Stop or loop.
case 0x66: {
if (repeat && vgm.loopOffset > 0) {
Expand All @@ -262,22 +262,22 @@ void playVgmMusic(VGM vgm) {
}
break;
}

// Sample dalay short.
case 0x70 ... 0x7F: {
sampleDelay = (command % 0x0F) + 1;
delay(sampleDelay * 0.023);
break;
break;
}

// Unsupported sample
case 0x51 ... 0x59:
case 0x5B ... 0x5F: {
offset += 2;
break;
}
}

// Do we need to loop?
if (repeat && vgm.loopOffset >= 0 && vgm.loopLength > 0 && offset > vgm.loopLength) {
offset = vgm.loopOffset;
Expand All @@ -289,29 +289,29 @@ void playVgmMusic(VGM vgm) {
VGM loadVgm(FILE *vgmFile) {
VGM vgm;
vgm.file = NULL;

if (fileRead(vgmFile) == 0x56 && fileRead(vgmFile) == 0x67 && fileRead(vgmFile) == 0x6D) {
fseek(vgmFile, 0x50, SEEK_SET);
unsigned long clockSpeed = readDWord(vgmFile);

if (clockSpeed > 0) {
fseek(vgmFile, 0x18, SEEK_SET);
vgm.songLength = readDWord(vgmFile);
vgm.loopOffset = readDWord(vgmFile);
vgm.loopLength = readDWord(vgmFile);

// Calculate loop offset relative to song data offset.
if (vgm.loopOffset > 0) {
vgm.loopOffset -= 0xE4;
}

fseek(vgmFile, 0x100, SEEK_SET);
vgm.file = vgmFile;
} else {
printf("No YM3812 sample data!\n");
}
}

return vgm;
}

Expand Down Expand Up @@ -360,63 +360,63 @@ int fileError() {
the version of the library linked do not match, or Z_ERRNO if there
is an error reading or writing the files. */
int inflate(FILE *source, FILE *dest) {
int ret;
unsigned int have;
z_stream strm;
unsigned char in[CHUNK];
unsigned char out[CHUNK];
/* allocate inflate state */
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
ret = inflateInit2(&strm, 16 + MAX_WBITS);
if (ret != Z_OK)
return ret;
/* decompress until deflate stream ends or end of file */
do {
strm.avail_in = fread(in, 1, CHUNK, source);
if (ferror(source)) {
(void)inflateEnd(&strm);
return Z_ERRNO;
}
if (strm.avail_in == 0)
break;
strm.next_in = in;
/* run inflate() on input until output buffer not full */
do {
strm.avail_out = CHUNK;
strm.next_out = out;

ret = inflate(&strm, Z_NO_FLUSH);
//assert(ret != Z_STREAM_ERROR); /* state not clobbered */
switch (ret) {
case Z_NEED_DICT:
ret = Z_DATA_ERROR; /* and fall through */
case Z_DATA_ERROR:
case Z_MEM_ERROR:
(void)inflateEnd(&strm);
return ret;
}

have = CHUNK - strm.avail_out;
if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
(void)inflateEnd(&strm);
return Z_ERRNO;
}

} while (strm.avail_out == 0);
/* done when inflate() says it's done */
} while (ret != Z_STREAM_END);

/* clean up and return */
(void)inflateEnd(&strm);
return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
int ret;
unsigned int have;
z_stream strm;
unsigned char in[CHUNK];
unsigned char out[CHUNK];

/* allocate inflate state */
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
ret = inflateInit2(&strm, 16 + MAX_WBITS);
if (ret != Z_OK)
return ret;


/* decompress until deflate stream ends or end of file */
do {
strm.avail_in = fread(in, 1, CHUNK, source);
if (ferror(source)) {
(void)inflateEnd(&strm);
return Z_ERRNO;
}
if (strm.avail_in == 0)
break;
strm.next_in = in;

/* run inflate() on input until output buffer not full */
do {
strm.avail_out = CHUNK;
strm.next_out = out;

ret = inflate(&strm, Z_NO_FLUSH);
//assert(ret != Z_STREAM_ERROR); /* state not clobbered */
switch (ret) {
case Z_NEED_DICT:
ret = Z_DATA_ERROR; /* and fall through */
case Z_DATA_ERROR:
case Z_MEM_ERROR:
(void)inflateEnd(&strm);
return ret;
}

have = CHUNK - strm.avail_out;
if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
(void)inflateEnd(&strm);
return Z_ERRNO;
}

} while (strm.avail_out == 0);
/* done when inflate() says it's done */
} while (ret != Z_STREAM_END);

/* clean up and return */
(void)inflateEnd(&strm);
return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
}


Expand All @@ -429,7 +429,8 @@ void printHeader() {
printf("\033[1;36m/ | \\ | | |___/ \\ | |_> > |__/ __ \\\\___ |\n");
printf("\033[1;34m\\_______ /____| |_______ \\_______ \\ /\\ | __/|____(____ / ____|\n");
printf("\033[0;34m \\/ \\/ \\/ \\/ |__| \\/\\/ \n");
printf("\033[0mOPL2.play v1.1.0 for the OPL2 Audio Board by Maarten Janssen in 2017\n");
printf("\033[0mOPL2.play v1.1.1 for the OPL2 Audio Board\n");
printf("By Maarten Janssen in 2017 - 2019\n");
printf("Visit \033[1;34mhttp://github.com/DhrBaksteen/ArduinoOPL2\033[0m to learn more!\n\n");
}
}
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Arduino OPL2
version=1.4.4
version=1.4.5
author=Maarten Janssen <maarten@cheerful.nl>
maintainer=Maarten Janssen <maarten@cheerful.nl>
sentence=Use this library to control the OPL2 Audio Board
Expand Down
2 changes: 1 addition & 1 deletion license.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2016-2018 Maarten Janssen
Copyright (c) 2016-2019 Maarten Janssen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
Loading

0 comments on commit 2c6496c

Please sign in to comment.