Skip to content

Commit

Permalink
added light show functionality for songs
Browse files Browse the repository at this point in the history
  • Loading branch information
christopher committed Feb 23, 2015
1 parent 41cfbd5 commit cd9db6b
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 6 deletions.
5 changes: 0 additions & 5 deletions .dep.inc

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.wav
Empty file added DeviceHandler.cpp
Empty file.
Empty file added DeviceHandler.h
Empty file.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ all:
rm -f infinitylights
g++ -lusb-1.0 main.cpp InfinityPortal.cpp SkylandersPortal.cpp -o infinitylights

lightshow:
rm -f lightshow
g++ -lusb-1.0 lightshow.cpp InfinityPortal.cpp SkylandersPortal.cpp -o lightshow

clean:
rm -f infinitylights
rm -f infinitylights lightshow
70 changes: 70 additions & 0 deletions beat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

if(posix_getuid() != 0) {
die("Please run this application as root/sudo\n");
}

if(!isset($argv[1])) {
throw new Exception("No file selected");
}

$audioFile = $argv[1];
$fileType = mime_content_type($audioFile);

if($fileType != "audio/x-wav") {
throw new Exception("Wrong file type selected");
}

$lightShowProc = proc_open(sprintf("./lightshow",$audioFile),array(0 => array("pipe", "r"),1 => array("pipe", "w")),$lightPipes);

$lightsLine = explode(" ",str_replace("\n","",fgets($lightPipes[1])));

if($lightsLine[0] != "lights") {
throw new Exception("Output formatting error");
}

$lightCount = $lightsLine[1];

$lightCount = max($lightCount,3);

$soxProc = proc_open(sprintf("sox %s -t dat -r 32 -c %d - 2>&1",$audioFile,$lightCount),array(0 => array("pipe", "r"),1 => array("pipe", "w")),$pipes);

$soxOut = $pipes[1];

$channelLast = array();

$startTime = microtime(true);

proc_open(sprintf("play %s 2>&1 &",$audioFile),array(0 => array("pipe", "r"),1 => array("pipe", "w")),$pipesAudio);

echo "Starting the music!\n";

while(!feof($soxOut)) {

$line = trim(str_replace(array("\n","\r"),"",fgets($soxOut)));

if(strpos($line,";") === 0 || !$line) {
continue;
}

// $data = explode(" ",$line);
$data = preg_split("/ +/",$line);
$time = $data[0];

$currTime = microtime(true)-$startTime;

while($currTime < $time) {
$currTime = microtime(true)-$startTime;
}

// var_dump("It's $currTime");

for($i = 1 ; $i <= $lightCount ; $i++) {
// if(isset($channelLast[$i-1]) && $data[$i] < $channelLast[$i-1]) {
if($data[$i] > 0) {
fwrite($lightPipes[0],$i."\n");
}

$channelLast[$i-1] = $data[$i];
}
}
Binary file added lightshow
Binary file not shown.
122 changes: 122 additions & 0 deletions lightshow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* File: main.cpp
* Author: christopher
*
* Created on 10 August 2014, 21:09
*/

#include <cstdlib>
#include <stdio.h>
#include <sys/types.h>
#include <iostream>
#include <math.h>
#include "libusb-1.0/libusb.h"
#include <unistd.h>
#include "InfinityPortal.h"
#include "SkylandersPortal.h"

using namespace std;

/*
*
*/
int main(int argc, char** argv) {

srand (time(NULL));

libusb_device** devices;
libusb_context* context;
struct libusb_device_handle* tryDeviceHandler;

libusb_init(&context);
int devicesCount = libusb_get_device_list(context, &devices);

struct libusb_device_descriptor descriptor;
libusb_device_handle* deviceHandler;

int retVal = 0;
int skylanderPortalIds[0xff];
int infinityPortalIds[0xff];
int skylanderPortalCount = 0;
int infinityPortalCount = 0;

for(int i = 0 ; i < devicesCount ; i++) {

retVal = libusb_open(devices[i], &tryDeviceHandler);

if(retVal < 0) {
continue;
}

libusb_get_device_descriptor(devices[i], &descriptor);

if(descriptor.idVendor == 0x0e6f && descriptor.idProduct == 0x0129) {

infinityPortalIds[infinityPortalCount] = i;
infinityPortalCount++;

} else if(descriptor.idVendor == 0x1430 && descriptor.idProduct == 0x150) {

skylanderPortalIds[skylanderPortalCount] = i;
skylanderPortalCount++;
}
}

if(skylanderPortalCount == 0 && infinityPortalCount == 0) {
printf("Please plug in either a Portal of Power or an Infinity Base\n");
return -1;
}

InfinityPortal infinityPortals[infinityPortalCount];
SkylandersPortal skylanderPortals[skylanderPortalCount];

int j;

int lightsCount = 0;

for(j = 0 ; j < infinityPortalCount ; j++) {
infinityPortals[j] = InfinityPortal(infinityPortalIds[j]);
lightsCount += 3;
}

for(j = 0 ; j < skylanderPortalCount ; j++) {
skylanderPortals[j] = SkylandersPortal(skylanderPortalIds[j]);
lightsCount += 1;
}

printf("lights %d\n",lightsCount);

// while(true) {
// for(j = 0 ; j < max(skylanderPortalCount,infinityPortalCount) ; j++) {
// if(j < skylanderPortalCount) {
// skylanderPortals[j].setColour(random()%0x100,random()%0x100,random()%0x100);
// // skylanderPortals[j].setLeftColour(random()%0x100,random()%0x100,random()%0x100);
// // skylanderPortals[j].setRightColour(random()%0x100,random()%0x100,random()%0x100);
// // skylanderPortals[j].flashTrapLight();
// }

// if(j < infinityPortalCount) {
// for(int k = 0 ; k < 3 ; k++) {
// infinityPortals[j].setColour(k+1,random()%0x100,random()%0x100,random()%0x100);
// }
// }
// }

// usleep(100000);
// }

for (std::string line; std::getline(std::cin, line);) {
int intVal = atoi(line.c_str());
// printf("Got line: %d\n",intVal);

if(intVal > lightsCount) {
continue;
}

skylanderPortals[intVal-1].setColour(random()%0x100,random()%0x100,random()%0x100);

}

printf("Done!\n");
return 0;
}
2 changes: 2 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ int main(int argc, char** argv) {
if(j < skylanderPortalCount) {
// printf("Doing %d\n",j);
skylanderPortals[j].setColour(random()%0x100,random()%0x100,random()%0x100);
// skylanderPortals[j].setLeftColour(random()%0x100,random()%0x100,random()%0x100);
// skylanderPortals[j].setRightColour(random()%0x100,random()%0x100,random()%0x100);
// skylanderPortals[j].flashTrapLight();
}

Expand Down

0 comments on commit cd9db6b

Please sign in to comment.