[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[tyndur-devel] [PATCH] Player: Ein einfaches Musikabspielprogramm



From: Max Reitz <max@xxxxxxxxxx>

+ Ein Musikabspielprogramm für .wav und .ogg (bisher). Für
  Ogg Vorbis werden allerdings noch libogg und libvorbis
  (bzw. vorbisfile) benötigt (siehe lbuilds), also folgende Libs
  nach src/modules/c/player kopieren: libogg.a, libvorbis.a,
  libvorbisfile.a, evtl. libvorbisenc.a
  Außerdem die entsprechenden Header (Verzeichnisse "ogg" und
  "vorbis") nach src/modules/include kopieren.

Signed-off-by: Max Reitz <max@xxxxxxxxxx>
---
 src/modules/c/player/.nobuild     |    1 +
 src/modules/c/player/Makefile.all |    9 +
 src/modules/c/player/main.c       |  228 ++++++++++++++++++++++++++
 src/modules/c/player/ogg-vorbis.c |  124 ++++++++++++++
 src/modules/c/player/ogg-vorbis.h |   28 ++++
 src/modules/c/player/player.h     |   39 +++++
 src/modules/c/player/riff-wave.c  |  322 +++++++++++++++++++++++++++++++++++++
 src/modules/c/player/riff-wave.h  |   28 ++++
 8 files changed, 779 insertions(+), 0 deletions(-)
 create mode 100644 src/modules/c/player/.nobuild
 create mode 100644 src/modules/c/player/Makefile.all
 create mode 100644 src/modules/c/player/main.c
 create mode 100644 src/modules/c/player/ogg-vorbis.c
 create mode 100644 src/modules/c/player/ogg-vorbis.h
 create mode 100644 src/modules/c/player/player.h
 create mode 100644 src/modules/c/player/riff-wave.c
 create mode 100644 src/modules/c/player/riff-wave.h

diff --git a/src/modules/c/player/.nobuild b/src/modules/c/player/.nobuild
new file mode 100644
index 0000000..fbef933
--- /dev/null
+++ b/src/modules/c/player/.nobuild
@@ -0,0 +1 @@
+$(eval $(NOBUILD))
\ No newline at end of file
diff --git a/src/modules/c/player/Makefile.all b/src/modules/c/player/Makefile.all
new file mode 100644
index 0000000..fdd306d
--- /dev/null
+++ b/src/modules/c/player/Makefile.all
@@ -0,0 +1,9 @@
+shopt -s extglob
+source $LOST_BUILDMK_ROOT/config.sh
+
+echo "LD   $1/apps/player"
+
+$LOST_TOOLS_LD -oplayer -Ttext=0x40000000 *.o --start-group $2 *.a --end-group
+
+
+mv player $1/apps/
diff --git a/src/modules/c/player/main.c b/src/modules/c/player/main.c
new file mode 100644
index 0000000..5dfb295
--- /dev/null
+++ b/src/modules/c/player/main.c
@@ -0,0 +1,228 @@
+/******************************************************************************
+ * Copyright (c) 2009 Max Reitz                                               *
+ *                                                                            *
+ * Permission is hereby granted,  free of charge,  to any  person obtaining a *
+ * copy of this software and associated documentation files (the "Software"), *
+ * to deal in the Software without restriction,  including without limitation *
+ * the rights to use,  copy, modify, merge, publish,  distribute, sublicense, *
+ * and/or sell copies  of the  Software,  and to permit  persons to whom  the *
+ * Software is furnished to do so, subject to the following conditions:       *
+ *                                                                            *
+ * The above copyright notice and this permission notice shall be included in *
+ * all copies or substantial portions of the Software.                        *
+ *                                                                            *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
+ * IMPLIED, INCLUDING  BUT NOT  LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
+ * FITNESS FOR A PARTICULAR  PURPOSE AND  NONINFRINGEMENT.  IN NO EVENT SHALL *
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
+ * LIABILITY,  WHETHER IN AN ACTION OF CONTRACT,  TORT OR OTHERWISE,  ARISING *
+ * FROM,  OUT OF  OR IN CONNECTION  WITH THE  SOFTWARE  OR THE  USE OR  OTHER *
+ * DEALINGS IN THE SOFTWARE.                                                  *
+ *****************************************************************************/
+
+#include <ctype.h>
+#include <errno.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <readline/readline.h>
+#include <sys/stat.h>
+
+#include "player.h"
+
+#include "ogg-vorbis.h"
+#include "riff-wave.h"
+
+
+#define BUF_SIZE 1048576
+
+
+struct format {
+    struct format* next_format;
+
+    void* (*is_this_format)(FILE* fp, void** format_inf);
+    const char* name;
+};
+
+
+static struct format* formats = NULL;
+
+int add_format_support(void* (*is_this_format)(FILE* fp, void** format_inf),
+    const char* name)
+{
+    struct format* new_format;
+    struct format* format;
+
+    if (is_this_format == NULL) {
+        return 0;
+    }
+
+    new_format = calloc(sizeof(*new_format), 1);
+    new_format->is_this_format = is_this_format;
+    new_format->name = name;
+    new_format->next_format = NULL;
+
+    if (formats == NULL) {
+        formats = new_format;
+        return 1;
+    }
+
+    format = formats;
+    while (format->next_format != NULL) {
+        format = format->next_format;
+    }
+    format->next_format = new_format;
+
+    return 1;
+}
+
+struct format* get_file_format(FILE* fp, void** format_inf,
+    size_t (**transform_data)(FILE* fp, void* format_inf, size_t out_frames,
+    void* buffer))
+{
+    struct format* format;
+    size_t (*rval)(FILE*, void*, size_t, void*);
+
+    format = formats;
+    while (format) {
+        rval = format->is_this_format(fp, format_inf);
+        if (rval != NULL) {
+            *transform_data = rval;
+            return format;
+        }
+
+        format = format->next_format;
+    }
+
+    return NULL;
+}
+
+int main(void)
+{
+    FILE* ifp = NULL;
+    FILE* ofp = NULL;
+    FILE* tfp;
+    void* buf;
+    void* fmt;
+    char* line;
+    char* command;
+    char* par;
+    char ofname[64] = "ac97:/crd0";
+    struct format* file_fmt;
+    struct general_file_format* gen_fmt;
+    int i, now, pwas;
+    size_t ifsize = 0;
+    struct stat stt;
+    size_t (*get_music)(FILE*, void*, size_t, void*);
+
+    add_wave_support();
+    add_vorbis_support();
+
+    printf("Ausgabedatei: \"%s\"\n", ofname);
+    ofp = fopen(ofname, "r+");
+    if (ofp == NULL) {
+        fprintf(stderr, "Ausgabedatei konnte nicht geöffnet werden.\n");
+    }
+
+    putchar('\n');
+    for (;;)
+    {
+        line = readline("> ");
+        if (line == NULL) {
+            continue;
+        }
+
+        command = strtok(line, " ");
+        if (command == NULL) {
+            command = line;
+        }
+        while (isspace(*command) && *command) {
+            command++;
+        }
+        for (i = 0; command[i]; i++) {
+            command[i] = tolower(command[i]);
+        }
+
+        par = &command[++i];
+        while (isspace(*par) && *par) {
+            par++;
+        }
+
+        if (!*command) {
+            continue;
+        } else if (!strcmp(command, "open")) {
+            if ((tfp = fopen(par, "r")) == NULL) {
+                fprintf(stderr, "Die Datei konnte nicht geöffnet werden.\n");
+                continue;
+            }
+            if (ifp != NULL) {
+                fclose(ifp);
+            }
+            ifp = tfp;
+            fstat(fileno(ifp), &stt);
+            ifsize = stt.st_size;
+        } else if (!strcmp(command, "quit") || !strcmp(command, "exit")) {
+            break;
+        } else if (!strcmp(command, "play")) {
+            if ((ifp == NULL) || (ofp == NULL)) {
+                fprintf(stderr, "Es werden sowohl Datei als auch Ausgabegerät "
+                    "benötigt.\n");
+                continue;
+            }
+            fseek(ifp, 0, SEEK_SET);
+            clearerr(ifp);
+
+            file_fmt = get_file_format(ifp, &fmt, &get_music);
+            if (file_fmt == NULL) {
+                fprintf(stderr, "Unbekanntes Dateiformat.\n");
+                continue;
+            }
+            printf("Dateiformat \"%s\".\n", file_fmt->name);
+            buf = calloc(1, BUF_SIZE);
+            gen_fmt = fmt;
+            printf("0:00 von %i:%02i\r", (int)(gen_fmt->length / 60),
+                gen_fmt->length % 60);
+            do {
+                pwas = gen_fmt->position;
+                fflush(stdout);
+                now = get_music(ifp, fmt, BUF_SIZE >> 2, buf);
+                fwrite(buf, 4, now, ofp);
+                printf("%i:%02i von %i:%02i\r", (int)(pwas / 60), pwas % 60,
+                    (int)(gen_fmt->length / 60), gen_fmt->length % 60);
+            } while (now >= (BUF_SIZE >> 2));
+            printf("\n");
+            free(buf);
+            free(fmt);
+        } else if (!strcmp(command, "help")) {
+            printf("Musikplayer (ohne Namen) für tyndur\n\n");
+            printf("Verfügbare Befehle:\n");
+            printf(" - help: Zeigt diese Liste an\n");
+            printf(" - version: Zeigt Versionsinformationen an\n");
+            printf(" - open <Datei>: Öffnet die Datei\n");
+            printf(" - play: Spielt eine geöffnete Datei ab\n");
+            printf(" - quit/exit: Beendet das Programm\n");
+        } else if (!strcmp(command, "version")) {
+            printf("Musikplayer (ohne Namen) für tyndur\n\n");
+            printf("Von Max Reitz\n\n");
+            printf("Unterstützte Formate:\n");
+            file_fmt = formats;
+            while (file_fmt != NULL) {
+                printf(" - \"%s\"\n", file_fmt->name);
+                file_fmt = file_fmt->next_format;
+            }
+        } else {
+            fprintf(stderr, "Unbekannter Befehl.\n");
+        }
+    }
+
+    if (ofp != NULL) {
+        fclose(ofp);
+    }
+    if (ifp != NULL) {
+        fclose(ifp);
+    }
+
+    return EXIT_SUCCESS;
+}
diff --git a/src/modules/c/player/ogg-vorbis.c b/src/modules/c/player/ogg-vorbis.c
new file mode 100644
index 0000000..7513167
--- /dev/null
+++ b/src/modules/c/player/ogg-vorbis.c
@@ -0,0 +1,124 @@
+/******************************************************************************
+ * Copyright (c) 2009 Max Reitz                                               *
+ *                                                                            *
+ * Permission is hereby granted,  free of charge,  to any  person obtaining a *
+ * copy of this software and associated documentation files (the "Software"), *
+ * to deal in the Software without restriction,  including without limitation *
+ * the rights to use,  copy, modify, merge, publish,  distribute, sublicense, *
+ * and/or sell copies  of the  Software,  and to permit  persons to whom  the *
+ * Software is furnished to do so, subject to the following conditions:       *
+ *                                                                            *
+ * The above copyright notice and this permission notice shall be included in *
+ * all copies or substantial portions of the Software.                        *
+ *                                                                            *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
+ * IMPLIED, INCLUDING  BUT NOT  LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
+ * FITNESS FOR A PARTICULAR  PURPOSE AND  NONINFRINGEMENT.  IN NO EVENT SHALL *
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
+ * LIABILITY,  WHETHER IN AN ACTION OF CONTRACT,  TORT OR OTHERWISE,  ARISING *
+ * FROM,  OUT OF  OR IN CONNECTION  WITH THE  SOFTWARE  OR THE  USE OR  OTHER *
+ * DEALINGS IN THE SOFTWARE.                                                  *
+ *****************************************************************************/
+
+#include <stdlib.h>
+#include <string.h>
+#include <vorbis/vorbisfile.h>
+
+#include "player.h"
+
+struct vorbis_format {
+    struct general_file_format gen;
+    OggVorbis_File vorbis_file;
+    int bitstream;
+    int channels;
+};
+
+static void* is_vorbis(FILE* fp, void** format_inf);
+static size_t vorbis2raw(FILE* fp, void* format_inf, size_t out_frames,
+    void* out_buf);
+
+void add_vorbis_support(void)
+{
+    add_format_support(&is_vorbis, "Ogg Vorbis");
+}
+
+static void* is_vorbis(FILE* fp, void** format_inf)
+{
+    long pos = ftell(fp);
+    OggVorbis_File vorbis_file;
+    struct vorbis_format* fmt;
+
+    if (ov_open(fp, &vorbis_file, NULL, 0) < 0) {
+        fseek(fp, pos, SEEK_SET);
+        return NULL;
+    }
+
+    *format_inf = calloc(sizeof(*fmt), 1);
+    fmt = *format_inf;
+    memcpy(&fmt->vorbis_file, &vorbis_file, sizeof(vorbis_file));
+    fmt->bitstream = 0;
+    fmt->channels = ov_info(&fmt->vorbis_file, -1)->channels;
+    fmt->gen.position = 0;
+    fmt->gen.length = ov_time_total(&fmt->vorbis_file, -1);
+
+    return &vorbis2raw;
+}
+
+static size_t vorbis2raw(FILE* fp, void* format_inf, size_t out_frames,
+    void* out_buf)
+{
+    struct vorbis_format* fmt = format_inf;
+    int sum = 0, now = 1024;
+    void* tbuf;
+    void* out;
+#ifndef USE_INLINE_ASM
+    uint16_t *ibuf;
+    uint16_t *obuf;
+    int swas;
+#endif
+
+    //TODO Resamplen
+
+    if (fmt->channels == 1) {
+        tbuf = calloc(2, out_frames);
+        out = tbuf;
+
+        while (out_frames && now) {
+            now = ov_read(&fmt->vorbis_file, out, out_frames * 2, 0, 2, 1,
+                &fmt->bitstream) >> 1;
+            sum += now;
+            out += now * 2;
+            out_frames -= now;
+        }
+
+#ifdef USE_INLINE_ASM
+        __asm__ __volatile__ (".blow_up_to_two:"
+                "lodsw;"
+                "stosw;"
+                "stosw;"
+                "loop .blow_up_to_two"::"c"(sum),"S"(tbuf),"D"(out_buf));
+#else
+        ibuf = tbuf;
+        obuf = out_buf;
+        swas = sum;
+        while (swas--) {
+            *(obuf++) = *ibuf;
+            *(obuf++) = *(ibuf++);
+        }
+#endif
+
+        free(tbuf);
+    } else {
+        while (out_frames && now) {
+            now = ov_read(&fmt->vorbis_file, out_buf, out_frames * 4, 0, 2, 1,
+                &fmt->bitstream) >> 2;
+            sum += now;
+            out_buf += now * 4;
+            out_frames -= now;
+        }
+    }
+
+    fmt->gen.position = (volatile int)ov_time_tell(&fmt->vorbis_file);
+
+    return sum;
+}
diff --git a/src/modules/c/player/ogg-vorbis.h b/src/modules/c/player/ogg-vorbis.h
new file mode 100644
index 0000000..3b509fe
--- /dev/null
+++ b/src/modules/c/player/ogg-vorbis.h
@@ -0,0 +1,28 @@
+/******************************************************************************
+ * Copyright (c) 2009 Max Reitz                                               *
+ *                                                                            *
+ * Permission is hereby granted,  free of charge,  to any  person obtaining a *
+ * copy of this software and associated documentation files (the "Software"), *
+ * to deal in the Software without restriction,  including without limitation *
+ * the rights to use,  copy, modify, merge, publish,  distribute, sublicense, *
+ * and/or sell copies  of the  Software,  and to permit  persons to whom  the *
+ * Software is furnished to do so, subject to the following conditions:       *
+ *                                                                            *
+ * The above copyright notice and this permission notice shall be included in *
+ * all copies or substantial portions of the Software.                        *
+ *                                                                            *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
+ * IMPLIED, INCLUDING  BUT NOT  LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
+ * FITNESS FOR A PARTICULAR  PURPOSE AND  NONINFRINGEMENT.  IN NO EVENT SHALL *
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
+ * LIABILITY,  WHETHER IN AN ACTION OF CONTRACT,  TORT OR OTHERWISE,  ARISING *
+ * FROM,  OUT OF  OR IN CONNECTION  WITH THE  SOFTWARE  OR THE  USE OR  OTHER *
+ * DEALINGS IN THE SOFTWARE.                                                  *
+ *****************************************************************************/
+
+#ifndef OGG_VORBIS_H
+#define OGG_VORBIS_H
+
+void add_vorbis_support(void);
+
+#endif
diff --git a/src/modules/c/player/player.h b/src/modules/c/player/player.h
new file mode 100644
index 0000000..74a0d87
--- /dev/null
+++ b/src/modules/c/player/player.h
@@ -0,0 +1,39 @@
+/******************************************************************************
+ * Copyright (c) 2009 Max Reitz                                               *
+ *                                                                            *
+ * Permission is hereby granted,  free of charge,  to any  person obtaining a *
+ * copy of this software and associated documentation files (the "Software"), *
+ * to deal in the Software without restriction,  including without limitation *
+ * the rights to use,  copy, modify, merge, publish,  distribute, sublicense, *
+ * and/or sell copies  of the  Software,  and to permit  persons to whom  the *
+ * Software is furnished to do so, subject to the following conditions:       *
+ *                                                                            *
+ * The above copyright notice and this permission notice shall be included in *
+ * all copies or substantial portions of the Software.                        *
+ *                                                                            *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
+ * IMPLIED, INCLUDING  BUT NOT  LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
+ * FITNESS FOR A PARTICULAR  PURPOSE AND  NONINFRINGEMENT.  IN NO EVENT SHALL *
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
+ * LIABILITY,  WHETHER IN AN ACTION OF CONTRACT,  TORT OR OTHERWISE,  ARISING *
+ * FROM,  OUT OF  OR IN CONNECTION  WITH THE  SOFTWARE  OR THE  USE OR  OTHER *
+ * DEALINGS IN THE SOFTWARE.                                                  *
+ *****************************************************************************/
+
+#ifndef PLAYER_H
+#define PLAYER_H
+
+//Das definieren, um x86-Inline-Assembler für ein paar Sachen zu benutzen
+#define USE_INLINE_ASM
+
+
+struct general_file_format {
+    int length;
+    volatile int position;
+};
+
+
+int add_format_support(void* (*is_this_format)(FILE* fp, void** format_inf),
+    const char* name);
+
+#endif
diff --git a/src/modules/c/player/riff-wave.c b/src/modules/c/player/riff-wave.c
new file mode 100644
index 0000000..a3a206a
--- /dev/null
+++ b/src/modules/c/player/riff-wave.c
@@ -0,0 +1,322 @@
+/******************************************************************************
+ * Copyright (c) 2009 Max Reitz                                               *
+ *                                                                            *
+ * Permission is hereby granted,  free of charge,  to any  person obtaining a *
+ * copy of this software and associated documentation files (the "Software"), *
+ * to deal in the Software without restriction,  including without limitation *
+ * the rights to use,  copy, modify, merge, publish,  distribute, sublicense, *
+ * and/or sell copies  of the  Software,  and to permit  persons to whom  the *
+ * Software is furnished to do so, subject to the following conditions:       *
+ *                                                                            *
+ * The above copyright notice and this permission notice shall be included in *
+ * all copies or substantial portions of the Software.                        *
+ *                                                                            *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
+ * IMPLIED, INCLUDING  BUT NOT  LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
+ * FITNESS FOR A PARTICULAR  PURPOSE AND  NONINFRINGEMENT.  IN NO EVENT SHALL *
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
+ * LIABILITY,  WHETHER IN AN ACTION OF CONTRACT,  TORT OR OTHERWISE,  ARISING *
+ * FROM,  OUT OF  OR IN CONNECTION  WITH THE  SOFTWARE  OR THE  USE OR  OTHER *
+ * DEALINGS IN THE SOFTWARE.                                                  *
+ *****************************************************************************/
+
+#include <ctype.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "player.h"
+
+
+struct wave_header {
+    char fmt_chunk[4];
+    uint32_t fmt_chunk_len;
+    uint16_t tag;
+    uint16_t channels;
+    uint32_t sample_rate;
+    uint32_t bytes_per_second;
+    uint16_t block_align;
+    uint16_t bits_per_sample;
+} __attribute__ ((packed));
+
+struct wave_format {
+    struct general_file_format gen;
+
+    int channels;
+    int sample_rate;
+    int bits_per_sample;
+    size_t size;
+    int pos;
+    int bytes_per_second;
+};
+
+
+static void* is_wave(FILE* fp, void** format_inf);
+static size_t wave2raw(FILE* fp, void* format_inf, size_t out_frames, void* out_buf);
+
+
+void add_wave_support(void)
+{
+    add_format_support(&is_wave, "RIFF Wave");
+}
+
+static void* is_wave(FILE* fp, void** format_inf)
+{
+    char chunk_name[4];
+    struct wave_header wav_fmt;
+    int i;
+    long pos;
+    size_t ifsize;
+    struct wave_format* fmt;
+
+    pos = ftell(fp);
+
+    fread(chunk_name, 1, 4, fp);
+    if (!strncmp(chunk_name, "RIFF", 4)) {
+        fseek(fp, 4, SEEK_CUR);
+        fread(chunk_name, 1, 4, fp);
+        if (!strncmp(chunk_name, "WAVE", 4)) {
+            fread(&wav_fmt, 1, sizeof(wav_fmt), fp);
+            for (i = 0; i < 4; i++) {
+                wav_fmt.fmt_chunk[i] = toupper(wav_fmt.fmt_chunk[i]);
+            }
+            if (strncmp(wav_fmt.fmt_chunk, "FMT ", 4) ||
+                (wav_fmt.fmt_chunk_len != 16))
+            {
+                fseek(fp, pos, SEEK_SET);
+                return NULL;
+            }
+
+            if ((wav_fmt.tag != 1) || ((wav_fmt.channels != 2) &&
+                (wav_fmt.channels != 1)) || (wav_fmt.sample_rate != 44100) ||
+                ((wav_fmt.bits_per_sample != 16) &&
+                (wav_fmt.bits_per_sample != 8) &&
+                (wav_fmt.bits_per_sample != 24) &&
+                (wav_fmt.bits_per_sample != 32)))
+            {
+                fseek(fp, pos, SEEK_SET);
+                return NULL;
+            }
+
+            do {
+                fread(chunk_name, 1, 4, fp);
+                fread(&ifsize, 1, 4, fp);
+                if (strncmp(chunk_name, "data", 4)) {
+                    fseek(fp, ifsize, SEEK_CUR);
+                }
+            } while (strncmp(chunk_name, "data", 4) && !feof(fp));
+
+            if (feof(fp)) {
+                fseek(fp, pos, SEEK_SET);
+                return NULL;
+            }
+        } else {
+            fseek(fp, pos, SEEK_SET);
+            return NULL;
+        }
+    } else {
+        fseek(fp, pos, SEEK_SET);
+        return NULL;
+    }
+
+    *format_inf = calloc(sizeof(*fmt), 1);
+    fmt = *format_inf;
+    fmt->channels = wav_fmt.channels;
+    fmt->sample_rate = wav_fmt.sample_rate;
+    fmt->bits_per_sample = wav_fmt.bits_per_sample;
+    fmt->size = ifsize;
+    fmt->pos = 0;
+    fmt->bytes_per_second = wav_fmt.bytes_per_second;
+    fmt->gen.length = ifsize / wav_fmt.bytes_per_second;
+    fmt->gen.position = 0;
+
+    return &wave2raw;
+}
+
+static size_t wave2raw(FILE* fp, void* format_inf, size_t out_frames, void* out_buf)
+{
+    struct wave_format* fmt = format_inf;
+    size_t now;
+    void* tbuf;
+#ifndef USE_INLINE_ASM
+    size_t count;
+    uint8_t* i8buf;
+    uint16_t* ibuf;
+    uint32_t* i32buf;
+    uint16_t* obuf;
+#endif
+
+    //TODO Resamplen
+
+    now = fmt->size / (fmt->channels * (fmt->bits_per_sample >> 3));
+    if (now > out_frames) {
+        now = out_frames;
+    }
+
+    if ((fmt->channels == 2) && (fmt->bits_per_sample == 16)) {
+        fread(out_buf, 4, now, fp);
+        fmt->size -= now * 4;
+        fmt->pos += now * 4;
+    } else if ((fmt->channels == 1) && (fmt->bits_per_sample == 16)) {
+        tbuf = calloc(now, 2);
+        fread(tbuf, 2, now, fp);
+        fmt->size -= now * 2;
+        fmt->pos += now * 2;
+
+#ifdef USE_INLINE_ASM
+        __asm__ __volatile__ (".blow_up_to_two:"
+                "lodsw;"
+                "stosw;"
+                "stosw;"
+                "loop .blow_up_to_two"::"c"(now),"S"(tbuf),"D"(out_buf));
+#else
+        count = now;
+        ibuf = tbuf;
+        obuf = out_buf;
+        while (count--) {
+            *(obuf++) = *ibuf;
+            *(obuf++) = *(ibuf++);
+        }
+#endif
+
+        free(tbuf);
+    } else if (fmt->channels == 2) {
+        tbuf = calloc(now, fmt->bits_per_sample >> 2);
+        fread(tbuf, fmt->bits_per_sample >> 2, now, fp);
+        fmt->size -= now * (fmt->bits_per_sample >> 2);
+        fmt->pos += now * (fmt->bits_per_sample >> 2);
+        switch (fmt->bits_per_sample)
+        {
+            case 8:
+#ifdef USE_INLINE_ASM
+                __asm__ __volatile__ (".from_2_8:"
+                        "xor %%eax,%%eax;"
+                        "lodsb;"
+                        "shl $7,%%eax;" //Bitte *nicht* fragen, warum nicht $8
+                        "stosw;"
+                        "loop .from_2_8"::"c"(now * 2),"S"(tbuf),"D"(out_buf));
+#else
+                count = now * 2;
+                i8buf = tbuf;
+                obuf = out_buf;
+                while (count--) {
+                    *(obuf++) = *(i8buf++) << 7;
+                }
+#endif
+                break;
+            case 24:
+#ifdef USE_INLINE_ASM
+                __asm__ __volatile__ (".from_2_24:"
+                        "lodsb;"
+                        "lodsw;"
+                        "stosw;"
+                        "loop .from_2_24"
+                        ::"c"(now * 2),"S"(tbuf),"D"(out_buf));
+#else
+                count = now * 2;
+                i8buf = tbuf;
+                obuf = out_buf;
+                i8buf -= 2;
+                while (count--) {
+                    i8buf += 3;
+                    *(obuf++) = i8buf[0] | (i8buf[1] << 8);
+                }
+#endif
+                break;
+            case 32:
+                //Gibts eigentlich gar nicht. Aber warum nicht? Dann ist das
+                //eben der einzige Player mit RIFF-WAVE-PCM-32-Bit-Unter-
+                //stützung. Wow.
+#ifdef USE_INLINE_ASM
+                __asm__ __volatile__ (".from_2_32:"
+                        "lodsl;"
+                        "shr $16,%%eax;"
+                        "stosw;"
+                        "loop .from_2_32"
+                        ::"c"(now * 2),"S"(tbuf),"D"(out_buf));
+#else
+                count = now * 2;
+                i32buf = tbuf;
+                obuf = out_buf;
+                while (count--) {
+                    *(obuf++) = *(i32buf++) >> 16;
+                }
+#endif
+                break;
+        }
+        free(tbuf);
+    }
+    else if (fmt->channels == 1)
+    {
+        tbuf = calloc(now, fmt->bits_per_sample >> 3);
+        fread(tbuf, fmt->bits_per_sample >> 3, now, fp);
+        fmt->size -= now * (fmt->bits_per_sample >> 3);
+        fmt->pos += now * (fmt->bits_per_sample >> 3);
+        switch (fmt->bits_per_sample)
+        {
+            case 8:
+#ifdef USE_INLINE_ASM
+                __asm__ __volatile__ (".from_1_8:"
+                        "xor %%eax,%%eax;"
+                        "lodsb;"
+                        "shl $7,%%eax;" //Bitte *nicht* fragen, warum nicht $8
+                        "stosw;"
+                        "stosw;"
+                        "loop .from_1_8"::"c"(now),"S"(tbuf),"D"(out_buf));
+#else
+                count = now;
+                i8buf = tbuf;
+                obuf = out_buf;
+                while (count--) {
+                    *(obuf++) = *(i8buf) << 7;
+                    *(obuf++) = *(i8buf++) << 7;
+                }
+#endif
+                break;
+            case 24:
+#ifdef USE_INLINE_ASM
+                __asm__ __volatile__ (".from_1_24:"
+                        "lodsb;"
+                        "lodsw;"
+                        "stosw;"
+                        "stosw;"
+                        "loop .from_1_24"::"c"(now),"S"(tbuf),"D"(out_buf));
+#else
+                count = now;
+                i8buf = tbuf;
+                obuf = out_buf;
+                i8buf -= 2;
+                while (count--) {
+                    i8buf += 3;
+                    *(obuf++) = i8buf[0] | (i8buf[1] << 8);
+                    *(obuf++) = i8buf[0] | (i8buf[1] << 8);
+                }
+#endif
+                break;
+            case 32:
+#ifdef USE_INLINE_ASM
+                __asm__ __volatile__ (".from_1_32:"
+                        "lodsl;"
+                        "shr $16,%%eax;"
+                        "stosw;"
+                        "stosw;"
+                        "loop .from_1_32"::"c"(now),"S"(tbuf),"D"(out_buf));
+#else
+                count = now;
+                i32buf = tbuf;
+                obuf = out_buf;
+                while (count--) {
+                    *(obuf++) = *i32buf >> 16;
+                    *(obuf++) = *(i32buf++) >> 16;
+                }
+#endif
+                break;
+        }
+        free(tbuf);
+    }
+
+    fmt->gen.position = fmt->pos / fmt->bytes_per_second;
+
+    return now;
+}
diff --git a/src/modules/c/player/riff-wave.h b/src/modules/c/player/riff-wave.h
new file mode 100644
index 0000000..9eff4b3
--- /dev/null
+++ b/src/modules/c/player/riff-wave.h
@@ -0,0 +1,28 @@
+/******************************************************************************
+ * Copyright (c) 2009 Max Reitz                                               *
+ *                                                                            *
+ * Permission is hereby granted,  free of charge,  to any  person obtaining a *
+ * copy of this software and associated documentation files (the "Software"), *
+ * to deal in the Software without restriction,  including without limitation *
+ * the rights to use,  copy, modify, merge, publish,  distribute, sublicense, *
+ * and/or sell copies  of the  Software,  and to permit  persons to whom  the *
+ * Software is furnished to do so, subject to the following conditions:       *
+ *                                                                            *
+ * The above copyright notice and this permission notice shall be included in *
+ * all copies or substantial portions of the Software.                        *
+ *                                                                            *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
+ * IMPLIED, INCLUDING  BUT NOT  LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
+ * FITNESS FOR A PARTICULAR  PURPOSE AND  NONINFRINGEMENT.  IN NO EVENT SHALL *
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
+ * LIABILITY,  WHETHER IN AN ACTION OF CONTRACT,  TORT OR OTHERWISE,  ARISING *
+ * FROM,  OUT OF  OR IN CONNECTION  WITH THE  SOFTWARE  OR THE  USE OR  OTHER *
+ * DEALINGS IN THE SOFTWARE.                                                  *
+ *****************************************************************************/
+
+#ifndef RIFF_WAVE_H
+#define RIFF_WAVE_H
+
+void add_wave_support(void);
+
+#endif
-- 
1.6.3.3