summaryrefslogtreecommitdiff
path: root/wiispkr.c
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2011-12-24 11:33:54 +0100
committerDaniel Friesel <derf@finalrewind.org>2011-12-24 11:33:54 +0100
commit1d224769b57199110511522f3fc1a0c119ee3e8c (patch)
tree3ee0670ca920c2f72f65e1f1fd2d9e018d383755 /wiispkr.c
parentef06f59cb716f494dfd7348e01ffa8fd698967a0 (diff)
wiiplay / wiispkr: 2kHz works best here (and you can actually recognize the songs!)
Diffstat (limited to 'wiispkr.c')
-rw-r--r--wiispkr.c38
1 files changed, 30 insertions, 8 deletions
diff --git a/wiispkr.c b/wiispkr.c
index ae783b6..7097cc0 100644
--- a/wiispkr.c
+++ b/wiispkr.c
@@ -4,11 +4,20 @@
* 0. You just DO WHAT THE FUCK YOU WANT TO
*
* Loosely based on libcwiimote/test/test2.c
+ *
+ *
+ * A note on sample rates:
+ * Right now we use 2kHz with 8bit signed pcm.
+ * The speaker eats up to 20 bytes of raw data at once, so this gives us
+ * 100 updates per second (one every 10 us).
+ * Higher rates like 3kHz don't work as well, looks like one report
+ * every 10us is about as fast as the bluetooth chipset / driver here gets.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
+#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
@@ -33,6 +42,10 @@ int main(int argc, char **argv)
{
wiimote_t wiimote = WIIMOTE_INIT;
int readbytes;
+ char report_err = 0;
+
+ wiimote_report_t report = WIIMOTE_REPORT_INIT;
+ report.channel = WIIMOTE_RID_SPK;
struct timeval tv;
uint32_t timeout = 0;
@@ -42,8 +55,8 @@ int main(int argc, char **argv)
uint8_t sample[20];
- uint8_t spk_8bit[] = { 0x00, 0x40, 0x40, 0x1f, 0x60, 0x00, 0x00 };
- uint8_t spk_4bit[] = { 0x00, 0x00, 0xd0, 0x07, 0x40, 0x00, 0x00 };
+ /* see http://wiibrew.org/wiki/Wiimote#Speaker_Configuration */
+ uint8_t spk_8bit[] = { 0x00, 0x40, 0x70, 0x17, 0x60, 0x00, 0x00 };
if (argc < 2) {
@@ -67,7 +80,7 @@ int main(int argc, char **argv)
wiimote_write_byte(&wiimote, 0x04a20009, 0x01);
wiimote_write_byte(&wiimote, 0x04a20001, 0x08);
- wiimote_write(&wiimote, 0x04a20001, spk_8bit, sizeof(spk_4bit));
+ wiimote_write(&wiimote, 0x04a20001, spk_8bit, sizeof(spk_8bit));
wiimote_write_byte(&wiimote, 0x04a20008, 0x01);
wiimote_send_byte(&wiimote, WIIMOTE_RID_SPK_MUTE, 0x00);
@@ -80,16 +93,25 @@ int main(int argc, char **argv)
t = (tv.tv_sec * 1000000 + tv.tv_usec) / 100;
if (t > timeout) {
- printf("\r\033[2Kplay second %f pos %d", (double)(t - start) / 10000, fpos);
+ printf("\r\033[2K%c play second %f pos %d",
+ (report_err ? '!' : ' '),
+ (double)(t - start) / 10000, fpos);
fflush(stdout);
- if ((readbytes = read(0, &sample, sizeof(sample))) == -1)
+ if ((readbytes = read(0, &sample, sizeof(sample))) < 1)
return 0;
fpos += readbytes;
- wiimote_speaker_play(&wiimote, sample, sizeof(sample));
- timeout = t + 130;
+
+ report.speaker.size = sizeof(sample);
+ memcpy(report.speaker.data, sample, report.speaker.size);
+ if (wiimote_report(&wiimote, &report, sizeof(report.speaker)) < 0)
+ report_err = 1;
+ else
+ report_err = 0;
+
+ timeout = t + 100;
}
else
- usleep(timeout - t);
+ usleep((timeout - t) * 10);
}
return 0;