Beispiel #1
0
        /// <summary>
        /// 获取时间戳
        /// </summary>
        /// <param name="sound_path"></param>
        /// <param name="word_path"></param>
        /// <param name="language"></param>
        /// <param name="splitTime"></param>
        public void GetTimeSpan(string sound_path, string word_path, string language, double splitTime = 1.5)
        {
            var inputStream = new AudioFileReader(sound_path);

            try
            {
                if (sound_path.Contains(".mp3"))
                {
                    sound_path = NAudioHelper.GetWavPath(sound_path);
                }
                WAVReader reader = new WAVReader();
                reader.GetTimeSpan(sound_path, out voicePoint, out voiceMilliSecond, splitTime);
                if (voicePoint.Count % 2 != 0)//voicePoint是为了截取小音频
                {
                    voicePoint.Add(inputStream.TotalTime);
                }
                int _name = 1;//命名
                for (int i = 0; i < voicePoint.Count; i += 2)
                {
                    GetBaiduSpeech(voicePoint[i], voicePoint[i + 1], sound_path, _name, language, splitTime);
                    ++_name;
                }
                //开始比对
                //获取文档内容
                CompareText(word_path, language);
                //通过时间戳集合去匹配路径与歌词文本(百度不准)
                for (int i = 0; i < voiceMilliSecond.Count; i++)
                {
                    MusicResult result = new MusicResult();
                    result.timeSpan = voiceMilliSecond[i];
                    string url = voiceFiles[i];
                    if (url == "")                                                                    //上传失败,重新上传
                    {
                        string fileName  = Path.GetFileNameWithoutExtension(sound_path) + i + ".wav"; //重命名文件
                        string newFolder = System.AppDomain.CurrentDomain.BaseDirectory + "NewSoundFiles/" + Path.GetFileNameWithoutExtension(sound_path) + "/";
                        string newFile   = newFolder + fileName;
                        //绝对路径
                        string path_absolute = context.Server.MapPath("/NewSoundFiles/" + Path.GetFileNameWithoutExtension(sound_path) + "/" + fileName);
                        result.fileUrl = UploadFile.PostFile(path_absolute);
                    }
                    else
                    {
                        result.fileUrl = i < voiceFiles.Count ? voiceFiles[i] : "";
                    }

                    result.text = i < baiduText.Count ? baiduText[i] : "未匹配上字符串";
                    return_list.Add(result);
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex.Message);
            }
            finally
            {
                inputStream.Close();
                inputStream.Dispose();
                isFinish = true;
            }
        }
Beispiel #2
0
        public MusicResult AddSound(Sound sound)
        {
            using (_context)
            {
                try
                {
                    Guid id = Guid.NewGuid();

                    sound.Id = Convert.ToString(id);
                    _context.Add(sound);

                    _context.SaveChanges();

                    MusicResult result = new MusicResult
                    {
                        Id           = sound.Id,
                        IsSuccessful = true,
                        Message      = "Sound add successful"
                    };

                    return(result);
                }
                catch (Exception exception)
                {
                    MusicResult result = new MusicResult
                    {
                        Id           = sound.Id,
                        IsSuccessful = false,
                        Message      = $"An unexpected error on adding sound: {exception.Message}"
                    };

                    return(result);
                }
            }
        }
Beispiel #3
0
        public MusicResult UpdateSound(Sound sound)
        {
            using (_context)
            {
                try
                {
                    string id     = Convert.ToString(sound.Id);
                    Sound  record = SoundRepository.FindBy(x => x.Id == id).FirstOrDefault();

                    if (record == null)
                    {
                        MusicResult errorResult = new MusicResult
                        {
                            Id           = record.Id,
                            IsSuccessful = false,
                            Message      = $"An unexpected error on finding the sound. Can't find the sound with id : {id}"
                        };

                        return(errorResult);
                    }

                    record.Id       = sound.Id;
                    record.FileName = sound.FileName;
                    record.FilePath = sound.FilePath;
                    record.FileSize = sound.FileSize;
                    record.Modified = DateTime.Now;

                    _context.Update(record);

                    _context.SaveChanges();

                    MusicResult result = new MusicResult
                    {
                        Id           = id,
                        IsSuccessful = true,
                        Message      = "Update sound successfully"
                    };

                    return(result);
                }
                catch (Exception exception)
                {
                    MusicResult result = new MusicResult
                    {
                        Id           = sound.Id,
                        IsSuccessful = false,
                        Message      = $"An unexpected error on updating sound: {exception.Message}"
                    };

                    return(result);
                }
            }
        }
Beispiel #4
0
 public MusicResult CanUserAddToQueue(MusicContext context, bool checkCmds = true)
 {
     if (checkCmds)
     {
         MusicResult mr = CanUserUseMusicCommands(context);
         if (!mr.IsSuccessful)
         {
             return(mr);
         }
     }
     if ((context.AskedBy as IGuildUser).VoiceChannel != voiceChannel)
     {
         return(new MusicResult($"You need to be in the same music **voice** channel as the bot ({voiceChannel.Name})."));
     }
     return(new MusicResult());
 }
Beispiel #5
0
        public async override Task <PreconditionResult> CheckPermissions(ICommandContext context, CommandInfo command, IServiceProvider map) //TODO: Remover connect aqui
        {
            if (!(context.Channel is ITextChannel))
            {
                return(PreconditionResult.FromError("null"));
            }
            if (!(context is MayaCommandContext))
            {
                return(PreconditionResult.FromError("null"));
            }
            MayaCommandContext con = context as MayaCommandContext;

            if (!con.MainHandler.GuildConfigHandler(con.Guild).GetMusic().IsEnabled)
            {
                return(PreconditionResult.FromError("Music system disabled."));
            }
            if (!con.MainHandler.GuildMusicHandler(con.Guild).IsReady())
            {
                return(PreconditionResult.FromError("Music system not ready."));
            }
            MusicResult mr = con.MainHandler.GuildMusicHandler(con.Guild).CanUserUseMusicCommands(new MusicContext(context));

            if (!mr.IsSuccessful)
            {
                return(PreconditionResult.FromError(mr.Error));
            }
            IVoiceChannel vc      = con.MainHandler.GuildMusicHandler(con.Guild).GetVoiceChannel();
            IVoiceChannel channel = (con.Guild as SocketGuild)?.FindVoiceChannel(con.MainHandler.GuildConfigHandler(con.Guild).GetMusic().VoiceChannel);

            if (channel == null)
            {
                return(PreconditionResult.FromError($"Missing voice channel ({con.MainHandler.GuildConfigHandler(con.Guild).GetMusic().VoiceChannel})."));
            }
            else if (vc == null)
            {
                await con.MainHandler.GuildMusicHandler(con.Guild).JoinVoiceChannelAsync(channel);
            }
            else if (vc != channel && !con.MainHandler.GuildMusicHandler(con.Guild).ShouldDownload()) //!download = doing nothing
            {
                await con.MainHandler.GuildMusicHandler(con.Guild).JoinVoiceChannelAsync(channel);
            }
            return(PreconditionResult.FromSuccess());
        }
Beispiel #6
0
        public MusicResult DeleteSound(string id)
        {
            using (_context)
            {
                try
                {
                    var record = SoundRepository.FindBy(x => x.Id == id).FirstOrDefault();
                    if (record == null)
                    {
                        MusicResult errorResult = new MusicResult
                        {
                            Id           = record.Id,
                            IsSuccessful = false,
                            Message      = "There isn't a record in the database. "
                        };
                        return(errorResult);
                    }
                    _context.Remove(record);

                    _context.SaveChanges();

                    MusicResult result = new MusicResult
                    {
                        Id           = id,
                        IsSuccessful = true,
                        Message      = "Delete sound successful"
                    };

                    return(result);
                }
                catch (Exception exception)
                {
                    MusicResult result = new MusicResult
                    {
                        Id           = id,
                        IsSuccessful = false,
                        Message      = $"An unexpected error on deleting sound: {exception.Message}"
                    };

                    return(result);
                }
            }
        }
Beispiel #7
0
 public static MusicResultViewModel ToViewModels(this MusicResult model, IMapper mapper)
 {
     return(mapper.Map <MusicResultViewModel>(model));
 }