Make the midiplugin's out of memory error more obvious to people who don't know what...
[kugel-rb.git] / apps / plugins / midi / midiutil.c
blobe10c593dc147334bf1889231809e3a7c3b4ad065
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Stepan Moskovchenko
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "plugin.h"
22 #include "midiutil.h"
24 int chVol[16] IBSS_ATTR; /* Channel volume */
25 int chPan[16] IBSS_ATTR; /* Channel panning */
26 int chPat[16] IBSS_ATTR; /* Channel patch */
27 int chPW[16] IBSS_ATTR; /* Channel pitch wheel, MSB only */
28 int chPBDepth[16] IBSS_ATTR; /* Channel pitch bend depth */
29 int chPBNoteOffset[16] IBSS_ATTR; /* Pre-computed whole semitone offset */
30 int chPBFractBend[16] IBSS_ATTR; /* Fractional bend applied to delta */
31 unsigned char chLastCtrlMSB[16]; /* MIDI regs, used for Controller 6. */
32 unsigned char chLastCtrlLSB[16]; /* The non-registered ones are ignored */
34 struct GPatch * gusload(char *);
35 struct GPatch * patchSet[128];
36 struct GPatch * drumSet[128];
38 struct SynthObject voices[MAX_VOICES] IBSS_ATTR;
40 void *alloc(int size)
42 static char *offset = NULL;
43 static size_t totalSize = 0;
44 char *ret;
46 int remainder = size % 4;
48 size = size + 4-remainder;
50 if (offset == NULL)
52 offset = rb->plugin_get_audio_buffer(&totalSize);
55 if (size + 4 > (int)totalSize)
57 midi_debug("Out of Memory");
58 midi_debug("MALLOC BARF");
59 midi_debug("MALLOC BARF");
60 midi_debug("MALLOC BARF");
61 midi_debug("MALLOC BARF");
62 midi_debug("MALLOC BARF");
63 /* We've made our point. */
65 return NULL;
68 ret = offset + 4;
69 *((unsigned int *)offset) = size;
71 offset += size + 4;
72 totalSize -= size + 4;
73 return ret;
76 /* Rick's code */
78 void *alloc(int size)
80 static char *offset = NULL;
81 static ssize_t totalSize = 0;
82 char *ret;
85 if (offset == NULL)
87 offset = rb->plugin_get_audio_buffer((size_t *)&totalSize);
90 if (size + 4 > totalSize)
92 return NULL;
95 ret = offset + 4;
96 *((unsigned int *)offset) = size;
98 offset += size + 4;
99 totalSize -= size + 4;
100 return ret;
104 #define malloc(n) my_malloc(n)
105 void * my_malloc(int size)
107 return alloc(size);
110 unsigned char readChar(int file)
112 char buf[2];
113 rb->read(file, &buf, 1);
114 return buf[0];
117 unsigned char * readData(int file, int len)
119 unsigned char * dat = malloc(len);
120 rb->read(file, dat, len);
121 return dat;
124 int eof(int fd)
126 int curPos = rb->lseek(fd, 0, SEEK_CUR);
128 int size = rb->lseek(fd, 0, SEEK_END);
130 rb->lseek(fd, curPos, SEEK_SET);
131 return size+1 == rb->lseek(fd, 0, SEEK_CUR);
134 /* Here is a hacked up printf command to get the output from the game. */
135 int midi_debug(const char *fmt, ...)
137 static int p_xtpt = 0;
138 char p_buf[50];
139 bool ok;
140 va_list ap;
142 va_start(ap, fmt);
143 ok = rb->vsnprintf(p_buf,sizeof(p_buf), fmt, ap);
144 va_end(ap);
146 int i=0;
148 /* Device LCDs display newlines funny. */
149 for(i=0; p_buf[i]!=0; i++)
150 if(p_buf[i] == '\n')
151 p_buf[i] = ' ';
153 rb->lcd_putsxy(1,p_xtpt, (unsigned char *)p_buf);
154 rb->lcd_update();
156 p_xtpt+=8;
157 if(p_xtpt>LCD_HEIGHT-8)
159 p_xtpt=0;
160 rb->lcd_clear_display();
162 return 1;