/ MCUME_esp32 / espsnd / main / sndplay.cpp
sndplay.cpp
  1  extern "C" {
  2  #include "emuapi.h"
  3  }
  4  
  5  
  6  #include "esp_system.h"
  7  #include "esp_event.h"
  8  #include "esp_event_loop.h"
  9  #include <string.h>
 10  
 11  #include "reSID/reSID.h"
 12  #include "LibFC14/fc14audiodecoder.h"
 13  #include "StSnd/StSoundLibrary.h"
 14  
 15  
 16  enum MusType {
 17    Undefined,
 18    C64Dmp,
 19    StYm,
 20    AmFc
 21  };  
 22  
 23  static MusType mustype = Undefined; 
 24  static bool playing = false;
 25  static int mussize=0;
 26  static void * musbuffer=nullptr;
 27  
 28  char buffer[26];
 29  char oldbuffer[26];
 30  
 31  static AudioPlaySID playSID;
 32  static void *fcDecoder = nullptr;  
 33  static YMMUSIC *ymDecoder;
 34  
 35  
 36  void snd_Init(void)
 37  {
 38    switch (mustype) {
 39      case C64Dmp:
 40        playing = true;
 41        break;
 42      case AmFc:
 43        if (musbuffer) {
 44          bool haveModule = false;  
 45          fcDecoder = fc14dec_new();
 46          haveModule = fc14dec_init(fcDecoder,(void*)musbuffer,mussize);
 47          if ( !haveModule ) {
 48            fc14dec_delete(fcDecoder);
 49            printf("FC module not supported\n");
 50          }
 51          else {
 52            printf("FC music loaded\n");
 53            fc14dec_mixer_init(fcDecoder,22050,16,1,0 /*32767*/);
 54            playing = true;
 55          }
 56        }
 57        break;
 58      case StYm:
 59        if (musbuffer) {
 60          ymDecoder = ymMusicCreate();
 61          printf("YM music loader\n");    
 62          if (ymMusicLoadMemory(ymDecoder,(void*)musbuffer,mussize))
 63          { 
 64            printf("YM music loaded\n");
 65            ymMusicInfo_t info;
 66            ymMusicGetInfo(ymDecoder,&info);
 67            printf("Name.....: %s\n",info.pSongName);
 68            printf("Author...: %s\n",info.pSongAuthor);
 69            printf("Comment..: %s\n",info.pSongComment);
 70            printf("Duration.: %d:%02d\n",info.musicTimeInSec/60,info.musicTimeInSec%60);
 71            printf("Driver...: %s\n", info.pSongPlayer);
 72            ymMusicSetLoopMode(ymDecoder,YMTRUE); 
 73            ymMusicPlay(ymDecoder);         
 74            playing = true;
 75          } 
 76        }
 77        break;
 78      default:
 79        break;
 80    }  
 81  }
 82  
 83  int oldSec = -1;
 84  void snd_Step(void)
 85  {
 86    vTaskDelay(20 / portTICK_PERIOD_MS);
 87    switch (mustype) {
 88      case C64Dmp:
 89        if (emu_FileRead(buffer, 25) == 25 ) {
 90          for(int i=0;i<25;i++) {
 91            if(buffer[i] != oldbuffer[i]) {       
 92               playSID.setreg(i, buffer[i]);
 93              oldbuffer[i] = buffer[i];                  
 94            } 
 95          }
 96        } else {
 97          if (playSID.isPlaying()) {
 98            emu_FileClose();
 99            playSID.stop();
100          } 
101        }
102        break;
103      case AmFc:
104        break;
105      case StYm:
106        //int sec = ymMusicGetPos(ymDecoder) / 1000;
107        //if (sec != oldSec)
108       // {
109       //   printf("Time: %d:%02d\r",sec/60,sec%60);
110       //   oldSec = sec;
111       // }  
112        break;
113      default:
114        break;
115    }    
116  }
117  
118  void snd_Start(char * filename)
119  {
120    mussize = emu_FileSize(filename);
121    char *dot = strrchr(filename, '.');
122    if (dot && !strcmp(dot, ".dmp")) {
123      mustype = C64Dmp; 
124      if (emu_FileOpen(filename) == 0) {
125        printf("SID music failure, cannot open\n");        
126      }   
127    }
128    else if (dot && !strcmp(dot, ".fc")) {
129      mustype = AmFc; 
130      musbuffer = emu_Malloc(mussize);
131      if (musbuffer)
132      {
133        if (emu_FileOpen(filename)) {
134          if (emu_FileRead((char*)musbuffer, mussize) != mussize ) {
135            musbuffer = nullptr;
136          }
137          emu_FileClose();
138        } else {
139          musbuffer = nullptr;
140        }  
141      }
142      if (musbuffer == nullptr) {
143        printf("FC music failure, cannot open/allocate\n");
144      }           
145    }
146    else if (dot && !strcmp(dot, ".ym")) {
147      mustype = StYm; 
148    }
149  }
150  
151  void  SND_Process( void * stream, int len )
152  {
153    if (playing) {
154      switch (mustype) {
155        case C64Dmp:
156          playSID.update(stream, len);
157          break;
158        case AmFc:
159          fc14dec_buffer_fill(fcDecoder,stream,len*2);  
160          break;
161        case StYm:
162          ymMusicCompute((void*)ymDecoder,(ymsample*)stream,len);
163         break;
164        default:
165          break;
166      }  
167    }
168  }
169  
170