- Música i só amb SDL_mixer
This commit is contained in:
12
Makefile
12
Makefile
@@ -3,22 +3,22 @@ source = *.cpp ./lua/*.c
|
|||||||
|
|
||||||
windows:
|
windows:
|
||||||
@echo off
|
@echo off
|
||||||
g++ $(source) -Wall -Os -ffunction-sections -fdata-sections -Wl,--gc-sections -lmingw32 -lSDL2main -lSDL2 -o "$(executable).exe"
|
g++ $(source) -Wall -Os -ffunction-sections -fdata-sections -Wl,--gc-sections -lmingw32 -lSDL2main -lSDL2 -lSDL2_mixer -o "$(executable).exe"
|
||||||
strip -s -R .comment -R .gnu.version --strip-unneeded "$(executable).exe"
|
strip -s -R .comment -R .gnu.version --strip-unneeded "$(executable).exe"
|
||||||
|
|
||||||
windows_debug:
|
windows_debug:
|
||||||
@echo off
|
@echo off
|
||||||
g++ $(source) -D DEBUG -g -Wall -Os -lmingw32 -lSDL2main -lSDL2 -o "$(executable)_debug.exe"
|
g++ $(source) -D DEBUG -g -Wall -Os -lmingw32 -lSDL2main -lSDL2 -lSDL2_mixer -o "$(executable)_debug.exe"
|
||||||
|
|
||||||
macos:
|
macos:
|
||||||
clang++ $(source) -Wall -Os -ffunction-sections -fdata-sections -lSDL2 -o "$(executable)"
|
clang++ $(source) -Wall -Os -ffunction-sections -fdata-sections -lSDL2 -lSDL2_mixer -o "$(executable)"
|
||||||
|
|
||||||
macos_debug:
|
macos_debug:
|
||||||
clang++ $(source) -D DEBUG -g -Wall -Os -ffunction-sections -fdata-sections -lSDL2 -o "$(executable)_debug"
|
clang++ $(source) -D DEBUG -g -Wall -Os -ffunction-sections -fdata-sections -lSDL2 -lSDL2_mixer -o "$(executable)_debug"
|
||||||
|
|
||||||
linux:
|
linux:
|
||||||
g++ $(source) -Wall -Os -ffunction-sections -fdata-sections -Wl,--gc-sections -lSDL2 -o "$(executable)"
|
g++ $(source) -Wall -Os -ffunction-sections -fdata-sections -Wl,--gc-sections -lSDL2 -lSDL2_mixer -o "$(executable)"
|
||||||
strip -s -R .comment -R .gnu.version --strip-unneeded "$(executable)"
|
strip -s -R .comment -R .gnu.version --strip-unneeded "$(executable)"
|
||||||
|
|
||||||
linux_debug:
|
linux_debug:
|
||||||
g++ $(source) -D DEBUG -g -Wall -Os -ffunction-sections -fdata-sections -Wl,--gc-sections -lSDL2 -o "$(executable)_debug"
|
g++ $(source) -D DEBUG -g -Wall -Os -ffunction-sections -fdata-sections -Wl,--gc-sections -lSDL2 -lSDL2_mixer -o "$(executable)_debug"
|
||||||
|
|||||||
340
chirp.cpp
340
chirp.cpp
@@ -1,340 +0,0 @@
|
|||||||
#include "chirp.h"
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <SDL2/SDL.h>
|
|
||||||
//1 2 3 4 6 8 12 16 24 32
|
|
||||||
//f s c n b r
|
|
||||||
|
|
||||||
#define SILENCE 128
|
|
||||||
#define NOISE 129
|
|
||||||
|
|
||||||
#define AUDIO_NONE 0
|
|
||||||
#define AUDIO_PLAY 1
|
|
||||||
|
|
||||||
#define MAX_CHANNELS 5
|
|
||||||
|
|
||||||
typedef void (*waveform_t)(const uint16_t, const uint32_t, uint8_t*, const uint8_t, const uint8_t);
|
|
||||||
static waveform_t waveforms[6];
|
|
||||||
|
|
||||||
const static uint16_t lengths[10] = { 313, 625, 938, 1250, 1875, 2500, 3750, 5000, 7500, 10000 };
|
|
||||||
const static uint16_t tempos[10] = { 13230, 8820, 6615, 5292, 4410, 3780, 3308, 2940, 2646, 2406 };
|
|
||||||
const float periods[108] = { 1348.49207, 1272.80688, 1201.37, 1133.94214, 1070.29871, 1010.22772, 953.527893, 900.010376, 849.496887, 801.818176, 756.815613, 714.338745, 674.246033, 636.403564, 600.684875, 566.971069, 535.149475, 505.11377, 476.763947, 450.005249, 424.748352, 400.909088, 378.407806, 357.169373, 337.123016, 318.201782, 300.342438, 283.485535, 267.574738, 252.556885, 238.381973, 225.002625, 212.374176, 200.454544, 189.203888, 178.584702, 168.561508, 159.100876, 150.171234, 141.742767, 133.787354, 126.278458, 119.190987, 112.501305, 106.187096, 100.227272, 94.6019516, 89.2923508, 84.2807541, 79.5504379, 75.0856171, 70.8713837, 66.8936768, 63.139225, 59.5954933, 56.2506561, 53.0935478, 50.113636, 47.3009758, 44.6461754, 42.140377, 39.775219, 37.5428085, 35.4356918, 33.4468384, 31.5696125, 29.7977467, 28.1253281, 26.5467739, 25.056818, 23.650486, 22.3230877, 21.0701885, 19.8876095, 18.7714043, 17.7178459, 16.7234192, 15.7848072, 14.8988733, 14.0626631, 13.273387, 12.528409, 11.8252439, 11.1615429, 10.5350943, 9.94380569, 9.38570118, 8.85892296, 8.36171055, 7.89240265, 7.44943666, 7.03133202, 6.636693, 6.2642045, 5.91262197, 5.58077145, 5.26754713, 4.97190285, 4.69285059, 4.42946148, 4.18085527, 3.94620132, 3.72471833, 3.51566601, 3.3183465, 3.13210225, 2.95631051, 2.7903862 };
|
|
||||||
|
|
||||||
SDL_AudioDeviceID audio_device;
|
|
||||||
uint8_t audio_state = AUDIO_NONE;
|
|
||||||
|
|
||||||
#define RELATIVE 0
|
|
||||||
#define SEQUENTIAL 1
|
|
||||||
|
|
||||||
struct instrument_t {
|
|
||||||
uint8_t waveform = 0;
|
|
||||||
int8_t volume[8] = {0,0,0,0,0,0,0,0};
|
|
||||||
uint8_t volume_data = RELATIVE;
|
|
||||||
int8_t pitch[8] = {0,0,0,0,0,0,0,0};
|
|
||||||
uint8_t pitch_data = RELATIVE;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct channel_t {
|
|
||||||
char* song = NULL;
|
|
||||||
char* song_ptr = NULL;
|
|
||||||
char* song_start = NULL;
|
|
||||||
uint32_t count = 0;
|
|
||||||
float length = 0.25f;
|
|
||||||
uint8_t volume = 32;
|
|
||||||
uint8_t octave = 4;
|
|
||||||
uint32_t tempo = 44100;
|
|
||||||
uint8_t waveform = 0;
|
|
||||||
uint8_t instrument = 0;
|
|
||||||
Uint8* play_pos;
|
|
||||||
int32_t play_len;
|
|
||||||
Uint8 play_buffer[132300];
|
|
||||||
char* stack[10] = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
|
|
||||||
uint8_t stackpos = 0;
|
|
||||||
char* labels[10] = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
|
|
||||||
instrument_t instruments[10];
|
|
||||||
};
|
|
||||||
static channel_t channels[MAX_CHANNELS];
|
|
||||||
|
|
||||||
void chirp_stop_channel(const int c) {
|
|
||||||
if (channels[c].song != NULL) {
|
|
||||||
free(channels[c].song);
|
|
||||||
channels[c].song=NULL;
|
|
||||||
}
|
|
||||||
for (int i=0;i<MAX_CHANNELS;++i) if (channels[i].song != NULL) return;
|
|
||||||
|
|
||||||
audio_state=AUDIO_NONE;
|
|
||||||
}
|
|
||||||
|
|
||||||
void audioCallback(void * userdata, uint8_t * stream, int len) {
|
|
||||||
SDL_memset(stream, 0, len);
|
|
||||||
if (audio_state == AUDIO_PLAY) {
|
|
||||||
for (int i=0;i<MAX_CHANNELS;++i) if (channels[i].song != NULL) {
|
|
||||||
int l_len=len;
|
|
||||||
uint8_t *l_stream=stream;
|
|
||||||
while( l_len > 0 ) {
|
|
||||||
while( channels[i].play_len == 0 ) {
|
|
||||||
channels[i].play_len = interpret_next_token(i);
|
|
||||||
|
|
||||||
if (channels[i].play_len == -1) {
|
|
||||||
chirp_stop_channel(i);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
channels[i].play_pos = channels[i].play_buffer;
|
|
||||||
}
|
|
||||||
if (channels[i].play_len == -1) break;
|
|
||||||
|
|
||||||
const int actual_len = ( l_len > channels[i].play_len ? channels[i].play_len : l_len );
|
|
||||||
for (int j=0;j<actual_len;++j) l_stream[j] += channels[i].play_pos[j];
|
|
||||||
|
|
||||||
l_stream += actual_len;
|
|
||||||
channels[i].play_pos += actual_len;
|
|
||||||
channels[i].play_len -= actual_len;
|
|
||||||
l_len -= actual_len;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#define COUNT (channels[c].count++ % period)
|
|
||||||
|
|
||||||
void square_waveform(const uint16_t period, const uint32_t length, uint8_t* buffer, const uint8_t volume, const uint8_t c) {
|
|
||||||
for( uint32_t i = 0; i < length; i++ ) buffer[i] = ( COUNT < (period >> 1) ? volume : -volume );
|
|
||||||
}
|
|
||||||
void saw_waveform(const uint16_t period, const uint32_t length, uint8_t* buffer, const uint8_t volume, const uint8_t c) {
|
|
||||||
for( uint32_t i = 0; i < length; i++ ) buffer[i] = -volume + uint16_t( float(COUNT) / float(period) * volume*2 );
|
|
||||||
}
|
|
||||||
void triangle_waveform(const uint16_t period, const uint32_t length, uint8_t* buffer, const uint8_t volume, const uint8_t c) {
|
|
||||||
for( uint32_t i = 0; i < length; i++ ) {
|
|
||||||
uint16_t pos = COUNT;
|
|
||||||
uint16_t half_period = period >> 1;
|
|
||||||
if (pos < half_period) {
|
|
||||||
buffer[i] = -volume + uint16_t( (float(pos) / float(half_period)) * float(volume*2) );
|
|
||||||
} else {
|
|
||||||
buffer[i] = volume - uint16_t( (float(pos-half_period) / float(half_period)) * float(volume*2) );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
void pulse12_waveform(const uint16_t period, const uint32_t length, uint8_t* buffer, const uint8_t volume, const uint8_t c) {
|
|
||||||
for( uint32_t i = 0; i < length; i++ ) buffer[i] = ( COUNT < (period >> 3) ? volume : -volume );
|
|
||||||
}
|
|
||||||
void pulse25_waveform(const uint16_t period, const uint32_t length, uint8_t* buffer, const uint8_t volume, const uint8_t c) {
|
|
||||||
for( uint32_t i = 0; i < length; i++ ) buffer[i] = ( COUNT < (period >> 2) ? volume : -volume );
|
|
||||||
}
|
|
||||||
void noise_waveform(const uint16_t period, const uint32_t length, uint8_t* buffer, const uint8_t volume, const uint8_t c) {
|
|
||||||
for( uint32_t i = 0; i < length; i++ ) buffer[i] = rand()%2==0 ? volume : -volume;
|
|
||||||
}
|
|
||||||
|
|
||||||
void chirp_init() {
|
|
||||||
SDL_AudioSpec audioSpec = {22050, AUDIO_S8, 1, 0, 512, 0, 0, audioCallback, NULL};
|
|
||||||
audio_device = SDL_OpenAudioDevice(NULL, 0, &audioSpec, NULL, 0);
|
|
||||||
SDL_PauseAudioDevice(audio_device, 0);
|
|
||||||
waveforms[0] = &square_waveform;
|
|
||||||
waveforms[1] = &saw_waveform;
|
|
||||||
waveforms[2] = &triangle_waveform;
|
|
||||||
waveforms[3] = &pulse12_waveform;
|
|
||||||
waveforms[4] = &pulse25_waveform;
|
|
||||||
waveforms[5] = &noise_waveform;
|
|
||||||
for (uint8_t i=0;i<MAX_CHANNELS;++i) channels[i].song=NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t interpret_note(const int c, const uint8_t note, const char param ) {
|
|
||||||
const uint32_t length = ( param == -1 ? channels[c].length : ((float)lengths[uint8_t(param)])/10000.0f ) * channels[c].tempo;
|
|
||||||
if( note == SILENCE ) { memset( channels[c].play_buffer, 0, length ); return length; }
|
|
||||||
//if( note == NOISE ) { for( uint32_t i = 0; i < length; i++ ) channels[c].play_buffer[i] = rand()%2==0 ? channels[c].volume : -channels[c].volume; return length; }
|
|
||||||
|
|
||||||
uint32_t l = 0;
|
|
||||||
uint8_t envelope_pos=0;
|
|
||||||
uint8_t* buffer = channels[c].play_buffer;
|
|
||||||
uint32_t min_len = uint32_t(0.0313f * channels[c].tempo);
|
|
||||||
uint8_t fullnote = note + channels[c].octave*12;
|
|
||||||
uint8_t fullvolume = channels[c].volume;
|
|
||||||
while (l<length) {
|
|
||||||
uint8_t actualnote = fullnote + channels[c].instruments[channels[c].instrument].pitch[envelope_pos];
|
|
||||||
if (channels[c].instruments[channels[c].instrument].pitch_data==SEQUENTIAL) fullnote = actualnote;
|
|
||||||
const uint16_t period = periods[actualnote];
|
|
||||||
|
|
||||||
uint8_t actualvolume = fullvolume + (channels[c].instruments[channels[c].instrument].volume[envelope_pos] << 3);
|
|
||||||
if (channels[c].instruments[channels[c].instrument].volume_data==SEQUENTIAL) fullvolume = actualvolume;
|
|
||||||
|
|
||||||
waveforms[channels[c].instruments[channels[c].instrument].waveform](period, min_len, buffer, actualvolume, c); //channels[c].play_buffer, channels[c].volume);
|
|
||||||
l+=min_len;
|
|
||||||
envelope_pos=(envelope_pos+1)&7;
|
|
||||||
buffer += min_len;
|
|
||||||
}
|
|
||||||
return length;
|
|
||||||
}
|
|
||||||
|
|
||||||
int chirp_play(const char* new_song) {
|
|
||||||
int c = 0;
|
|
||||||
while (c<MAX_CHANNELS && channels[c].song != NULL) c++;
|
|
||||||
if (c==MAX_CHANNELS) return -1;
|
|
||||||
|
|
||||||
channels[c].play_pos = channels[c].play_buffer;
|
|
||||||
channels[c].play_len = 0;
|
|
||||||
|
|
||||||
channels[c].length=0.25f;
|
|
||||||
channels[c].volume=32;
|
|
||||||
channels[c].octave=4;
|
|
||||||
channels[c].tempo=44100;
|
|
||||||
channels[c].waveform=0;
|
|
||||||
channels[c].stackpos=0;
|
|
||||||
channels[c].song = (char*)malloc( strlen( new_song ) + 1 );
|
|
||||||
strcpy( channels[c].song, new_song );
|
|
||||||
channels[c].song_ptr = channels[c].song_start = channels[c].song;
|
|
||||||
|
|
||||||
audio_state = AUDIO_PLAY;
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
|
|
||||||
int32_t interpret_next_token(const int c) { //uint8_t* buffer) {
|
|
||||||
char** token = &channels[c].song_ptr;
|
|
||||||
char note = 0;
|
|
||||||
char param = -1;
|
|
||||||
|
|
||||||
switch( **token ) {
|
|
||||||
case 'b': note += 2;
|
|
||||||
case 'a': note += 2;
|
|
||||||
case 'g': note += 2;
|
|
||||||
case 'f': note += 1;
|
|
||||||
case 'e': note += 2;
|
|
||||||
case 'd': note += 2;
|
|
||||||
case 'c':
|
|
||||||
param = *++*token;
|
|
||||||
if( param == '#' || param == '+' ) { note++; param = *++*token; } else if( param == '-' ) { note--; param = *++*token; }
|
|
||||||
if( param >= 48 && param <= 57 ) { param -= 48; ++*token; } else { param = -1; }
|
|
||||||
return interpret_note( c, note, param );
|
|
||||||
case 'r':
|
|
||||||
param = *++*token;
|
|
||||||
if( param >= 48 && param <= 57 ) { param -= 48; ++*token; } else { param = -1; }
|
|
||||||
return interpret_note( c, SILENCE, param );
|
|
||||||
case 'n':
|
|
||||||
param = *++*token;
|
|
||||||
if( param >= 48 && param <= 57 ) { param -= 48; ++*token; } else { param = -1; }
|
|
||||||
return interpret_note( c, NOISE, param );
|
|
||||||
case 'o':
|
|
||||||
param = *++*token;
|
|
||||||
if( param >= 48 && param <= 57 ) { channels[c].octave = (param - 48) % 8; ++*token; }
|
|
||||||
return 0;
|
|
||||||
case '>':
|
|
||||||
channels[c].octave = (channels[c].octave+1) % 8; ++*token;
|
|
||||||
return 0;
|
|
||||||
case '<':
|
|
||||||
channels[c].octave = (channels[c].octave-1) % 8; ++*token;
|
|
||||||
return 0;
|
|
||||||
case 'l':
|
|
||||||
param = *++*token;
|
|
||||||
if( param >= 48 && param <= 57 ) { channels[c].length = ((float)lengths[param - 48])/10000.0f; ++*token; }
|
|
||||||
return 0;
|
|
||||||
case 'v':
|
|
||||||
param = *++*token;
|
|
||||||
if( param >= 48 && param <= 57 ) { channels[c].volume = (param - 48) << 3; ++*token; }
|
|
||||||
return 0;
|
|
||||||
case 't':
|
|
||||||
param = *++*token;
|
|
||||||
if( param >= 48 && param <= 57 ) { channels[c].tempo = tempos[param - 48] * 10; ++*token; }
|
|
||||||
return 0;
|
|
||||||
case 'i':
|
|
||||||
param = *++*token;
|
|
||||||
if( param >= 48 && param <= 57 ) { channels[c].instrument = param - 48; ++*token; }
|
|
||||||
return 0;
|
|
||||||
/* case 'w':
|
|
||||||
param = *++*token;
|
|
||||||
if( param >= 48 && param <= 57 ) { channels[c].waveform = param - 48; ++*token; }
|
|
||||||
return 0;*/
|
|
||||||
case '{':
|
|
||||||
{
|
|
||||||
uint8_t instrument = 0;
|
|
||||||
param = *++*token;
|
|
||||||
if( param >= 48 && param <= 57 ) { instrument = param - 48; param = *++*token; }
|
|
||||||
while (param != '}') {
|
|
||||||
switch (param) {
|
|
||||||
case 'w':
|
|
||||||
param = *++*token;
|
|
||||||
if( param >= 48 && param <= 57 ) { channels[c].instruments[instrument].waveform = param - 48; param = *++*token; }
|
|
||||||
break;
|
|
||||||
case 'v':
|
|
||||||
channels[c].instruments[instrument].volume_data = RELATIVE;
|
|
||||||
param = *++*token;
|
|
||||||
if (param=='r') { channels[c].instruments[instrument].volume_data = RELATIVE; param = *++*token; }
|
|
||||||
if (param=='s') { channels[c].instruments[instrument].volume_data = SEQUENTIAL; param = *++*token; }
|
|
||||||
for (int i=0;i<8;++i) {
|
|
||||||
int8_t sign = 1;
|
|
||||||
if (param=='-') { sign=-1; param = *++*token; }
|
|
||||||
if( param >= 48 && param <= 57 ) channels[c].instruments[instrument].volume[i] = (param-48)*sign;
|
|
||||||
param = *++*token;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'p':
|
|
||||||
channels[c].instruments[instrument].pitch_data = RELATIVE;
|
|
||||||
param = *++*token;
|
|
||||||
if (param=='r') { channels[c].instruments[instrument].pitch_data = RELATIVE; param = *++*token; }
|
|
||||||
if (param=='s') { channels[c].instruments[instrument].pitch_data = SEQUENTIAL; param = *++*token; }
|
|
||||||
for (int i=0;i<8;++i) {
|
|
||||||
int8_t sign = 1;
|
|
||||||
if (param=='-') { sign=-1; param = *++*token; }
|
|
||||||
if( param >= 48 && param <= 57 ) channels[c].instruments[instrument].pitch[i] = (param-48)*sign;
|
|
||||||
param = *++*token;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case '}':
|
|
||||||
default:
|
|
||||||
param = *++*token;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
//param = *++*token;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
case '!':
|
|
||||||
channels[c].stackpos = 0;
|
|
||||||
channels[c].song_start = ++*token;
|
|
||||||
return 0;
|
|
||||||
case '=':
|
|
||||||
channels[c].stackpos = 0;
|
|
||||||
channels[c].song_ptr = channels[c].song_start;
|
|
||||||
return 0;
|
|
||||||
case '[':
|
|
||||||
param = *++*token;
|
|
||||||
if( param >= 48 && param <= 57 ) { param -= 48; ++*token; } else { param = 0; }
|
|
||||||
channels[c].labels[uint8_t(param)] = *token; //channels[c].song_ptr;
|
|
||||||
{
|
|
||||||
char* nextpos = *token;
|
|
||||||
uint8_t innerblocks=0;
|
|
||||||
while(innerblocks>0 || *nextpos!=']') {
|
|
||||||
if (*nextpos=='[') innerblocks++;
|
|
||||||
if (*nextpos==']') innerblocks--;
|
|
||||||
nextpos++;
|
|
||||||
}
|
|
||||||
channels[c].stack[channels[c].stackpos]=nextpos+1;
|
|
||||||
}
|
|
||||||
channels[c].stackpos++;
|
|
||||||
return 0;
|
|
||||||
case ']':
|
|
||||||
++*token;
|
|
||||||
channels[c].stackpos--;
|
|
||||||
channels[c].song_ptr=channels[c].stack[channels[c].stackpos];
|
|
||||||
return 0;
|
|
||||||
case '@':
|
|
||||||
param = *++*token;
|
|
||||||
if( param >= 48 && param <= 57 ) { param -= 48; ++*token; } else { param = 0; }
|
|
||||||
channels[c].stack[channels[c].stackpos]=*token;
|
|
||||||
channels[c].stackpos++;
|
|
||||||
channels[c].song_ptr=channels[c].labels[uint8_t(param)];
|
|
||||||
return 0;
|
|
||||||
case '\0':
|
|
||||||
return -1;
|
|
||||||
default:
|
|
||||||
++*token;
|
|
||||||
return 0;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
void chirp_stop() {
|
|
||||||
for (int i=0;i<MAX_CHANNELS;++i) {
|
|
||||||
if (channels[i].song != NULL) {
|
|
||||||
free(channels[i].song);
|
|
||||||
channels[i].song=NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
audio_state=AUDIO_NONE;
|
|
||||||
}
|
|
||||||
10
chirp.h
10
chirp.h
@@ -1,10 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
void chirp_init();
|
|
||||||
|
|
||||||
int chirp_play(const char* new_song);
|
|
||||||
|
|
||||||
int32_t interpret_next_token(const int c);
|
|
||||||
|
|
||||||
void chirp_stop();
|
|
||||||
@@ -2,9 +2,6 @@ x=0
|
|||||||
|
|
||||||
function _init()
|
function _init()
|
||||||
text="HOLA MINI"
|
text="HOLA MINI"
|
||||||
playchirp("{0w3vs-10000000ps0-5-6-90000}{1w5v0-1-2-30000p00000000}!t9v4o2 l3i0f l6r i1l3f l5r l3f i0f r f r i1f l6r=") -- drums
|
|
||||||
playchirp("t9v4{0w0v0-1-2-30000}o2 ! l9 rrrrrrrr l3 [0 frrfg#rgr][1 frrfa#>c<a#g#]@0@1@0@1@0[2 f>fr<f>frfr<] =") -- bass
|
|
||||||
playchirp("t9v1 {1w0v44422211p037037037} l9 rrrrrrrrrrrrrrrr i1 [0 l5frfl3rl5dl3rl5<a#raa#ra#l3rl5al3rl8r>]") -- arp
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function _update()
|
function _update()
|
||||||
@@ -13,14 +10,3 @@ function _update()
|
|||||||
x=x+1
|
x=x+1
|
||||||
if x>160 then x=-strlen(text)*4 end
|
if x>160 then x=-strlen(text)*4 end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- 0 *
|
|
||||||
-- 1 **
|
|
||||||
-- 2 ***
|
|
||||||
-- 3 ****
|
|
||||||
-- 4 ******
|
|
||||||
-- 5 ********
|
|
||||||
-- 6 ************
|
|
||||||
-- 7 ****************
|
|
||||||
-- 8 ************************
|
|
||||||
-- 9 ********************************
|
|
||||||
54
lua.cpp
54
lua.cpp
@@ -649,14 +649,50 @@ extern "C" {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int cpp_playchirp(lua_State *L) {
|
static int cpp_playmusic(lua_State *L) {
|
||||||
const char* str = luaL_checkstring(L, 1);
|
const char* str = luaL_checkstring(L, 1);
|
||||||
playchirp(str);
|
const int volume = luaL_optinteger(L, 2, -1);
|
||||||
|
playmusic(str, volume);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int cpp_stopchirp(lua_State *L) {
|
static int cpp_pausemusic(lua_State *L) {
|
||||||
stopchirp();
|
pausemusic();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cpp_resumemusic(lua_State *L) {
|
||||||
|
resumemusic();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cpp_stopmusic(lua_State *L) {
|
||||||
|
stopmusic();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cpp_loadsound(lua_State *L) {
|
||||||
|
const char* str = luaL_checkstring(L, 1);
|
||||||
|
lua_pushinteger(L,loadsound(str));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cpp_freesound(lua_State *L) {
|
||||||
|
const int sound = luaL_checknumber(L, 1);
|
||||||
|
freesound(sound);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cpp_playsound(lua_State *L) {
|
||||||
|
const int sound = luaL_checknumber(L, 1);
|
||||||
|
const int volume = luaL_optinteger(L, 2, -1);
|
||||||
|
lua_pushinteger(L,playsound(sound, volume));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cpp_stopsound(lua_State *L) {
|
||||||
|
const int sound = luaL_checknumber(L, 1);
|
||||||
|
stopsound(sound);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -761,8 +797,14 @@ void push_lua_funcs() {
|
|||||||
lua_pushcfunction(L,cpp_freadw); lua_setglobal(L, "freadw");
|
lua_pushcfunction(L,cpp_freadw); lua_setglobal(L, "freadw");
|
||||||
lua_pushcfunction(L,cpp_freadb); lua_setglobal(L, "freadb");
|
lua_pushcfunction(L,cpp_freadb); lua_setglobal(L, "freadb");
|
||||||
|
|
||||||
lua_pushcfunction(L,cpp_playchirp); lua_setglobal(L, "playchirp");
|
lua_pushcfunction(L,cpp_playmusic); lua_setglobal(L, "playmusic");
|
||||||
lua_pushcfunction(L,cpp_stopchirp); lua_setglobal(L, "stopchirp");
|
lua_pushcfunction(L,cpp_pausemusic); lua_setglobal(L, "pausemusic");
|
||||||
|
lua_pushcfunction(L,cpp_resumemusic); lua_setglobal(L, "resumemusic");
|
||||||
|
lua_pushcfunction(L,cpp_stopmusic); lua_setglobal(L, "stopmusic");
|
||||||
|
lua_pushcfunction(L,cpp_loadsound); lua_setglobal(L, "loadsound");
|
||||||
|
lua_pushcfunction(L,cpp_freesound); lua_setglobal(L, "freesound");
|
||||||
|
lua_pushcfunction(L,cpp_playsound); lua_setglobal(L, "playsound");
|
||||||
|
lua_pushcfunction(L,cpp_stopsound); lua_setglobal(L, "stopsound");
|
||||||
|
|
||||||
lua_pushcfunction(L,cpp_exit); lua_setglobal(L, "quit");
|
lua_pushcfunction(L,cpp_exit); lua_setglobal(L, "quit");
|
||||||
|
|
||||||
|
|||||||
67
mini.cpp
67
mini.cpp
@@ -3,8 +3,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
#include "gif.c"
|
#include "gif.c"
|
||||||
|
#include "SDL2/SDL_mixer.h"
|
||||||
#include "chirp.h"
|
|
||||||
|
|
||||||
#pragma pack(1)
|
#pragma pack(1)
|
||||||
|
|
||||||
@@ -75,6 +74,10 @@ Uint8 key_just_pressed = 0;
|
|||||||
int mouse_x, mouse_y, mouse_wheel;
|
int mouse_x, mouse_y, mouse_wheel;
|
||||||
Uint32 mouse_buttons;
|
Uint32 mouse_buttons;
|
||||||
|
|
||||||
|
#define MAX_SOUNDS 50
|
||||||
|
Mix_Music *music = NULL;
|
||||||
|
Mix_Chunk *sounds[MAX_SOUNDS];
|
||||||
|
|
||||||
char* get_value_from_line(char* line) {
|
char* get_value_from_line(char* line) {
|
||||||
char* equal_character = strchr(line, '=');
|
char* equal_character = strchr(line, '=');
|
||||||
if (equal_character == NULL) return NULL;
|
if (equal_character == NULL) return NULL;
|
||||||
@@ -122,10 +125,13 @@ void reinit() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void initaudio() {
|
void initaudio() {
|
||||||
chirp_init();
|
Mix_OpenAudio(44100, AUDIO_S16, 1, 512);
|
||||||
|
for (int i=0;i<MAX_SOUNDS;++i) sounds[i] = NULL;
|
||||||
}
|
}
|
||||||
void quitaudio() {
|
void quitaudio() {
|
||||||
|
if (music != NULL) Mix_FreeMusic(music);
|
||||||
|
for (int i=0;i<MAX_SOUNDS;++i) if (sounds[i]!=NULL) Mix_FreeChunk(sounds[i]);
|
||||||
|
Mix_CloseAudio();
|
||||||
}
|
}
|
||||||
|
|
||||||
int scrw() {
|
int scrw() {
|
||||||
@@ -202,6 +208,8 @@ int main(int argc,char*argv[]){
|
|||||||
SDL_ShowCursor(show_cursor);
|
SDL_ShowCursor(show_cursor);
|
||||||
mini_bak = SDL_CreateTexture(mini_ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, screen_width, screen_height);
|
mini_bak = SDL_CreateTexture(mini_ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, screen_width, screen_height);
|
||||||
|
|
||||||
|
Mix_Init(MIX_INIT_OGG);
|
||||||
|
|
||||||
SDL_Event mini_eve;
|
SDL_Event mini_eve;
|
||||||
|
|
||||||
reinit();
|
reinit();
|
||||||
@@ -219,7 +227,6 @@ int main(int argc,char*argv[]){
|
|||||||
if (mini_eve.type == SDL_KEYDOWN) {
|
if (mini_eve.type == SDL_KEYDOWN) {
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
if (mini_eve.key.keysym.scancode == SDL_SCANCODE_F1) {
|
if (mini_eve.key.keysym.scancode == SDL_SCANCODE_F1) {
|
||||||
chirp_stop();
|
|
||||||
if (lua_is_playing()) {
|
if (lua_is_playing()) {
|
||||||
lua_quit();
|
lua_quit();
|
||||||
quitaudio();
|
quitaudio();
|
||||||
@@ -228,7 +235,6 @@ int main(int argc,char*argv[]){
|
|||||||
should_exit=true;
|
should_exit=true;
|
||||||
}
|
}
|
||||||
} else if (mini_eve.key.keysym.scancode == SDL_SCANCODE_F5) {
|
} else if (mini_eve.key.keysym.scancode == SDL_SCANCODE_F5) {
|
||||||
chirp_stop();
|
|
||||||
should_exit=true;
|
should_exit=true;
|
||||||
} else {
|
} else {
|
||||||
key_just_pressed = mini_eve.key.keysym.scancode;
|
key_just_pressed = mini_eve.key.keysym.scancode;
|
||||||
@@ -262,6 +268,9 @@ int main(int argc,char*argv[]){
|
|||||||
}
|
}
|
||||||
lua_quit();
|
lua_quit();
|
||||||
quitaudio();
|
quitaudio();
|
||||||
|
|
||||||
|
Mix_Quit();
|
||||||
|
|
||||||
for (int i=0;i<10;++i) freesurf(i);
|
for (int i=0;i<10;++i) freesurf(i);
|
||||||
SDL_DestroyTexture(mini_bak);
|
SDL_DestroyTexture(mini_bak);
|
||||||
SDL_DestroyRenderer(mini_ren);
|
SDL_DestroyRenderer(mini_ren);
|
||||||
@@ -987,12 +996,50 @@ bool freadb() {
|
|||||||
return strcmp(fstr, "true")==0?true:false;
|
return strcmp(fstr, "true")==0?true:false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void playchirp(const char *song) {
|
void playmusic(const char *filename, const int volume) {
|
||||||
chirp_play(song);
|
int size;
|
||||||
|
char *buffer = file_getfilebuffer(filename, size);
|
||||||
|
if (music != NULL) Mix_FreeMusic(music);
|
||||||
|
music = Mix_LoadMUS_RW(SDL_RWFromMem(buffer, size), 1);
|
||||||
|
Mix_PlayMusic(music, -1);
|
||||||
|
if (volume!=-1) Mix_VolumeMusic(volume);
|
||||||
}
|
}
|
||||||
|
|
||||||
void stopchirp() {
|
void pausemusic() {
|
||||||
chirp_stop();
|
Mix_PauseMusic();
|
||||||
|
}
|
||||||
|
|
||||||
|
void resumemusic() {
|
||||||
|
Mix_ResumeMusic();
|
||||||
|
}
|
||||||
|
|
||||||
|
void stopmusic() {
|
||||||
|
Mix_HaltMusic();
|
||||||
|
}
|
||||||
|
|
||||||
|
int loadsound(const char *filename) {
|
||||||
|
int size;
|
||||||
|
char *buffer = file_getfilebuffer(filename, size);
|
||||||
|
int i=0;
|
||||||
|
while (i<MAX_SOUNDS && sounds[i]!=NULL) i++;
|
||||||
|
if (i==MAX_SOUNDS) { i=0; Mix_FreeChunk(sounds[i]); }
|
||||||
|
sounds[i]=Mix_LoadWAV_RW(SDL_RWFromMem(buffer, size), 1);
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
void freesound(int soundfile) {
|
||||||
|
Mix_FreeChunk(sounds[soundfile]);
|
||||||
|
sounds[soundfile] = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
int playsound(int soundfile, const int volume) {
|
||||||
|
const int channel = Mix_PlayChannel(-1, sounds[soundfile], 0);
|
||||||
|
if (volume!=-1) Mix_Volume(channel, volume);
|
||||||
|
return channel;
|
||||||
|
}
|
||||||
|
|
||||||
|
void stopsound(int soundchannel) {
|
||||||
|
Mix_HaltChannel(soundchannel);
|
||||||
}
|
}
|
||||||
|
|
||||||
void exit() {
|
void exit() {
|
||||||
|
|||||||
13
mini.h
13
mini.h
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
|
|
||||||
#define MINI_VERSION "0.9.02d"
|
#define MINI_VERSION "0.9.32d"
|
||||||
|
|
||||||
#define KEY_UNKNOWN 0
|
#define KEY_UNKNOWN 0
|
||||||
#define KEY_A 4
|
#define KEY_A 4
|
||||||
@@ -257,7 +257,14 @@ const char *freads();
|
|||||||
const char *freadw();
|
const char *freadw();
|
||||||
bool freadb();
|
bool freadb();
|
||||||
|
|
||||||
void playchirp(const char *song);
|
void playmusic(const char *filename, const int volume=-1);
|
||||||
void stopchirp();
|
void pausemusic();
|
||||||
|
void resumemusic();
|
||||||
|
void stopmusic();
|
||||||
|
|
||||||
|
int loadsound(const char *filename);
|
||||||
|
void freesound(int soundfile);
|
||||||
|
int playsound(int soundfile, const int volume=-1);
|
||||||
|
void stopsound(int soundchannel);
|
||||||
|
|
||||||
void exit();
|
void exit();
|
||||||
|
|||||||
Reference in New Issue
Block a user