1 | /*
|
---|
2 | * Copyright (c) 2014 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup edit
|
---|
30 | * @brief Amiga music module player.
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 | /**
|
---|
34 | * @file
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <errno.h>
|
---|
38 | #include <hound/client.h>
|
---|
39 | #include <io/console.h>
|
---|
40 | #include <stdio.h>
|
---|
41 | #include <stdlib.h>
|
---|
42 | #include <trackmod.h>
|
---|
43 |
|
---|
44 | static bool quit = false;
|
---|
45 |
|
---|
46 | static void modplay_key_press(kbd_event_t *ev)
|
---|
47 | {
|
---|
48 | if ((ev->mods & KM_ALT) == 0 &&
|
---|
49 | (ev->mods & KM_SHIFT) == 0 &&
|
---|
50 | (ev->mods & KM_CTRL) != 0) {
|
---|
51 | if (ev->key == KC_Q)
|
---|
52 | quit = true;
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | static void modplay_event(cons_event_t *event)
|
---|
57 | {
|
---|
58 | switch (event->type) {
|
---|
59 | case CEV_KEY:
|
---|
60 | if (event->ev.key.type == KEY_PRESS) {
|
---|
61 | modplay_key_press(&event->ev.key);
|
---|
62 | }
|
---|
63 | break;
|
---|
64 | default:
|
---|
65 | break;
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | int main(int argc, char *argv[])
|
---|
70 | {
|
---|
71 | trackmod_module_t *mod;
|
---|
72 | trackmod_modplay_t *modplay;
|
---|
73 | hound_context_t *hound;
|
---|
74 | console_ctrl_t *con;
|
---|
75 | cons_event_t event;
|
---|
76 | suseconds_t timeout;
|
---|
77 | pcm_format_t format;
|
---|
78 | void *buffer;
|
---|
79 | size_t buffer_size;
|
---|
80 | int rc;
|
---|
81 |
|
---|
82 | if (argc != 2) {
|
---|
83 | printf("syntax: modplay <filename.mod>\n");
|
---|
84 | return 1;
|
---|
85 | }
|
---|
86 |
|
---|
87 | con = console_init(stdin, stdout);
|
---|
88 |
|
---|
89 | rc = trackmod_module_load(argv[1], &mod);
|
---|
90 | if (rc != EOK) {
|
---|
91 | printf("Error loading %s.\n", argv[1]);
|
---|
92 | return 1;
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | format.channels = 1;
|
---|
97 | format.sampling_rate = 44100;
|
---|
98 | #ifdef __LE__
|
---|
99 | format.sample_format = PCM_SAMPLE_SINT16_LE;
|
---|
100 | #else
|
---|
101 | format.sample_format = PCM_SAMPLE_SINT16_BE;
|
---|
102 | #endif
|
---|
103 | buffer_size = 64 * 1024;
|
---|
104 |
|
---|
105 | buffer = malloc(buffer_size);
|
---|
106 | if (buffer == NULL) {
|
---|
107 | printf("Error allocating audio buffer.\n");
|
---|
108 | return 1;
|
---|
109 | }
|
---|
110 |
|
---|
111 | hound = hound_context_create_playback(argv[1], format, buffer_size);
|
---|
112 | if (hound == NULL) {
|
---|
113 | printf("Error creating playback context.\n");
|
---|
114 | return 1;
|
---|
115 | }
|
---|
116 |
|
---|
117 | rc = hound_context_connect_target(hound, HOUND_DEFAULT_TARGET);
|
---|
118 | if (rc != EOK) {
|
---|
119 | printf("Error connecting default audio target (%d).\n", rc);
|
---|
120 | return 1;
|
---|
121 | }
|
---|
122 |
|
---|
123 | rc = trackmod_modplay_create(mod, format.sampling_rate, &modplay);
|
---|
124 | if (rc != EOK) {
|
---|
125 | printf("Error setting up playback.\n");
|
---|
126 | return 1;
|
---|
127 | }
|
---|
128 |
|
---|
129 | printf("Playing '%s'. Press Ctrl+Q to quit.\n", argv[1]);
|
---|
130 |
|
---|
131 | while (true) {
|
---|
132 | timeout = 0;
|
---|
133 | if (console_get_event_timeout(con, &event, &timeout))
|
---|
134 | modplay_event(&event);
|
---|
135 | if (quit)
|
---|
136 | break;
|
---|
137 |
|
---|
138 | trackmod_modplay_get_samples(modplay, buffer, buffer_size / 4);
|
---|
139 |
|
---|
140 | rc = hound_write_main_stream(hound, buffer, buffer_size / 4);
|
---|
141 | if (rc != EOK) {
|
---|
142 | printf("Error writing audio stream.\n");
|
---|
143 | break;
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | hound_context_destroy(hound);
|
---|
148 | trackmod_modplay_destroy(modplay);
|
---|
149 | trackmod_module_destroy(mod);
|
---|
150 |
|
---|
151 | return 0;
|
---|
152 | }
|
---|
153 |
|
---|
154 |
|
---|
155 | /** @}
|
---|
156 | */
|
---|