Subversion Repositories freemyipod

Rev

Rev 715 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
714 user890104 1
//
2
//
3
//    Copyright 2011 TheSeven, user890104
4
//
5
//
6
//    This file is part of emCORE.
7
//
8
//    emCORE is free software: you can redistribute it and/or
9
//    modify it under the terms of the GNU General Public License as
10
//    published by the Free Software Foundation, either version 2 of the
11
//    License, or (at your option) any later version.
12
//
13
//    emCORE is distributed in the hope that it will be useful,
14
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
//    See the GNU General Public License for more details.
17
//
18
//    You should have received a copy of the GNU General Public License along
19
//    with emCORE.  If not, see <http://www.gnu.org/licenses/>.
20
//
21
//
22
 
23
 
24
#include "emcoreapp.h"
25
#include "beep.h"
26
 
27
void playsong(void *buf, size_t size);
28
 
29
static void main()
30
{
31
    if (0x47324e49 != get_platform_id()) // IN2G
32
    {
33
        cputs(3, "Your device is not supported!\n");
34
        return;
35
    }
36
 
37
    size_t size;
38
    int fd;
39
    void *buf;
40
    unsigned char play;
41
 
42
    play = 0;
43
    fd = file_open("/song.dat", O_RDONLY);
44
 
45
    if (fd <= 0)
46
    {
47
        cputs(3, "Unable to open /song.dat!\n");
48
        return;
49
    }
50
 
51
    cputs(3, "File opened\n");
52
    size = filesize(fd);
53
 
54
    if (size <= 0)
55
    {
56
        close(fd);
57
        cputs(3, "Unable to get file size or file is empty!\n");
58
        return;
59
    }
60
 
61
    cprintf(3, "File size: %d bytes\n", size);
62
 
63
    if (0 != size % 8)
64
    {
65
        close(fd);
66
        cputs(3, "Invalid file, unable to continue!\n");
67
        return;
68
    }
69
 
70
    buf = memalign(0x10, size);
71
 
72
    if (!buf)
73
    {
74
        close(fd);
75
        cputs(3, "Memory allocation failed!\n");
76
        return;
77
    }
78
 
79
    if (size != read(fd, buf, size))
80
    {
81
        free(buf);
82
        close(fd);
83
        cputs(3, "Read error!\n");
84
        return;
85
    }
86
 
87
    cputs(3, "Read successful\n");
88
    close(fd);
89
    cputs(3, "File closed\n");
90
    playsong(buf, size);
91
    free(buf);
92
}
93
 
94
void playsong(void *buf, size_t size) {
95
    int i;
96
    unsigned int cycles[size / 8], lengths[size / 8];
97
 
98
    for (i = 0; i < size / 8; ++i)
99
    {
100
        cycles[i] = *((unsigned int *)(buf) + i + i);
101
        lengths[i] = *((unsigned int *)(buf) + i + i + 1);
102
    }
103
 
104
    for (i = 0; i < size / 8; ++i)
105
    {
106
        if (0 == cycles[i])
107
        {
108
            sleep(lengths[i]);
109
            continue;
110
        }
111
 
112
        singlebeep(cycles[i], lengths[i]);
113
    }
114
}
115
 
116
EMCORE_APP_HEADER("Beeper", main, 127)