一段控制红外线的代码

来源:互联网 发布:淘宝高仿鞋子 编辑:程序博客网 时间:2024/04/18 15:05
 
#include <pspkernel.h>
#include 
<pspdebug.h>
#include 
<pspaudiolib.h>
#include 
<stdlib.h>
#include 
<math.h>
#include 
<string.h>

/* Define the module info section */
PSP_MODULE_INFO(
"POLYPHONIC"011);

/* Define the main thread's attribute value (optional) */
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER 
| THREAD_ATTR_VFPU);

/* Define printf, just to make typing easier */
#define printf    pspDebugScreenPrintf

void dump_threadstatus(void);

/* Exit callback */
int exit_callback(int arg1, int arg2, void *common)
{
    sceKernelExitGame();

    
return 0;
}

/* Callback thread */
int CallbackThread(SceSize args, void *argp)
{
    
int cbid;

    cbid 
= sceKernelCreateCallback("Exit Callback", (void *) exit_callback, NULL);
    sceKernelRegisterExitCallback(cbid);

    sceKernelSleepThreadCB();

    
return 0;
}

/* Sets up the callback thread and returns its thread id */
int SetupCallbacks(void)
{
    
int thid = 0;

    thid 
= sceKernelCreateThread("update_thread", CallbackThread, 0x110xFA000);
    
if(thid >= 0)
    {
        sceKernelStartThread(thid, 
00);
    }

    
return thid;
}

int  pspAudioInit();
void pspAudioEndPre();
void pspAudioEnd();

#define SAMPLE_COUNT 0x10000
float sample[SAMPLE_COUNT];

#define SAMPLE_RATE 44100

#define OCTAVE_COUNT 6

float octaves[OCTAVE_COUNT][12];

typedef 
struct {
    
int note;
    
int octave;
    
int duration;
} Note_t;

typedef 
struct {
    Note_t currentNote;
    
int noteIndex;
    
int currentTime;
    
float currentsampleIndex;
    
float currentsampleIncrement;
} ChannelState_t;

ChannelState_t channelStates[
3];

// "S" means "#"
#define NOTE_END -2
#define NOTE_PAUSE -1
#define NOTE_C 0
#define NOTE_CS 1
#define NOTE_D 2
#define NOTE_DS 3
#define NOTE_E 4
#define NOTE_F 5
#define NOTE_FS 6
#define NOTE_G 7
#define NOTE_GS 8
#define NOTE_A 9
#define NOTE_AS 10
#define NOTE_B 11

#define EIGHT_NOTE(note, octave, duration) { note, octave, SAMPLE_RATE * duration / 8}

Note_t channel0[] 
= {
    EIGHT_NOTE(NOTE_D, 
47),
    EIGHT_NOTE(NOTE_E, 
41),
    EIGHT_NOTE(NOTE_F, 
41),
    EIGHT_NOTE(NOTE_E, 
41),
    EIGHT_NOTE(NOTE_F, 
41),
    EIGHT_NOTE(NOTE_E, 
41),
    EIGHT_NOTE(NOTE_A, 
39),
    EIGHT_NOTE(NOTE_B, 
32),
    EIGHT_NOTE(NOTE_C, 
41),
    EIGHT_NOTE(NOTE_D, 
47),
    EIGHT_NOTE(NOTE_E, 
41),
    EIGHT_NOTE(NOTE_F, 
41),
    EIGHT_NOTE(NOTE_E, 
41),
    EIGHT_NOTE(NOTE_F, 
41),
    EIGHT_NOTE(NOTE_E, 
41),
    EIGHT_NOTE(NOTE_A, 
39),
    EIGHT_NOTE(NOTE_G, 
33),
    EIGHT_NOTE(NOTE_A, 
37),
    EIGHT_NOTE(NOTE_B, 
31),
    EIGHT_NOTE(NOTE_C, 
41),
    EIGHT_NOTE(NOTE_B, 
31),
    EIGHT_NOTE(NOTE_C, 
41),
    EIGHT_NOTE(NOTE_B, 
31),
    EIGHT_NOTE(NOTE_E, 
39),
    EIGHT_NOTE(NOTE_F, 
32),
    EIGHT_NOTE(NOTE_G, 
31),
    EIGHT_NOTE(NOTE_A, 
37),
    EIGHT_NOTE(NOTE_B, 
31),
    EIGHT_NOTE(NOTE_C, 
41),
    EIGHT_NOTE(NOTE_B, 
31),
    EIGHT_NOTE(NOTE_C, 
41),
    EIGHT_NOTE(NOTE_B, 
31),
    EIGHT_NOTE(NOTE_E, 
312),
    EIGHT_NOTE(NOTE_D, 
49),
    EIGHT_NOTE(NOTE_C, 
43),
    EIGHT_NOTE(NOTE_B, 
36),
    EIGHT_NOTE(NOTE_A, 
36),
    EIGHT_NOTE(NOTE_D, 
47),
    EIGHT_NOTE(NOTE_E, 
41),
    EIGHT_NOTE(NOTE_E, 
41),
    EIGHT_NOTE(NOTE_C, 
41),
    EIGHT_NOTE(NOTE_B, 
31),
    EIGHT_NOTE(NOTE_C, 
41),
    EIGHT_NOTE(NOTE_B, 
36),
    EIGHT_NOTE(NOTE_A, 
36),
    EIGHT_NOTE(NOTE_C, 
49),
    EIGHT_NOTE(NOTE_B, 
33),
    EIGHT_NOTE(NOTE_E, 
312),
    EIGHT_NOTE(NOTE_C, 
47),
    EIGHT_NOTE(NOTE_D, 
41),
    EIGHT_NOTE(NOTE_C, 
41),
    EIGHT_NOTE(NOTE_B, 
31),
    EIGHT_NOTE(NOTE_C, 
41),
    EIGHT_NOTE(NOTE_B, 
31),
    EIGHT_NOTE(NOTE_E, 
312),
    { NOTE_END, 
00 }
};

Note_t channel1[] 
= {
    EIGHT_NOTE(NOTE_D, 
11),
    EIGHT_NOTE(NOTE_A, 
11),
    EIGHT_NOTE(NOTE_D, 
21),
    EIGHT_NOTE(NOTE_E, 
21),
    EIGHT_NOTE(NOTE_F, 
21),
    EIGHT_NOTE(NOTE_A, 
21),
    EIGHT_NOTE(NOTE_F, 
21),
    EIGHT_NOTE(NOTE_E, 
21),
    EIGHT_NOTE(NOTE_D, 
21),
    EIGHT_NOTE(NOTE_A, 
11),
    EIGHT_NOTE(NOTE_D, 
11),
    EIGHT_NOTE(NOTE_A, 
11),
    EIGHT_NOTE(NOTE_A, 
01),
    EIGHT_NOTE(NOTE_E, 
11),
    EIGHT_NOTE(NOTE_A, 
11),
    EIGHT_NOTE(NOTE_B, 
11),
    EIGHT_NOTE(NOTE_C, 
21),
    EIGHT_NOTE(NOTE_E, 
21),
    EIGHT_NOTE(NOTE_C, 
21),
    EIGHT_NOTE(NOTE_B, 
11),
    EIGHT_NOTE(NOTE_A, 
11),
    EIGHT_NOTE(NOTE_E, 
11),
    EIGHT_NOTE(NOTE_A, 
01),
    EIGHT_NOTE(NOTE_E, 
11),
    EIGHT_NOTE(NOTE_D, 
11),
    EIGHT_NOTE(NOTE_A, 
11),
    EIGHT_NOTE(NOTE_D, 
21),
    EIGHT_NOTE(NOTE_E, 
21),
    EIGHT_NOTE(NOTE_F, 
21),
    EIGHT_NOTE(NOTE_A, 
21),
    EIGHT_NOTE(NOTE_F, 
21),
    EIGHT_NOTE(NOTE_E, 
21),
    EIGHT_NOTE(NOTE_D, 
21),
    EIGHT_NOTE(NOTE_A, 
11),
    EIGHT_NOTE(NOTE_D, 
11),
    EIGHT_NOTE(NOTE_A, 
11),
    EIGHT_NOTE(NOTE_A, 
01),
    EIGHT_NOTE(NOTE_E, 
11),
    EIGHT_NOTE(NOTE_A, 
11),
    EIGHT_NOTE(NOTE_B, 
11),
    EIGHT_NOTE(NOTE_C, 
21),
    EIGHT_NOTE(NOTE_E, 
21),
    EIGHT_NOTE(NOTE_C, 
21),
    EIGHT_NOTE(NOTE_B, 
11),
    EIGHT_NOTE(NOTE_A, 
11),
    EIGHT_NOTE(NOTE_E, 
11),
    EIGHT_NOTE(NOTE_A, 
01),
    EIGHT_NOTE(NOTE_E, 
11),
    EIGHT_NOTE(NOTE_F, 
11),
    EIGHT_NOTE(NOTE_C, 
21),
    EIGHT_NOTE(NOTE_F, 
21),
    EIGHT_NOTE(NOTE_G, 
21),
    EIGHT_NOTE(NOTE_A, 
21),
    EIGHT_NOTE(NOTE_C, 
31),
    EIGHT_NOTE(NOTE_A, 
21),
    EIGHT_NOTE(NOTE_G, 
21),
    EIGHT_NOTE(NOTE_F, 
21),
    EIGHT_NOTE(NOTE_C, 
21),
    EIGHT_NOTE(NOTE_F, 
11),
    EIGHT_NOTE(NOTE_C, 
21),
    EIGHT_NOTE(NOTE_C, 
11),
    EIGHT_NOTE(NOTE_G, 
11),
    EIGHT_NOTE(NOTE_C, 
21),
    EIGHT_NOTE(NOTE_D, 
21),
    EIGHT_NOTE(NOTE_E, 
21),
    EIGHT_NOTE(NOTE_G, 
21),
    EIGHT_NOTE(NOTE_E, 
21),
    EIGHT_NOTE(NOTE_D, 
21),
    EIGHT_NOTE(NOTE_C, 
21),
    EIGHT_NOTE(NOTE_G, 
11),
    EIGHT_NOTE(NOTE_C, 
11),
    EIGHT_NOTE(NOTE_G, 
11),
    EIGHT_NOTE(NOTE_F, 
11),
    EIGHT_NOTE(NOTE_C, 
21),
    EIGHT_NOTE(NOTE_F, 
21),
    EIGHT_NOTE(NOTE_G, 
21),
    EIGHT_NOTE(NOTE_A, 
21),
    EIGHT_NOTE(NOTE_C, 
31),
    EIGHT_NOTE(NOTE_A, 
21),
    EIGHT_NOTE(NOTE_G, 
21),
    EIGHT_NOTE(NOTE_F, 
21),
    EIGHT_NOTE(NOTE_C, 
21),
    EIGHT_NOTE(NOTE_F, 
11),
    EIGHT_NOTE(NOTE_C, 
21),
    EIGHT_NOTE(NOTE_C, 
11),
    EIGHT_NOTE(NOTE_G, 
11),
    EIGHT_NOTE(NOTE_C, 
21),
    EIGHT_NOTE(NOTE_D, 
21),
    EIGHT_NOTE(NOTE_E, 
21),
    EIGHT_NOTE(NOTE_G, 
21),
    EIGHT_NOTE(NOTE_E, 
21),
    EIGHT_NOTE(NOTE_D, 
21),
    EIGHT_NOTE(NOTE_C, 
21),
    EIGHT_NOTE(NOTE_G, 
11),
    EIGHT_NOTE(NOTE_C, 
11),
    EIGHT_NOTE(NOTE_G, 
11),
    EIGHT_NOTE(NOTE_D, 
11),
    EIGHT_NOTE(NOTE_A, 
11),
    EIGHT_NOTE(NOTE_D, 
21),
    EIGHT_NOTE(NOTE_E, 
21),
    EIGHT_NOTE(NOTE_F, 
21),
    EIGHT_NOTE(NOTE_A, 
21),
    EIGHT_NOTE(NOTE_F, 
21),
    EIGHT_NOTE(NOTE_E, 
21),
    EIGHT_NOTE(NOTE_D, 
21),
    EIGHT_NOTE(NOTE_A, 
11),
    EIGHT_NOTE(NOTE_D, 
11),
    EIGHT_NOTE(NOTE_A, 
11),
    EIGHT_NOTE(NOTE_A, 
01),
    EIGHT_NOTE(NOTE_E, 
11),
    EIGHT_NOTE(NOTE_A, 
11),
    EIGHT_NOTE(NOTE_B, 
11),
    EIGHT_NOTE(NOTE_C, 
21),
    EIGHT_NOTE(NOTE_E, 
21),
    EIGHT_NOTE(NOTE_C, 
21),
    EIGHT_NOTE(NOTE_B, 
11),
    EIGHT_NOTE(NOTE_A, 
11),
    EIGHT_NOTE(NOTE_E, 
11),
    EIGHT_NOTE(NOTE_A, 
01),
    EIGHT_NOTE(NOTE_E, 
11),
    EIGHT_NOTE(NOTE_D, 
11),
    EIGHT_NOTE(NOTE_A, 
11),
    EIGHT_NOTE(NOTE_D, 
21),
    EIGHT_NOTE(NOTE_E, 
21),
    EIGHT_NOTE(NOTE_F, 
21),
    EIGHT_NOTE(NOTE_A, 
21),
    EIGHT_NOTE(NOTE_F, 
21),
    EIGHT_NOTE(NOTE_E, 
21),
    EIGHT_NOTE(NOTE_D, 
21),
    EIGHT_NOTE(NOTE_A, 
11),
    EIGHT_NOTE(NOTE_D, 
11),
    EIGHT_NOTE(NOTE_A, 
11),
    EIGHT_NOTE(NOTE_A, 
01),
    EIGHT_NOTE(NOTE_E, 
11),
    EIGHT_NOTE(NOTE_A, 
11),
    EIGHT_NOTE(NOTE_B, 
11),
    EIGHT_NOTE(NOTE_C, 
21),
    EIGHT_NOTE(NOTE_E, 
21),
    EIGHT_NOTE(NOTE_C, 
21),
    EIGHT_NOTE(NOTE_B, 
11),
    EIGHT_NOTE(NOTE_A, 
11),
    EIGHT_NOTE(NOTE_E, 
11),
    EIGHT_NOTE(NOTE_A, 
01),
    EIGHT_NOTE(NOTE_E, 
11),
    EIGHT_NOTE(NOTE_F, 
11),
    EIGHT_NOTE(NOTE_C, 
21),
    EIGHT_NOTE(NOTE_F, 
21),
    EIGHT_NOTE(NOTE_G, 
21),
    EIGHT_NOTE(NOTE_A, 
21),
    EIGHT_NOTE(NOTE_C, 
31),
    EIGHT_NOTE(NOTE_A, 
21),
    EIGHT_NOTE(NOTE_G, 
21),
    EIGHT_NOTE(NOTE_F, 
21),
    EIGHT_NOTE(NOTE_C, 
21),
    EIGHT_NOTE(NOTE_F, 
11),
    EIGHT_NOTE(NOTE_C, 
21),
    EIGHT_NOTE(NOTE_C, 
11),
    EIGHT_NOTE(NOTE_G, 
11),
    EIGHT_NOTE(NOTE_C, 
21),
    EIGHT_NOTE(NOTE_D, 
21),
    EIGHT_NOTE(NOTE_E, 
21),
    EIGHT_NOTE(NOTE_G, 
21),
    EIGHT_NOTE(NOTE_E, 
21),
    EIGHT_NOTE(NOTE_D, 
21),
    EIGHT_NOTE(NOTE_C, 
21),
    EIGHT_NOTE(NOTE_G, 
11),
    EIGHT_NOTE(NOTE_C, 
11),
    EIGHT_NOTE(NOTE_G, 
11),
    EIGHT_NOTE(NOTE_F, 
11),
    EIGHT_NOTE(NOTE_C, 
21),
    EIGHT_NOTE(NOTE_F, 
21),
    EIGHT_NOTE(NOTE_G, 
21),
    EIGHT_NOTE(NOTE_A, 
21),
    EIGHT_NOTE(NOTE_C, 
31),
    EIGHT_NOTE(NOTE_A, 
21),
    EIGHT_NOTE(NOTE_G, 
21),
    EIGHT_NOTE(NOTE_F, 
21),
    EIGHT_NOTE(NOTE_C, 
21),
    EIGHT_NOTE(NOTE_F, 
11),
    EIGHT_NOTE(NOTE_C, 
21),
    EIGHT_NOTE(NOTE_C, 
11),
    EIGHT_NOTE(NOTE_G, 
11),
    EIGHT_NOTE(NOTE_C, 
21),
    EIGHT_NOTE(NOTE_D, 
21),
    EIGHT_NOTE(NOTE_E, 
21),
    EIGHT_NOTE(NOTE_G, 
21),
    EIGHT_NOTE(NOTE_E, 
21),
    EIGHT_NOTE(NOTE_D, 
21),
    EIGHT_NOTE(NOTE_C, 
21),
    EIGHT_NOTE(NOTE_G, 
11),
    EIGHT_NOTE(NOTE_C, 
11),
    EIGHT_NOTE(NOTE_G, 
11),
    { NOTE_END, 
00 }
};

Note_t
* channels[] = { channel0, channel1 };

void nextNote(int channel)
{
    ChannelState_t
* state = &channelStates[channel];
    state
->currentNote = channels[channel][state->noteIndex];
    state
->currentTime = 0;
    state
->currentsampleIndex = 0;
    
int note = state->currentNote.note;
    
if (note == NOTE_PAUSE) {
        state
->currentsampleIncrement = 0;
    } 
else {
        state
->currentsampleIncrement = octaves[state->currentNote.octave][note] * ((float) SAMPLE_COUNT) / ((float) SAMPLE_RATE);
    }

    state
->noteIndex++;
    
if (channels[channel][state->noteIndex].note == NOTE_END) state->noteIndex = 0;
}

// calculate current value of attack/delay/sustain/release envelope
float adsr(float time, float duration) {
    
if (time < 0.0return 0.0;
    
const float attack = 0.004;
    
const float decay = 0.02;
    
const float sustain = 0.5;
    
const float release = 0.08;
    duration 
-= attack + decay + release;
    
if (time < attack) return time / attack;
    time 
-= attack;
    
if (time < decay) return (decay - time) / decay * (1.0 - sustain) + sustain;
    time 
-= decay;
    
if (time < duration) return sustain;
    time 
-= duration;
    
if (time < release) return (release - time) / release * sustain;
    
return 0.0;
}

void audioOutCallback(int channel, unsigned short* buf, unsigned int reqn)
{
    ChannelState_t
* state = &channelStates[channel];
    unsigned 
int i;
    
for (i = 0; i < reqn; i++) {
        
float time = ((float) state->currentTime) / ((float) SAMPLE_RATE);
        
if (state->currentTime++ == state->currentNote.duration) nextNote(channel);
        
float value;
        
if (state->currentsampleIncrement == 0.0) {
            value 
= 0.0;
        } 
else {
            value 
= sample[(int)state->currentsampleIndex] * adsr(time, ((float) state->currentNote.duration) / ((float) SAMPLE_RATE));
            value 
*= (float0x7000;
            state
->currentsampleIndex += state->currentsampleIncrement;
            
if (state->currentsampleIndex >= SAMPLE_COUNT) state->currentsampleIndex -= (float) SAMPLE_COUNT;
        }
        buf[
0= value;
        buf[
1= value;
        buf 
+= 2;
    }
}

void audioOutCallback0(void *buf, unsigned int reqn, void *userdata) { audioOutCallback(0, buf, reqn); }
void audioOutCallback1(void *buf, unsigned int reqn, void *userdata) { audioOutCallback(1, buf, reqn); }

void createPitches(float basefloat* target)
{
    
int i;
    
for (i = 0; i < 12; i++) {
        target[i] 
= base;
        
base *= 1.0594630943592952645618252949463;  // 2^(1/12)
    }
}

int main(void)
{
    pspDebugScreenInit();
    SetupCallbacks();
    printf(
"Polyphonic sample by Shine ");
    printf(
"Soundtrack of the movie ");
    printf(
""Le fabuleux destin d'Amelie Poulain" ");
    printf(
"by Yann Tiersen ");

        
int i;
    
int maxAt = SAMPLE_COUNT / 16;
    
for (i = 0; i < SAMPLE_COUNT; i++) {
        
float value;
        
if (i < maxAt) {
            value 
= ((float) i) / ((float) maxAt) * 2.0 - 1.0;
        } 
else {
            value 
= 1.0 - ((float) (i - maxAt)) / ((float) (SAMPLE_COUNT - maxAt)) * 2.0;
        }
        sample[i] 
= value;
    }
    
float base = 40.0;
    
for (i = 0; i < OCTAVE_COUNT; i++) {
        createPitches(
base, octaves[i]);
        
base *= 2;
    }
    channelStates[
0].noteIndex = 0; nextNote(0);
    channelStates[
1].noteIndex = 0; nextNote(1);

    pspAudioInit();
    pspAudioSetVolume(
00x40000x4000);
    pspAudioSetVolume(
10x40000x4000);
    pspAudioSetChannelCallback(
0, audioOutCallback0, NULL);
    pspAudioSetChannelCallback(
1, audioOutCallback1, NULL);
    sceKernelSleepThread();

    
return 0;
}
原创粉丝点击