超高音vitasなりきりマイクのプログラム
Kansai.ARで歌ってきました - 無題 @ はてな
これで使っているソースコードを公開するのを忘れていました.
動かしているプラットフォームはDebian GNU/Linux(sid)です.動かすには別途cwiidとsndobjのインストールが必要です.
vitas.cpp
#include <iostream>
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
#include <SndObj/AudioDefs.h>
#include <stdarg.h>
#include <cwiid.h>
#define CWIID_NUM 50
#define VITAS_TIME 3000
#define toggle_bit(bf,b) \
(bf) = ((bf) & b) \
? ((bf) & ~(b)) \
: ((bf) | (b))
int tone = 0;
using namespace std;
void set_rpt_mode(cwiid_wiimote_t *wiimote, unsigned char rpt_mode);
cwiid_err_t err;
void err(cwiid_wiimote_t *wiimote, const char *s, va_list ap)
{
if (wiimote) printf("%d:", cwiid_get_id(wiimote));
else printf("-1:");
vprintf(s, ap);
printf("\n");
}
void cwiid()
{
cwiid_wiimote_t *wiimote; // wiimote handle
struct cwiid_state state; // wiimote state
bdaddr_t bdaddr; // bluetooth device address
unsigned char rpt_mode = 0;
int cnt = 0;
int sum = 0;
cwiid_set_err(err);
bdaddr = *BDADDR_ANY;
// Connect to the wiimote
printf("Put Wiimote in discoverable mode now (press 1+2)...\n");
if(!(wiimote = cwiid_open(&bdaddr, 0))){
fprintf(stderr, "Unable to connect to wiimote\n");
exit(EXIT_FAILURE);
}
printf("### vitas ###\n");
// set ACC rpt mode
toggle_bit(rpt_mode, CWIID_RPT_ACC);
set_rpt_mode(wiimote, rpt_mode);
while(1){
// get acc state
if (cwiid_get_state(wiimote, &state)) {
fprintf(stderr, "Error getting state\n");
}
sum += state.acc[CWIID_Y];
cnt++;
if(cnt>=CWIID_NUM){
sum = sum / CWIID_NUM;
int value = sum - 110;
if(value<0)
value = 0;
tone = value;
cnt = 0;
sum = 0;
}
}
if(cwiid_close(wiimote)){
fprintf(stderr, "Error on wiimote disconnect\n");
exit(EXIT_FAILURE);
}
}
void set_rpt_mode(cwiid_wiimote_t *wiimote, unsigned char rpt_mode)
{
if(cwiid_set_rpt_mode(wiimote, rpt_mode)){
fprintf(stderr, "Error setting report mode\n");
}
}
void vitas()
{
int dur =VITAS_TIME;
SndRTIO input(1, SND_INPUT);
SndIn audio(&input);
Gain gain(-3.f, &audio);
//Comb comb(0.9f, 0.008f, &gain); //robot
Comb comb(0.5f, 0.1f, &gain);
Pitch pitch(.1f, &comb, 0);
SndRTIO output(1, SND_OUTPUT);
output.SetOutput(1, &gain);
output.SetOutput(1, &comb);
output.SetOutput(1, &pitch);
for(int i=0; i<dur*(44100/DEF_VECSIZE); i++){
printf("tone = %d\n", tone);
pitch.SetPitch(tone);
input.Read();
audio.DoProcess();
gain.DoProcess();
comb.DoProcess();
pitch.DoProcess();
output.Write();
}
}
int main()
{
boost::thread th0(&cwiid);
boost::thread th1(&vitas);
th0.join();
th1.join();
return 0;
}
Makefile
TARGET = vitas CC = g++ CFLGS = -g -O2 -fPIC -DALSA LIBS = -lstdc++ -lm -lsndobj -lasound -lboost_thread -lcwiid SRCS = $(TARGET).cpp OBJS = $(SRCS:.cpp=.o) all: $(TARGET) $(TARGET): $(OBJS) $(CC) $(CFLGS) -o $(TARGET) $(OBJS) $(LIBS) %.o:%.cpp $(CC) $(CFLGS) -c $< clean: rm -rf *.o $(TARGET) *~