|
| 1 | +#ifndef SOUNDPLAYER_H_H |
| 2 | +#define SOUNDPLAYER_H_H |
| 3 | +#include<string> |
| 4 | +#include<Windows.h> |
| 5 | +#include<time.h> |
| 6 | +typedef int(__stdcall* w32mci)(const char*, char*, int, int); |
| 7 | +typedef int(__stdcall * w32mcierror)(int, char*, int); |
| 8 | +class Mci |
| 9 | +{ |
| 10 | +private: |
| 11 | + HINSTANCE hins; |
| 12 | + w32mci wmci; |
| 13 | + w32mcierror wmcierror; |
| 14 | +public: |
| 15 | + Mci(); |
| 16 | + ~Mci(); |
| 17 | + char buf[256]; |
| 18 | + bool send(std::string command);//error return false |
| 19 | +}; |
| 20 | +class AudioClip |
| 21 | +{ |
| 22 | +private: |
| 23 | + Mci mci; |
| 24 | + std::string filename; |
| 25 | + std::string alias; |
| 26 | + int length_ms; |
| 27 | +public: |
| 28 | + AudioClip(); |
| 29 | + ~AudioClip(); |
| 30 | + bool load(const std::string& _filename); |
| 31 | + bool play(int start_ms=0, int end_ms=-1); |
| 32 | + bool stop(); |
| 33 | + bool pause(); |
| 34 | + bool unpause(); |
| 35 | + int milliseconds(); |
| 36 | +}; |
| 37 | +Mci::Mci() |
| 38 | +{ |
| 39 | + HINSTANCE hins = LoadLibraryA("winmm.dll"); |
| 40 | + wmci=(w32mci)GetProcAddress(hins, "mciSendStringA"); |
| 41 | + wmcierror = (w32mcierror)GetProcAddress(hins, "mciGetErrorStringA"); |
| 42 | +} |
| 43 | +Mci::~Mci() |
| 44 | +{ |
| 45 | + FreeLibrary(hins); |
| 46 | +} |
| 47 | +bool Mci::send(std::string command) |
| 48 | +{ |
| 49 | + int errcode = wmci(command.c_str(), buf, 254, 0); |
| 50 | + if (errcode) |
| 51 | + { |
| 52 | + wmcierror(errcode, buf, 254); |
| 53 | + return false; |
| 54 | + } |
| 55 | + return true; |
| 56 | +} |
| 57 | +AudioClip::AudioClip() |
| 58 | +{ |
| 59 | + //do nothing |
| 60 | +} |
| 61 | +AudioClip::~AudioClip() |
| 62 | +{ |
| 63 | + std::string cmd; |
| 64 | + cmd = "close " + alias; |
| 65 | + mci.send(cmd); |
| 66 | +} |
| 67 | +bool AudioClip::load(const std::string& _filename) |
| 68 | +{ |
| 69 | + filename = _filename; |
| 70 | + for (unsigned int i = 0; i < filename.length(); i++) |
| 71 | + { |
| 72 | + if (filename[i] == '/') |
| 73 | + filename[i] = '\\'; |
| 74 | + } |
| 75 | + alias = "mp3_"; |
| 76 | + srand(time(NULL)); |
| 77 | + char randstr[6]; |
| 78 | + _itoa(rand() % 65536, randstr, 10); |
| 79 | + alias.append(randstr); |
| 80 | + std::string cmd; |
| 81 | + cmd = "open " + filename + " alias " + alias; |
| 82 | + if (mci.send(cmd) == false) |
| 83 | + return false; |
| 84 | + cmd = "set " + alias + " time format milliseconds"; |
| 85 | + if (mci.send(cmd) == false) |
| 86 | + return false; |
| 87 | + cmd = "status " + alias + " length"; |
| 88 | + if (mci.send(cmd) == false) |
| 89 | + return false; |
| 90 | + length_ms = atoi(mci.buf); |
| 91 | + return true; |
| 92 | +} |
| 93 | +bool AudioClip::play(int start_ms , int end_ms) |
| 94 | +{ |
| 95 | + if (end_ms == -1) end_ms = length_ms; |
| 96 | + std::string cmd; |
| 97 | + char start_str[16], end_str[16]; |
| 98 | + _itoa(start_ms, start_str,10); |
| 99 | + _itoa(end_ms, end_str, 10); |
| 100 | + cmd = "play " + alias+" from "; |
| 101 | + cmd.append(start_str); |
| 102 | + cmd.append(" to "); |
| 103 | + cmd.append(end_str); |
| 104 | + return mci.send(cmd); |
| 105 | +} |
| 106 | +bool AudioClip::stop() |
| 107 | +{ |
| 108 | + std::string cmd; |
| 109 | + cmd = "stop "+alias; |
| 110 | + if (mci.send(cmd) == false) |
| 111 | + return false; |
| 112 | + cmd = "seek " + alias + " to start"; |
| 113 | + if (mci.send(cmd) == false) |
| 114 | + return false; |
| 115 | + return true; |
| 116 | +} |
| 117 | +bool AudioClip::pause() |
| 118 | +{ |
| 119 | + std::string cmd; |
| 120 | + cmd = "pause " + alias; |
| 121 | + if (mci.send(cmd) == false) |
| 122 | + return false; |
| 123 | + return true; |
| 124 | +} |
| 125 | +bool AudioClip::unpause() |
| 126 | +{ |
| 127 | + std::string cmd; |
| 128 | + cmd = "resume " + alias; |
| 129 | + if (mci.send(cmd) == false) |
| 130 | + return false; |
| 131 | + return true; |
| 132 | +} |
| 133 | +int AudioClip::milliseconds() |
| 134 | +{ |
| 135 | + return length_ms; |
| 136 | +} |
| 137 | +#endif |
0 commit comments