示例#1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        /// <param name="provider"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            IWindowsFormsEditorService editorService = null;

            if (provider != null)
            {
                editorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
            }

            if (editorService == null)
            {
                return(value);
            }

            PcmFileLoaderEditorAttribute att = (PcmFileLoaderEditorAttribute)context.PropertyDescriptor.Attributes[typeof(PcmFileLoaderEditorAttribute)];

            // CurrencyValueEditorForm を使用したプロパティエディタの表示
            using (OpenFileDialog frm = new OpenFileDialog())
            {
                if (att != null)
                {
                    frm.Filter = att.Exts;
                }
                else
                {
                    frm.Filter = "All Files(*.*)|*.*";
                }

                while (true)
                {
                    var result = frm.ShowDialog();
                    if (result != DialogResult.OK)
                    {
                        break;
                    }

                    var fn = frm.FileName;
                    try
                    {
                        if (!Path.GetExtension(fn).Equals(".wav", StringComparison.OrdinalIgnoreCase))
                        {
                            List <byte> buf = new List <byte>();
                            foreach (byte data in File.ReadAllBytes(fn))
                            {
                                buf.Add(data);
                                if (att != null && att.MaxSize != 0 && att.MaxSize == buf.Count)
                                {
                                    break;
                                }
                            }
                            object rvalue = convertRawToRetValue(context, buf.ToArray());
                            if (rvalue != null)
                            {
                                return(rvalue);
                            }
                            return(value);
                        }
                        else
                        {
                            var data = WaveFileReader.ReadWaveFile(fn);

                            if (att.Bits != 0 && att.Bits != data.BitPerSample ||
                                att.Rate != 0 && att.Rate != data.SampleRate ||
                                att.Channels != 0 && att.Channels != data.Channel)
                            {
                                throw new FileLoadException(
                                          string.Format($"Incorrect wave format(Expected Ch={att.Channels} Bit={att.Bits}, Rate={att.Rate},{2})"));
                            }

                            if (data.Data != null)
                            {
                                object rvalue = convertToRetValue(context, data.Data);
                                if (rvalue != null)
                                {
                                    return(rvalue);
                                }
                            }
                            return(value);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (ex.GetType() == typeof(Exception))
                        {
                            throw;
                        }
                        else if (ex.GetType() == typeof(SystemException))
                        {
                            throw;
                        }

                        MessageBox.Show(ex.ToString());
                    }
                }
                return(value);
            }
        }
示例#2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        /// <param name="provider"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            IWindowsFormsEditorService editorService = null;

            if (provider != null)
            {
                editorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
            }

            if (editorService == null)
            {
                return(value);
            }

            PcmFileLoaderEditorAttribute att = (PcmFileLoaderEditorAttribute)context.PropertyDescriptor.Attributes[typeof(PcmFileLoaderEditorAttribute)];

            // CurrencyValueEditorForm を使用したプロパティエディタの表示
            using (OpenFileDialog frm = new OpenFileDialog())
            {
                if (att != null)
                {
                    frm.Filter = att.Exts;
                }
                else
                {
                    frm.Filter = "All Files(*.*)|*.*";
                }

                while (true)
                {
                    var result = frm.ShowDialog();
                    if (result != DialogResult.OK)
                    {
                        break;
                    }

                    var fn = frm.FileName;
                    try
                    {
                        if (!Path.GetExtension(fn).Equals(".wav", StringComparison.OrdinalIgnoreCase))
                        {
                            List <byte> buf = new List <byte>();
                            foreach (byte data in File.ReadAllBytes(fn))
                            {
                                buf.Add(data);
                                if (att != null && att.MaxSize != 0 && att.MaxSize == buf.Count)
                                {
                                    break;
                                }
                            }
                            object rvalue = convertRawToRetValue(context, buf.ToArray());
                            if (rvalue != null)
                            {
                                return(rvalue);
                            }
                            return(value);
                        }
                        else
                        {
                            var data = WaveFileReader.ReadWaveFile(fn);

                            if (att.Bits != 0 && att.Bits != data.BitPerSample ||
                                att.Rate != 0 && att.Rate != data.SampleRate ||
                                att.Channels != 0 && att.Channels != data.Channel)
                            {
                                throw new FileLoadException(
                                          string.Format($"Incorrect wave format(Expected Ch={att.Channels} Bit={att.Bits}, Rate={att.Rate},{2})"));
                            }

                            if (data.Data != null)
                            {
                                // byte[] -> short[]
                                List <short> wav = new List <short>();
                                for (int i = 0; i < data.Data.Length; i += 2)
                                {
                                    wav.Add((short)((data.Data[i + 1] << 8) | data.Data[i]));
                                }
                                // Encode
                                int max = 0;
                                if (att != null && att.MaxSize != 0)
                                {
                                    max = att.MaxSize;
                                }
                                byte[] adpcmData = encodeAdpcm(wav.ToArray(), max);

                                switch (context.Instance)
                                {
                                case YM2608Timbre tim:
                                    tim.BaseFreqency = 440d * 55000d / (double)data.SampleRate;
                                    tim.TimbreName   = Path.GetFileNameWithoutExtension(fn);
                                    break;

                                case YM2610BTimbre tim:
                                    tim.BaseFreqency = 440d * 55000d / (double)data.SampleRate;
                                    tim.TimbreName   = Path.GetFileNameWithoutExtension(fn);
                                    break;
                                }

                                // byte[] -> byte[]
                                object rvalue = convertToRetValue(context, adpcmData);
                                if (rvalue != null)
                                {
                                    return(rvalue);
                                }
                            }
                            return(value);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (ex.GetType() == typeof(Exception))
                        {
                            throw;
                        }
                        else if (ex.GetType() == typeof(SystemException))
                        {
                            throw;
                        }

                        MessageBox.Show(ex.ToString());
                    }
                }
                return(value);
            }
        }