[7e69e0e] | 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>
|
---|
[c1694b6b] | 42 | #include <str_error.h>
|
---|
[7e69e0e] | 43 | #include <trackmod.h>
|
---|
| 44 |
|
---|
| 45 | static bool quit = false;
|
---|
| 46 |
|
---|
| 47 | static void modplay_key_press(kbd_event_t *ev)
|
---|
| 48 | {
|
---|
| 49 | if ((ev->mods & KM_ALT) == 0 &&
|
---|
| 50 | (ev->mods & KM_SHIFT) == 0 &&
|
---|
| 51 | (ev->mods & KM_CTRL) != 0) {
|
---|
| 52 | if (ev->key == KC_Q)
|
---|
| 53 | quit = true;
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | static void modplay_event(cons_event_t *event)
|
---|
| 58 | {
|
---|
| 59 | switch (event->type) {
|
---|
| 60 | case CEV_KEY:
|
---|
| 61 | if (event->ev.key.type == KEY_PRESS) {
|
---|
| 62 | modplay_key_press(&event->ev.key);
|
---|
| 63 | }
|
---|
| 64 | break;
|
---|
| 65 | default:
|
---|
| 66 | break;
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | int main(int argc, char *argv[])
|
---|
| 71 | {
|
---|
| 72 | trackmod_module_t *mod;
|
---|
| 73 | trackmod_modplay_t *modplay;
|
---|
| 74 | hound_context_t *hound;
|
---|
| 75 | console_ctrl_t *con;
|
---|
| 76 | cons_event_t event;
|
---|
| 77 | suseconds_t timeout;
|
---|
| 78 | pcm_format_t format;
|
---|
| 79 | void *buffer;
|
---|
| 80 | size_t buffer_size;
|
---|
[b7fd2a0] | 81 | errno_t rc;
|
---|
[7e69e0e] | 82 |
|
---|
| 83 | if (argc != 2) {
|
---|
| 84 | printf("syntax: modplay <filename.mod>\n");
|
---|
| 85 | return 1;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | con = console_init(stdin, stdout);
|
---|
| 89 |
|
---|
[43dd72b7] | 90 | rc = trackmod_module_load(argv[1], &mod);
|
---|
[7e69e0e] | 91 | if (rc != EOK) {
|
---|
| 92 | printf("Error loading %s.\n", argv[1]);
|
---|
| 93 | return 1;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 |
|
---|
| 97 | format.channels = 1;
|
---|
| 98 | format.sampling_rate = 44100;
|
---|
| 99 | #ifdef __LE__
|
---|
| 100 | format.sample_format = PCM_SAMPLE_SINT16_LE;
|
---|
| 101 | #else
|
---|
| 102 | format.sample_format = PCM_SAMPLE_SINT16_BE;
|
---|
| 103 | #endif
|
---|
[6609050] | 104 | buffer_size = 64 * 1024;
|
---|
[7e69e0e] | 105 |
|
---|
| 106 | buffer = malloc(buffer_size);
|
---|
| 107 | if (buffer == NULL) {
|
---|
| 108 | printf("Error allocating audio buffer.\n");
|
---|
| 109 | return 1;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | hound = hound_context_create_playback(argv[1], format, buffer_size);
|
---|
| 113 | if (hound == NULL) {
|
---|
| 114 | printf("Error creating playback context.\n");
|
---|
| 115 | return 1;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | rc = hound_context_connect_target(hound, HOUND_DEFAULT_TARGET);
|
---|
| 119 | if (rc != EOK) {
|
---|
[c1694b6b] | 120 | printf("Error connecting default audio target: %s.\n", str_error(rc));
|
---|
[7e69e0e] | 121 | return 1;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | rc = trackmod_modplay_create(mod, format.sampling_rate, &modplay);
|
---|
| 125 | if (rc != EOK) {
|
---|
| 126 | printf("Error setting up playback.\n");
|
---|
| 127 | return 1;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | printf("Playing '%s'. Press Ctrl+Q to quit.\n", argv[1]);
|
---|
| 131 |
|
---|
| 132 | while (true) {
|
---|
| 133 | timeout = 0;
|
---|
| 134 | if (console_get_event_timeout(con, &event, &timeout))
|
---|
| 135 | modplay_event(&event);
|
---|
| 136 | if (quit)
|
---|
| 137 | break;
|
---|
| 138 |
|
---|
[6609050] | 139 | trackmod_modplay_get_samples(modplay, buffer, buffer_size / 4);
|
---|
[7e69e0e] | 140 |
|
---|
[6609050] | 141 | rc = hound_write_main_stream(hound, buffer, buffer_size / 4);
|
---|
[7e69e0e] | 142 | if (rc != EOK) {
|
---|
| 143 | printf("Error writing audio stream.\n");
|
---|
| 144 | break;
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | hound_context_destroy(hound);
|
---|
| 149 | trackmod_modplay_destroy(modplay);
|
---|
| 150 | trackmod_module_destroy(mod);
|
---|
| 151 |
|
---|
| 152 | return 0;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 |
|
---|
| 156 | /** @}
|
---|
| 157 | */
|
---|