Mercurial > hg > Game > Cerium
changeset 2020:6849865f96eb draft
add synthesizer project
author | Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 15 Jul 2014 17:23:40 +0900 |
parents | 433043c56a0c |
children | 8130b38b5391 |
files | example/synthesizer/AudioData.h example/synthesizer/Func.h example/synthesizer/Makefile example/synthesizer/Makefile.cell example/synthesizer/Makefile.cuda example/synthesizer/Makefile.def example/synthesizer/Makefile.gpu example/synthesizer/Makefile.linux example/synthesizer/Makefile.macosx example/synthesizer/README example/synthesizer/main.cc example/synthesizer/ppe/OSC.cc example/synthesizer/ppe/OSC.h example/synthesizer/spe/Exec.cc example/synthesizer/spe/Exec.h example/synthesizer/spe/Makefile example/synthesizer/task_init.cc |
diffstat | 17 files changed, 597 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/synthesizer/AudioData.h Tue Jul 15 17:23:40 2014 +0900 @@ -0,0 +1,16 @@ +#include <SDL_audio.h> +typedef struct audioData { + struct audioData *self; + int volume; + double Frequency; + char *waveform_name; + + int freq; + Uint16 format; + Uint8 channels; + Uint8 silence; + Uint16 samples; + Uint32 size; + //void (*callback)(void *userdata, Uint8 *stream, int len); + void *userdata; +} AudioData, *AudioDataPtr;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/synthesizer/Func.h Tue Jul 15 17:23:40 2014 +0900 @@ -0,0 +1,9 @@ +enum { +#include "SysTasks.h" + OSC_TASK, +}; + +#define DATA_NUM 16 +#define ADD_NUM 26 + +#define DATA_ID 0
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/synthesizer/Makefile Tue Jul 15 17:23:40 2014 +0900 @@ -0,0 +1,40 @@ +default: macosx + +macosx: FORCE + @echo "Make for Mac OS X" + @$(MAKE) -f Makefile.macosx + +linux: FORCE + @echo "Make for Linux" + @$(MAKE) -f Makefile.linux + +cell: FORCE + @echo "Make for CELL (Cell)" + @$(MAKE) -f Makefile.cell + +gpu: FORCE + @echo "Make for OpenCL" + @$(MAKE) -f Makefile.gpu + +cuda: FORCE + @echo "Make for Cuda" + @$(MAKE) -f Makefile.cuda + +test: + ./word_count -file c.txt + +parallel-test: macosx + @$(MAKE) -f Makefile.macosx test + +gpu-test: FORCE + @echo "Make for OpenCL" + @$(MAKE) -f Makefile.gpu test + + +FORCE: + +clean: + @$(MAKE) -f Makefile.macosx clean + @$(MAKE) -f Makefile.linux clean + @$(MAKE) -f Makefile.cell clean + @$(MAKE) -f Makefile.cuda clean
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/synthesizer/Makefile.cell Tue Jul 15 17:23:40 2014 +0900 @@ -0,0 +1,39 @@ +include ./Makefile.def + +SRCS_TMP = $(wildcard *.cc) +SRCS_EXCLUDE = # 除外するファイルを書く +SRCS = $(filter-out $(SRCS_EXCLUDE),$(SRCS_TMP)) +OBJS = $(SRCS:.cc=.o) + +TASK_DIR = ppe +TASK_SRCS_TMP = $(wildcard $(TASK_DIR)/*.cc) +TASK_SRCS_EXCLUDE = +TASK_SRCS = $(filter-out $(TASK_DIR)/$(TASK_SRCS_EXCLUDE),$(TASK_SRCS_TMP)) +TASK_OBJS = $(TASK_SRCS:.cc=.o) + +LIBS += -lCellManager -lspe2 -lpthread -Wl,--gc-sections + +.SUFFIXES: .cc .o + +.cc.o: + $(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@ + +all: $(TARGET) speobject + +$(TARGET): $(OBJS) $(TASK_OBJS) + $(CC) -o $@ $(OBJS) $(TASK_OBJS) $(LIBS) + +speobject: + cd spe; $(MAKE) + +link: + $(CC) -o $(TARGET) $(OBJS) $(TASK_OBJS) $(LIBS) + +debug: $(TARGET) + sudo ppu-gdb ./$(TARGET) + +clean: + rm -f $(TARGET) $(OBJS) $(TASK_OBJS) + rm -f *~ \#* + rm -f ppe/*~ ppe/\#* + cd spe; $(MAKE) clean
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/synthesizer/Makefile.cuda Tue Jul 15 17:23:40 2014 +0900 @@ -0,0 +1,51 @@ +include ./Makefile.def + +SRCS_TMP = $(wildcard *.cc) +SRCS_EXCLUDE = # 除外するファイルを書く +SRCS = $(filter-out $(SRCS_EXCLUDE),$(SRCS_TMP)) +OBJS = $(SRCS:.cc=.o) + +TASK_DIR = ppe +CUDA_TASK_DIR = cuda + +TASK_SRCS_TMP = $(wildcard $(TASK_DIR)/*.cc) +TASK_SRCS_EXCLUDE = +TASK_SRCS = $(filter-out $(TASK_DIR)/$(TASK_SRCS_EXCLUDE),$(TASK_SRCS_TMP)) $(wildcard $(CUDA_TASK_DIR)/*.cc) +TASK_OBJS = $(TASK_SRCS:.cc=.o) + +CUDA_SRCS_TMP = $(wildcard $(CUDA_TASK_DIR)/*.cu) +CUDA_SRCS_EXCLUDE = # 除外するファイルを書く +CUDA_SRCS = $(filter-out $(CUDA_TASK_DIR)/$(CUDA_SRCS_EXCLUDE),$(CUDA_SRCS_TMP)) +CUDA_OBJS = $(CUDA_SRCS:.cu=.ptx) + +CFLAGS += -D__CERIUM_CUDA__ +LIBS += `sdl-config --libs` -lCudaManager -F/Library/Frameworks -framework CUDA + +INCLUDE += -I$(CUDA_PATH) + +NVCC = nvcc +NVCCFLAGS = -ptx -arch=sm_20 + +.SUFFIXES: .cc .o .cu .ptx + +.cc.o: + $(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@ + +.cu.ptx: + $(NVCC) $(NVCCFLAGS) $< -o $@ + +all: $(TARGET) $(CUDA_OBJS) + +$(TARGET): $(OBJS) $(TASK_OBJS) $(CUDA_OBJS) + $(CC) -o $@ $(OBJS) $(TASK_OBJS) $(LIBS) + +link: + $(CC) -o $(TARGET) $(OBJS) $(TASK_OBJS) $(LIBS) + +debug: $(TARGET) + sudo ppu-gdb ./$(TARGET) + +clean: + rm -f $(TARGET) $(OBJS) $(TASK_OBJS) $(CUDA_OBJS) + rm -f *~ \#* + rm -f cuda/*~ cuda/\#*
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/synthesizer/Makefile.def Tue Jul 15 17:23:40 2014 +0900 @@ -0,0 +1,18 @@ +TARGET = synthesizer + +# include/library path +# ex macosx +#CERIUM = /Users/gongo/Source/Cerium +ABIBIT=64 + +# ex linux/ps3 +CERIUM = ../../../Cerium + + +OPT = -g -O0 + +CC = clang++ +CFLAGS = -Wall `sdl-config --cflags` -m$(ABIBIT) $(OPT) #-DDEBUG + +INCLUDE = -I${CERIUM}/include/TaskManager -I. -I.. +LIBS = -L${CERIUM}/TaskManager
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/synthesizer/Makefile.gpu Tue Jul 15 17:23:40 2014 +0900 @@ -0,0 +1,62 @@ +include ./Makefile.def + +SRCS_TMP = $(wildcard *.cc) +SRCS_EXCLUDE = # 除外するファイルを書く +SRCS = $(filter-out $(SRCS_EXCLUDE),$(SRCS_TMP)) +OBJS = $(SRCS:.cc=.o) + +TASK_DIR1 = ppe +TASK_DIR2 = gpu +TASK_SRCS_TMP = $(wildcard $(TASK_DIR2)/*.cc $(TASK_DIR1)/*.cc) +TASK_SRCS_EXCLUDE = # Exec.cc +TASK_SRCS = $(filter-out $(TASK_DIR1)/$(TASK_SRCS_EXCLUDE),$(TASK_SRCS_TMP)) +TASK_OBJS = $(TASK_SRCS:.cc=.o) + +CC += $(ABI) +CFLAGS += -D__CERIUM_GPU__ + +INCLUDE = -I${CERIUM}/include/TaskManager -I. -I.. +LIBS = -L${CERIUM}/TaskManager -DUSE_SIMPLE_TASK -lGpuManager -framework opencl `sdl-config --libs` + +.SUFFIXES: .cc .o + +.cc.o: + $(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@ + +all: $(TARGET) + +$(TARGET): $(OBJS) $(TASK_OBJS) + $(CC) -o $@ $(OBJS) $(TASK_OBJS) $(LIBS) + +link: + $(CC) -o $(TARGET) $(OBJS) $(TASK_OBJS) $(LIBS) + +debug: $(TARGET) + sudo lldb -- ./$(TARGET) -file c.txt -gpu -g + +test : + ./$(TARGET) -file c.txt -gpu -g + +clean: + rm -f $(TARGET) $(OBJS) $(TASK_OBJS) + rm -f *~ \#* + rm -f ppe/*~ ppe/\#* + rm -f spe/*~ spe/\#* + rm -f gpu/*~ gpu/\#* + + + + + + + + + + + + + + + + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/synthesizer/Makefile.linux Tue Jul 15 17:23:40 2014 +0900 @@ -0,0 +1,36 @@ +include ./Makefile.def + +SRCS_TMP = $(wildcard *.cc) +SRCS_EXCLUDE = # 除外するファイルを書く +SRCS = $(filter-out $(SRCS_EXCLUDE),$(SRCS_TMP)) +OBJS = $(SRCS:.cc=.o) + +TASK_DIR = ppe +TASK_SRCS_TMP = $(wildcard $(TASK_DIR)/*.cc) +TASK_SRCS_EXCLUDE = +TASK_SRCS = $(filter-out $(TASK_DIR)/$(TASK_SRCS_EXCLUDE),$(TASK_SRCS_TMP)) +TASK_OBJS = $(TASK_SRCS:.cc=.o) + +LIBS += -lFifoManager -lrt + +.SUFFIXES: .cc .o + +.cc.o: + $(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@ + +all: $(TARGET) + +$(TARGET): $(OBJS) $(TASK_OBJS) + $(CC) -o $@ $(OBJS) $(TASK_OBJS) $(LIBS) + +link: + $(CC) -o $(TARGET) $(OBJS) $(TASK_OBJS) $(LIBS) + +debug: $(TARGET) + sudo gdb ./$(TARGET) + +clean: + rm -f $(TARGET) $(OBJS) $(TASK_OBJS) + rm -f *~ \#* + rm -f ppe/*~ ppe/\#* + rm -f spe/*~ spe/\#*
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/synthesizer/Makefile.macosx Tue Jul 15 17:23:40 2014 +0900 @@ -0,0 +1,41 @@ +include ./Makefile.def + +SRCS_TMP = $(wildcard *.cc) +SRCS_EXCLUDE = # 除外するファイルを書く +SRCS = $(filter-out $(SRCS_EXCLUDE),$(SRCS_TMP)) +OBJS = $(SRCS:.cc=.o) + +TASK_DIR = ppe +TASK_SRCS_TMP = $(wildcard $(TASK_DIR)/*.cc) +TASK_SRCS_EXCLUDE = +TASK_SRCS = $(filter-out $(TASK_DIR)/$(TASK_SRCS_EXCLUDE),$(TASK_SRCS_TMP)) +TASK_OBJS = $(TASK_SRCS:.cc=.o) + +LIBS += -lFifoManager `sdl-config --libs` +CC += -m$(ABIBIT) + +.SUFFIXES: .cc .o + +.cc.o: + $(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@ + +all: $(TARGET) + +$(TARGET): $(OBJS) $(TASK_OBJS) + $(CC) -o $@ $(OBJS) $(TASK_OBJS) $(LIBS) + +link: + $(CC) -o $(TARGET) $(OBJS) $(TASK_OBJS) $(LIBS) + +debug: $(TARGET) + sudo gdb ./$(TARGET) + +test: + ./$(TARGET) -file c.txt -cpu 1 + ./$(TARGET) -file c.txt -cpu 4 + ./$(TARGET) -file c.txt -cpu 4 -i +clean: + rm -f $(TARGET) $(OBJS) $(TASK_OBJS) + rm -f *~ \#* + rm -f ppe/*~ ppe/\#* + rm -f spe/*~ spe/\#*
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/synthesizer/README Tue Jul 15 17:23:40 2014 +0900 @@ -0,0 +1,19 @@ + +word count + + 16 16 16 + |------|------|------|------|------|------|------|------| + +と言うように実行すると、 + /\/\/\/\ + \/\/\/\/ +patter になってしまう。 + + + 16 16 + |------|------| 16 + |------|------| 16 + |------|------| 16 + |------|------| + +となるようにしたい。run16 は逐次で実行されるので、二つspawnすれば良い?
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/synthesizer/main.cc Tue Jul 15 17:23:40 2014 +0900 @@ -0,0 +1,77 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/mman.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <unistd.h> +#include <sys/time.h> +#include "TaskManager.h" +#include "SchedTask.h" +#include "Func.h" +#include "AudioData.h" + +#include <sdl.h> +#include <SDL_audio.h> +#include <math.h> + +/* + * PS3でCPU数が2以上の時に、あまりが計算されてない + */ + +extern void task_init(); +CPU_TYPE spe_cpu = SPE_ANY; +int volume = 3000; +double Frequency = 440; +char *waveform_name; + +const char *usr_help_str = "Usage: ./word_count [-a -c -s] [-cpu spe_num] [-g] [-file filename] [-br]\n"; + +static void +run_start(TaskManager *manager) +{ + + AudioDataPtr au = (AudioDataPtr)manager->allocate(sizeof(AudioData)); + au->self = au; + + au->volume = volume; + au->Frequency = Frequency; + au->waveform_name = waveform_name; + + printf("Freq:%f\n",Frequency); + au->freq= 44100; /* Sampling rate: 44100Hz */ + au->format= AUDIO_S16LSB; /* 16-bit signed audio */ + au->channels= 1; /* Mono */ + au->samples= 8192; /* Buffer size: 8K = 0.37 sec. */ + // au->callback= callback; + au->userdata= NULL; + + HTaskPtr osc = manager->create_task(OSC_TASK); + osc->set_inData(0,(memaddr)&au->self,sizeof(memaddr)); + osc->spawn(); +} + +void +init(int argc, char **argv) +{ + for (int i = 1; argv[i]; ++i) { + if (strcmp(argv[i], "-freq") == 0) { + Frequency = atof(argv[i+1]); + } else if (strcmp(argv[i], "-wave") == 0) { + waveform_name = argv[i+1]; + } else if (strcmp(argv[i], "-vol") == 0) { + volume = atof(argv[i+1]); + } + } +} + + +int +TMmain(TaskManager *manager, int argc, char *argv[]) +{ + task_init(); + run_start(manager); + return 0; +} +/* end */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/synthesizer/ppe/OSC.cc Tue Jul 15 17:23:40 2014 +0900 @@ -0,0 +1,78 @@ +#include <stdio.h> +#include <string.h> +#include "AudioData.h" +#include "Func.h" +#include "OSC.h" + +#include <sdl.h> +#include <SDL_audio.h> +#include <math.h> +/* これは必須 */ +SchedDefineTask1(Audioosc,osc); + +int gvolume = 3000; +double gFrequency = 440; +char *gwaveform_name; +SDL_AudioSpec Desired; +SDL_AudioSpec Obtained; + +double square(double t){ + double decimal_part = t - abs(t); + return decimal_part < 0.5 ? 1 : -1; +} + +double tri(double t){ + + double decimal_part = t - abs(t); + + if(abs(t) % 2 != 0){ + return decimal_part < 0.5 ? decimal_part : 1 - decimal_part; + }else{ + return decimal_part < 0.5 ? -decimal_part : 1 - decimal_part; + } +} + + +void callback(void *unused,Uint8 *stream,int len){ + static unsigned int step = 0; + Uint16 *frames = (Uint16 *) stream; + int framesize = len / 2; + + if(strcmp(gwaveform_name, "tri")){ + + for (int i = 0; i < framesize ; i++, step++){ + frames[i] = tri(step * gFrequency / Obtained.freq) * gvolume ; + } + + }else if(strcmp(gwaveform_name, "sqr")){ + + for (int i = 0; i < framesize ; i++, step++){ + frames[i] = square(step * gFrequency / Obtained.freq) * gvolume ; + } + + } +} + +static int +osc(SchedTask *s, void *rbuf, void *wbuf) +{ + AudioData *i_data = (AudioDataPtr)s->get_input(0); + + gvolume = i_data->volume; + gwaveform_name = i_data->waveform_name; + gFrequency = i_data->Frequency; + + Desired.freq= i_data->freq; /* Sampling rate: 44100Hz */ + Desired.format= i_data->format; /* 16-bit signed audio */ + Desired.channels= i_data->channels; /* Mono */ + Desired.samples= i_data->samples; /* Buffer size: 8K = 0.37 sec. */ + Desired.callback= callback; + Desired.userdata= i_data->userdata; + + SDL_OpenAudio(&Desired, &Obtained); + SDL_PauseAudio(0); + SDL_Delay(200); + SDL_Quit(); + + return 0; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/synthesizer/ppe/OSC.h Tue Jul 15 17:23:40 2014 +0900 @@ -0,0 +1,9 @@ +#ifndef INCLUDED_TASK_HELLO +#define INCLUDED_TASK_HELLO + +#ifndef INCLUDED_SCHED_TASK +# include "SchedTask.h" +#endif + + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/synthesizer/spe/Exec.cc Tue Jul 15 17:23:40 2014 +0900 @@ -0,0 +1,45 @@ +#include <stdio.h> +#include <string.h> +#include "Exec.h" +#include "Func.h" + +/* これは必須 */ +SchedDefineTask1(Exec,wordcount); + +static int +wordcount(SchedTask *s, void *rbuf, void *wbuf) +{ + char *i_data = (char *)rbuf; + unsigned long long *o_data = (unsigned long long*)wbuf; + unsigned long long *head_tail_flag = o_data +2; + int length = (int)s->get_inputSize(0); + int word_flag = 0; + int word_num = 0; + int line_num = 0; + int i = 0; + + head_tail_flag[0] = (i_data[0] != 0x20) && (i_data[0] != 0x0A); + word_num -= 1-head_tail_flag[0]; + + for (; i < length; i++) { + if (i_data[i] == 0x20) { + word_flag = 1; + } else if (i_data[i] == 0x0A) { + line_num += 1; + word_flag = 1; + } else { + word_num += word_flag; + word_flag = 0; + } + } + + word_num += word_flag; + head_tail_flag[1] = (i_data[i-1] != 0x20) && (i_data[i-1] != 0x0A); + + // s->printf("SPE word %d line %d\n",word_num,line_num); + + o_data[0] = (unsigned long long)word_num; + o_data[1] = (unsigned long long)line_num; + + return 0; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/synthesizer/spe/Exec.h Tue Jul 15 17:23:40 2014 +0900 @@ -0,0 +1,9 @@ +#ifndef INCLUDED_TASK_HELLO +#define INCLUDED_TASK_HELLO + +#ifndef INCLUDED_SCHED_TASK +# include "SchedTask.h" +#endif + + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/synthesizer/spe/Makefile Tue Jul 15 17:23:40 2014 +0900 @@ -0,0 +1,26 @@ +include ../Makefile.def + +TARGET = ../spe-main + +SRCS_TMP = $(wildcard *.cc) +SRCS = $(filter-out $(SRCS_EXCLUDE),$(SRCS_TMP)) +OBJS = $(SRCS:.cc=.o) + +CC = spu-g++ +CFLAGS = -Wall -fno-exceptions -fno-rtti $(OPT) +INCLUDE = -I../${CERIUM}/include/TaskManager -I. -I.. +LIBS = -L../${CERIUM}/TaskManager -lspemanager + +.SUFFIXES: .cc .o + +.cc.o: + $(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@ + +all: $(TARGET) + +$(TARGET): $(OBJS) + $(CC) -o $@ $(OBJS) $(TASK_OBJS) $(LIBS) + +clean: + rm -f $(TARGET) $(OBJS) + rm -f *~ \#*
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/synthesizer/task_init.cc Tue Jul 15 17:23:40 2014 +0900 @@ -0,0 +1,22 @@ +#include "Func.h" +#include "Scheduler.h" +#ifdef __CERIUM_GPU__ +#include "GpuScheduler.h" +#endif +#ifdef __CERIUM_CUDA__ +#include "CudaScheduler.h" +#endif + +/* 必ずこの位置に書いて */ +SchedExternTask(Audioosc); + +/** + * この関数は ../spe/spe-main と違って + * 自分で呼び出せばいい関数なので + * 好きな関数名でおk (SchedRegisterTask は必須) + */ +void +task_init(void) +{ + SchedRegisterTask(OSC_TASK, Audioosc); +}