Ticket #7957: timidity.diff

File timidity.diff, 5.1 KB (added by SF/urkle, 22 years ago)

Timidity MidiDriver support

  • gameDetector.cpp

     
    568568#if defined(UNIX) && !defined(__BEOS__)
    569569    case MD_SEQ:        return MidiDriver_SEQ_create();
    570570#endif
     571#if defined(TIMIDITY)
     572                case MD_TIMIDITY:               return MidiDriver_TIMIDITY_create();
     573#endif
    571574#if defined(__APPLE__)
    572575        case MD_QTMUSIC:                return MidiDriver_QT_create();
    573576        case MD_COREAUDIO:              return MidiDriver_CORE_create();
  • sound/mididrv.cpp

     
    2929#include "stdafx.h"
    3030#include "scumm.h"
    3131#include "mididrv.h"
     32#if defined(TIMIDITY)
     33#include <sys/socket.h>
     34#include <netdb.h>
     35#endif
    3236
    3337#if defined(WIN32) && !defined(_WIN32_WCE)
    3438
     
    786790
    787791
    788792
    789 #if 0
    790793
    791 /* Old code for timidity support, maybe somebody can rewrite this for the
    792    new midi driver system?
    793  */
     794#if defined(TIMIDITY)
     795/* TIMIDITY driver */
     796class MidiDriver_TIMIDITY : public MidiDriver {
     797public:
     798        MidiDriver_TIMIDITY();
     799        int open(int mode);
     800        int open_socket(int *socketptr, int port);
     801        void close();
     802        void send(uint32 b);
     803        void pause(bool pause);
     804        void set_stream_callback(void *param, StreamCallback *sc);
     805private:
     806        StreamCallback *_stream_proc;
     807        void *_stream_param;
     808        int _mode;
     809        int device;
     810};
     811
     812MidiDriver_TIMIDITY::MidiDriver_TIMIDITY() {
     813  _mode=0;
     814        device=0;
     815}
    794816
    795817
    796 /*********** Timidity           */
    797 int MidiDriver::connect_to_timidity(int port)
     818int MidiDriver_TIMIDITY::open(int mode)
     819{       
     820        int s,s2,err;
     821        int len;
     822        int dummy,newport;
     823        char buf[256];
     824
     825        if (_mode != 0 )
     826                return MERR_ALREADY_OPEN;
     827        device = 0;
     828        _mode = mode;
     829        if (mode!=MO_SIMPLE) return MERR_STREAMING_NOT_AVAILABLE;
     830
     831        if ((err = open_socket(&s,7777))!=0)
     832                return err;
     833
     834        len = ::read(s, buf, 256);
     835        sprintf(buf,"SETBUF %f %f\n",0.1,0.15);
     836        write(s, buf, strlen(buf));
     837
     838        len = ::read(s, buf, 256);
     839        sprintf(buf,"OPEN lsb\n");
     840        write(s, buf, strlen(buf));
     841
     842        len = ::read(s, buf, 256);
     843        sscanf(buf,"%d %d",&dummy, &newport);
     844        write(s, buf, strlen(buf));
     845
     846        if ((err = open_socket(&s2,newport))!=0)
     847                return err;
     848        device = s2;
     849        return 0;
     850}
     851
     852int MidiDriver_TIMIDITY::open_socket(int *socketptr, int port)
    798853{
    799854        int s = 0;
    800855#if !defined(__APPLE__CW) && !defined(__MORPHOS__)      // No socket support on Apple Carbon or Morphos
     
    803858
    804859        serverhost = gethostbyname("localhost");
    805860        if (serverhost == NULL)
    806                 error("Could not resolve Timidity host ('localhost')");
     861                return MERR_CANNOT_CONNECT;
     862                //warning("Could not resolve Timidity host ('localhost')");
    807863
    808864        sadd.sin_family = serverhost->h_addrtype;
    809865        sadd.sin_port = htons(port);
     
    811867
    812868        s = socket(AF_INET, SOCK_STREAM, 0);
    813869        if (s < 0)
    814                 error("Could not open Timidity socket");
     870                return MERR_CANNOT_CONNECT;
     871                //error("Could not open Timidity socket");
    815872
    816873        if (connect(s, (struct sockaddr *)&sadd, sizeof(struct sockaddr_in)) < 0)
    817                 error("Could not connect to Timidity server");
     874                return MERR_CANNOT_CONNECT;
     875                //error("Could not connect to Timidity server");
     876        *socketptr = s;
     877        return 0;
    818878#endif
    819         return s;
     879        return MERR_DEVICE_NOT_AVAILABLE;
    820880}
    821881
    822 void MidiDriver::midiInitTimidity()
     882void MidiDriver_TIMIDITY::close()
    823883{
    824         int s, s2;
    825         int len;
    826         int dummy, newport;
    827         char buf[256];
     884        ::close(device);
     885        _mode = 0;
     886}
    828887
    829         s = connect_to_timidity(7777);
    830         len = read(s, buf, 256);                        // buf[len] = '\0'; printf("%s", buf);
    831         sprintf(buf, "SETBUF %f %f\n", 0.1, 0.15);
    832         write(s, buf, strlen(buf));
    833         len = read(s, buf, 256);                        // buf[len] = '\0'; printf("%s", buf);
     888void MidiDriver_TIMIDITY::send(uint32 b)
     889{
     890        unsigned char buf[256];
     891        int position = 0;
    834892
    835         sprintf(buf, "OPEN lsb\n");
    836         write(s, buf, strlen(buf));
    837         len = read(s, buf, 256);                        // buf[len] = '\0'; printf("%s", buf);
     893        switch (b & 0xF0) {
     894        case 0x80:
     895        case 0x90:
     896        case 0xA0:
     897        case 0xB0:
     898        case 0xE0:
     899                buf[position++] = SEQ_MIDIPUTC;
     900                buf[position++] = (unsigned char)b;
     901                buf[position++] = DEVICE_NUM;
     902                buf[position++] = 0;
     903                buf[position++] = SEQ_MIDIPUTC;
     904                buf[position++] = (unsigned char)((b >> 8) & 0x7F);
     905                buf[position++] = DEVICE_NUM;
     906                buf[position++] = 0;
     907                buf[position++] = SEQ_MIDIPUTC;
     908                buf[position++] = (unsigned char)((b >> 16) & 0x7F);
     909                buf[position++] = DEVICE_NUM;
     910                buf[position++] = 0;
     911                break;
     912        case 0xC0:
     913        case 0xD0:
     914                buf[position++] = SEQ_MIDIPUTC;
     915                buf[position++] = (unsigned char)b;
     916                buf[position++] = DEVICE_NUM;
     917                buf[position++] = 0;
     918                buf[position++] = SEQ_MIDIPUTC;
     919                buf[position++] = (unsigned char)((b >> 8) & 0x7F);
     920                buf[position++] = DEVICE_NUM;
     921                buf[position++] = 0;
     922                break;
     923        default:
     924                fprintf(stderr, "Unknown : %08x\n", (int) b);
     925                break;
     926        }
     927        write(device, buf, position);
     928}
    838929
    839         sscanf(buf, "%d %d", &dummy, &newport);
    840         printf("         => port = %d\n", newport);
     930void MidiDriver_TIMIDITY::pause(bool pause)
     931{
     932        debug(1,"pause: %d",pause);
     933        if (_mode == MO_STREAMING) {
     934        }
     935}
    841936
    842         s2 = connect_to_timidity(newport);
    843         _mo = (void *)s2;
     937void MidiDriver_TIMIDITY::set_stream_callback(void *param, StreamCallback *sc)
     938{
     939        _stream_param = param;
     940        _stream_proc = sc;
    844941}
    845942
     943MidiDriver *MidiDriver_TIMIDITY_create()
     944{
     945        return new MidiDriver_TIMIDITY();
     946}
    846947
    847 #endif /* 0 */
     948#endif /* TIMIDITY */