Browse Source

pattern loading

develop
Carmine De Rosa 5 years ago
parent
commit
bf8fc4033a
  1. BIN
      C/banks/BANK1/1.wav
  2. 66
      C/loaders.cpp
  3. BIN
      C/main
  4. 26
      C/main.cpp
  5. 6
      C/patterns/pattern1
  6. 6
      C/patterns/pattern2
  7. 4
      C/players.cpp
  8. BIN
      C/sonquencer

BIN
C/banks/BANK1/1.wav

Binary file not shown.

66
C/loaders.cpp

@ -1,7 +1,8 @@
int loadBank(int bank) {
memset(_sample, 0, sizeof(Mix_Chunk*) * NUM_WAVEFORMS);
printf("\nLoading bank %d ...\n", bank);
memset(samples, 0, sizeof(Mix_Chunk*) * NUM_WAVEFORMS);
int result = Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 512);
if( result < 0 ) {
@ -20,11 +21,70 @@ int loadBank(int bank) {
for( int i = 0; i < NUM_WAVEFORMS; i++ ) {
sprintf(waveFileName, "banks/BANK%d/%d.wav", bank, i+1);
_sample[i] = Mix_LoadWAV(waveFileName);
if( _sample[i] == NULL ) {
samples[i] = Mix_LoadWAV(waveFileName);
if( samples[i] == NULL ) {
fprintf(stderr, "Unable to load wave file: %s\n", waveFileName);
}
}
return true;
}
void loadPattern(int pattern) {
printf("\nLoading pattern %d ...\n", pattern);
FILE *fp;
char *line = NULL;
size_t len = 0;
ssize_t read;
int lineCount = 0;
char patternFile[16];
int bank = 0;
int cols = 0;
int rows = 0;
int patternArray[10][32];
sprintf(patternFile, "patterns/pattern%d", pattern);
fp = fopen(patternFile, "r");
if(fp == NULL) {exit(EXIT_FAILURE);}
while ((read = getline(&line, &len, fp)) != -1) {
//printf("Retrieved line of length %zu:\n", read);
if(lineCount == 0) { bank = line[10]-'0'; }
if(lineCount == 1) { rows = line[10]-'0'; }
if(lineCount > 1) {
if(lineCount == 2) {
cols = read-1;
}
for(int i=0; i<cols; i++) {
patternArray[lineCount-2][i] = line[i];
}
}
lineCount++;
}
printf("Pattern: %d\nBank: %d\nColumns: %d\nRows: %d\n", pattern, bank, cols, rows);
for(int i=0; i<rows; i++) {
//printf("\nCol %d: ", i);
for(int y=0; y<cols; y++) {
printf("%d ", patternArray[i][y]-'0');
}
printf("\n");
}
printf("\n");
fclose(fp);
if(line) {free(line);}
loadBank(bank);
}

BIN
C/main

Binary file not shown.

26
C/main.cpp

@ -1,15 +1,16 @@
#include <stdio.h>
#include "SDL.h"
#include "SDL_mixer.h"
#include "SDL_image.h"
#include <unistd.h>
#include <syslog.h>
#include <string.h>
#include <math.h>
#define NUM_WAVEFORMS 6
#include "SDL.h"
#include "SDL_mixer.h"
#include "SDL_image.h"
#define NUM_WAVEFORMS 4
Mix_Chunk* _sample[NUM_WAVEFORMS];
Mix_Chunk* samples[NUM_WAVEFORMS];
#include "players.cpp"
#include "loaders.cpp"
@ -33,16 +34,17 @@ int main(int argc, char** argv) {
SDL_SetRenderDrawColor(renderer, 100, 100, 100, 255);
SDL_Delay(1000);
if(loadBank(1) == false) {return -1;}
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
SDL_Event event;
if(loadBank(1) == false) {return -1;}
loadPattern(2);
/*
while (!done) {
bool gotEvent = SDL_PollEvent(&event);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
while (!done && gotEvent) {
switch (event.type) {
case SDL_KEYDOWN:
@ -91,10 +93,10 @@ int main(int argc, char** argv) {
playStep(play, &timer, &step);
}
*/
for( int i = 0; i < NUM_WAVEFORMS; i++ ) {
Mix_FreeChunk(_sample[i]);
Mix_FreeChunk(samples[i]);
}
Mix_CloseAudio();

6
C/patterns/pattern1

@ -0,0 +1,6 @@
bank 1
rows 4
00011010
01100010
00011000
10000000

6
C/patterns/pattern2

@ -0,0 +1,6 @@
bank 2
rows 4
10000010
01100010
00011000
10001001

4
C/players.cpp

@ -6,10 +6,10 @@ void playStep(bool play, int *timer, int *step) {
if(*timer == 0) {
printf("%d\n", *timer);
Mix_PlayChannel(-1, _sample[1], 0);
Mix_PlayChannel(-1, samples[1], 0);
}
}
void playSample(int sample) {
Mix_PlayChannel(-1, _sample[sample-1], 0);
Mix_PlayChannel(-1, samples[sample-1], 0);
}

BIN
C/sonquencer

Binary file not shown.
Loading…
Cancel
Save