Skip to content

Instantly share code, notes, and snippets.

@dopuskh3
Created January 25, 2010 13:01
Show Gist options
  • Save dopuskh3/285848 to your computer and use it in GitHub Desktop.
Save dopuskh3/285848 to your computer and use it in GitHub Desktop.
/*
CMakeLists.txt file:
cmake_minimum_required(VERSION 2.6)
project(wiispeak)
include_directories("/usr/include/libcwiimote/")
find_library(cwiimote cwiimote PATHS /usr/lib)
find_library(bluetooth bluetooth PATHS /usr/lib)
add_executable(wiispeak
test.c)
target_link_libraries(wiispeak ${cwiimote} ${bluetooth} "-lm")
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "wiimote_api.h"
static const int index_table[16] = { -1, -1, -1, -1, 2, 4, 6, 8,
-1, -1, -1, -1, 2, 4, 6, 8 };
static const int diff_table [16] = { 1, 3, 5, 7, 9, 11, 13, 15,
-1, -3, -5, -7, -9, -11, -13, 15 };
static const int step_scale [16] = { 230, 230, 230, 230, 307, 409, 512, 614,
230, 230, 230, 230, 307
, 409, 512, 614 };
int sample_convert(const short *input, uint8_t *buffer, int len){
memset(buffer, len, sizeof(uint8_t));
// from wwiiyourself code
// Encode to ADPCM, on initialization set adpcm_prev_value to 0 and adpcm_step
// to 127 (these variables must be preserved across reports)
int adpcm_prev_value = 0;
int adpcm_step = 127;
size_t i;
for(i=0; i<len; i++){
// convert to 16bit signed
int value = input[i];// (8bit) << 8);// | samples[i]; // dither it?
//value -= 32768;
// encode:
int diff = value - adpcm_prev_value;
unsigned char encoded_val = 0;
if(diff < 0) {
encoded_val |= 8;
diff = -diff;
}
diff = (diff << 2) / adpcm_step;
if (diff > 7)
diff = 7;
encoded_val |= diff;
adpcm_prev_value += ((adpcm_step * diff_table[encoded_val]) / 8);
if(adpcm_prev_value > 0x7fff)
adpcm_prev_value = 0x7fff;
if(adpcm_prev_value < -0x8000)
adpcm_prev_value = -0x8000;
adpcm_step = (adpcm_step * step_scale[encoded_val]) >> 8;
if(adpcm_step < 127)
adpcm_step = 127;
if(adpcm_step > 24567)
adpcm_step = 24567;
if(i & 1)
buffer[i>>1] |= encoded_val;
else
buffer[i>>1] |= encoded_val << 4;
}
printf("Converted\n");
}
int main()
{
wiimote_t wi = WIIMOTE_INIT;
uint8_t buffer[256];
short input[256];
if ( wiimote_connect(&wi, "00:23:CC:A1:85:6D") < 0){
fprintf(stderr, "Cannot connect\n");
return 1;
}
wi.mode.acc = 1;
wiimote_speaker_enable(&wi);
wiimote_speaker_init(&wi, WIIMOTE_FMT_S8, WIIMOTE_FREQ_44800HZ);
while (wiimote_is_open(&wi)) {
wiimote_update(&wi); // synchronize with wiimote
fread(input, sizeof(uint8_t), 20, stdin);
sample_convert(input, buffer, 20);
printf("Readed\n");
if (wi.keys.home) { // check if home key is pressed
wiimote_disconnect(&wi);
}
wiimote_speaker_freq(&wi, WIIMOTE_FREQ_44800HZ);
wiimote_speaker_play(&wi, buffer, 20);
}
// play a short sound sample
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment