OSDN Git Service

pyaudioを使って録音できるようになりました
authoryamat0jp <yamat0jp@yahoo.co.jp>
Sat, 18 Aug 2018 12:09:23 +0000 (21:09 +0900)
committeryamat0jp <yamat0jp@yahoo.co.jp>
Sat, 18 Aug 2018 12:09:23 +0000 (21:09 +0900)
開発者の人はP4Dをゲットしてください

Unit2.dfm
Unit2.pas
changer.dproj
changer.res
effect.pas
voice.py [new file with mode: 0644]

index 8d84c91..147c170 100644 (file)
--- a/Unit2.dfm
+++ b/Unit2.dfm
@@ -20,18 +20,32 @@ object Form2: TForm2
     Height = 23
     OnClick = SpeedButton1Click
   end
+  object button3: TLabel
+    Left = 34
+    Top = 245
+    Width = 139
+    Height = 26
+    Caption = 'pyaudio'#12391'10'#31186#38291#37682#38899#12375#12414#12377' '#27425#12395'GO'#12508#12479#12531#12434#12463#12522#12483#12463
+    WordWrap = True
+  end
+  object Label1: TLabel
+    Left = 34
+    Top = 159
+    Width = 79
+    Height = 13
+    Caption = 'python27.dll'#12497#12473
+  end
   object MediaPlayer1: TMediaPlayer
     Left = 136
     Top = 120
-    Width = 113
+    Width = 85
     Height = 30
-    VisibleButtons = [btPlay, btPause, btStop, btRecord]
+    VisibleButtons = [btPlay, btPause, btStop]
     AutoOpen = True
     DeviceType = dtWaveAudio
     DoubleBuffered = True
     ParentDoubleBuffered = False
     TabOrder = 0
-    OnClick = MediaPlayer1Click
     OnMouseEnter = MediaPlayer1MouseEnter
   end
   object Edit1: TEdit
@@ -43,8 +57,8 @@ object Form2: TForm2
     Text = 'Edit1'
   end
   object Button1: TButton
-    Left = 288
-    Top = 176
+    Left = 320
+    Top = 125
     Width = 75
     Height = 25
     Caption = 'GO'
@@ -59,6 +73,76 @@ object Form2: TForm2
     ItemHeight = 13
     TabOrder = 3
   end
+  object Memo1: TMemo
+    Left = 240
+    Top = 178
+    Width = 185
+    Height = 89
+    Lines.Strings = (
+      'import pyaudio  #'#37682#38899#27231#33021#12434#20351#12358#12383#12417#12398#12521#12452#12502#12521#12522
+      'import wave     #wav'#12501#12449#12452#12523#12434#25201#12358#12383#12417#12398#12521#12452#12502#12521#12522
+      ''
+      'RECORD_SECONDS = 10 #'#37682#38899#12377#12427#26178#38291#12398#38263#12373#65288#31186#65289
+      'WAVE_OUTPUT_FILENAME = "temp.wav" #'#38899#22768#12434#20445#23384#12377#12427#12501#12449#12452#12523#21517
+      'iDeviceIndex = 0 #'#37682#38899#12487#12496#12452#12473#12398#12452#12531#12487#12483#12463#12473#30058#21495
+      ''
+      '#'#22522#26412#24773#22577#12398#35373#23450
+      'FORMAT = pyaudio.paInt16 #'#38899#22768#12398#12501#12457#12540#12510#12483#12488
+      'CHANNELS = 2             #'#12473#12486#12524#12458
+      'RATE = 44100             #'#12469#12531#12503#12523#12524#12540#12488
+      'CHUNK = 2**11            #'#12487#12540#12479#28857#25968
+      'audio = pyaudio.PyAudio() #pyaudio.PyAudio()'
+      ''
+      'stream = audio.open(format=FORMAT, channels=CHANNELS,'
+      '        rate=RATE, input=True,'
+      '        input_device_index = iDeviceIndex, #'#37682#38899#12487#12496#12452#12473#12398#12452#12531#12487#12483#12463#12473#30058#21495
+      '        frames_per_buffer=CHUNK)'
+      ''
+      '#--------------'#37682#38899#38283#22987'---------------'
+      ''
+      'print ("recording...")'
+      'frames = []'
+      'for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):'
+      '#while True:'
+      '    data = stream.read(CHUNK)'
+      '    frames.append(data)'
+      ''
+      ''
+      'print ("finished recording")'
+      ''
+      '#--------------'#37682#38899#32066#20102'---------------'
+      ''
+      'stream.stop_stream()'
+      'stream.close()'
+      'audio.terminate()'
+      ''
+      'waveFile = wave.open(WAVE_OUTPUT_FILENAME, '#39'wb'#39')'
+      'waveFile.setnchannels(CHANNELS)'
+      'waveFile.setsampwidth(audio.get_sample_size(FORMAT))'
+      'waveFile.setframerate(RATE)'
+      'waveFile.writeframes(b'#39#39'.join(frames))'
+      'waveFile.close()')
+    TabOrder = 4
+    Visible = False
+    WordWrap = False
+  end
+  object Button2: TButton
+    Left = 64
+    Top = 214
+    Width = 75
+    Height = 25
+    Caption = 'record'
+    TabOrder = 5
+    OnClick = Button2Click
+  end
+  object Edit2: TEdit
+    Left = 34
+    Top = 178
+    Width = 187
+    Height = 21
+    TabOrder = 6
+    Text = 'C:\Users\yamat\Anaconda2'
+  end
   object OpenDialog1: TOpenDialog
     Filter = 'wav file|*.wav'
     Left = 528
@@ -70,4 +154,17 @@ object Form2: TForm2
     Left = 528
     Top = 168
   end
+  object PythonEngine1: TPythonEngine
+    DllPath = 'C:\Users\yamat\Anaconda2'
+    IO = PythonInputOutput1
+    Left = 432
+    Top = 168
+  end
+  object PythonInputOutput1: TPythonInputOutput
+    OnSendData = PythonInputOutput1SendData
+    UnicodeIO = False
+    RawOutput = False
+    Left = 432
+    Top = 216
+  end
 end
index 42389ef..f79becb 100644 (file)
--- a/Unit2.pas
+++ b/Unit2.pas
@@ -6,7 +6,8 @@ uses
   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
   System.Classes, Vcl.Graphics,
   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Buttons, Vcl.StdCtrls, Vcl.MPlayer,
-  effect, selectFile, spWav, wav, WriteHeader;
+  effect, selectFile, spWav, wav, WriteHeader, PythonEngine,
+  PythonGUIInputOutput;
 
 type
   TForm2 = class(TForm)
@@ -17,11 +18,19 @@ type
     SaveDialog1: TSaveDialog;
     Button1: TButton;
     ListBox1: TListBox;
+    PythonEngine1: TPythonEngine;
+    Memo1: TMemo;
+    Button2: TButton;
+    button3: TLabel;
+    Edit2: TEdit;
+    Label1: TLabel;
+    PythonInputOutput1: TPythonInputOutput;
     procedure SpeedButton1Click(Sender: TObject);
     procedure Button1Click(Sender: TObject);
     procedure MediaPlayer1MouseEnter(Sender: TObject);
-    procedure MediaPlayer1Click(Sender: TObject; Button: TMPBtnType;
-      var DoDefault: Boolean);
+    procedure Button2Click(Sender: TObject);
+    procedure PythonInputOutput1SendData(Sender: TObject;
+      const Data: AnsiString);
   private
     { Private \90é\8c¾ }
   public
@@ -80,33 +89,10 @@ begin
   Finalize(sp.pWav^);
 end;
 
-procedure TForm2.MediaPlayer1Click(Sender: TObject; Button: TMPBtnType;
-  var DoDefault: Boolean);
-var
-  s: TFileStream;
+procedure TForm2.Button2Click(Sender: TObject);
 begin
-  if Button = btRecord then
-  begin
-    MediaPlayer1.fileName := 'temp.wav';
-    if FileExists('temp.wav') = false then
-    begin
-    {
-      sp.channels:=2;
-      sp.samplePerSec:=44100;
-      sp.bytesPerSec:=176400;
-      sp.bitsPerSample:=16;
-      sp.posOfData:=44;
-      sp.sizeOfData:=0;
-      s:=TFileStream.Create('temp.wav',fmCreate);
-      try
-        waveHeaderWrite(s,sp);
-      finally
-        s.Free;
-      end;
-      }
-    end;
-    MediaPlayer1.Open;
-  end;
+  ListBox1.Items.Clear;
+  PythonEngine1.ExecStrings(Memo1.Lines);
 end;
 
 procedure TForm2.MediaPlayer1MouseEnter(Sender: TObject);
@@ -129,6 +115,12 @@ begin
   end;
 end;
 
+procedure TForm2.PythonInputOutput1SendData(Sender: TObject;
+  const Data: AnsiString);
+begin
+  ListBox1.Items.Add(Data);
+end;
+
 procedure TForm2.SpeedButton1Click(Sender: TObject);
 begin
   if OpenDialog1.Execute = true then
index 6cba938..20699c4 100644 (file)
@@ -5,8 +5,8 @@
         <FrameworkType>VCL</FrameworkType>
         <MainSource>changer.dpr</MainSource>
         <Base>True</Base>
-        <Config Condition="'$(Config)'==''">Debug</Config>
-        <Platform Condition="'$(Platform)'==''">Win32</Platform>
+        <Config Condition="'$(Config)'==''">Release</Config>
+        <Platform Condition="'$(Platform)'==''">Win64</Platform>
         <TargetedPlatforms>3</TargetedPlatforms>
         <AppType>Application</AppType>
     </PropertyGroup>
index 8bd5eb1..72ad27e 100644 (file)
Binary files a/changer.res and b/changer.res differ
index 5a19362..2b5486d 100644 (file)
@@ -16,7 +16,7 @@ function effect16BitWav(const sp: SpParam): integer;
 const
   j = 24;
 var
-  i, a, b, pmin, pmax: integer;
+  i, k, a, b, pmin, pmax: integer;
   len, temp_size, offset0, offset1, p, q: integer;
   m, ma, pitch, rate: Single;
   pMem, pCpy, pRes: array of SmallInt;
@@ -75,8 +75,8 @@ begin
       inc(offset1, p + q);
     end;
     pitch := 1.5;
-    len := trunc(len / pitch);
-    for i := 0 to len - 1 do
+    k := trunc(len / pitch);
+    for i := 0 to k - 1 do
     begin
       m := pitch * i;
       q := trunc(m);
diff --git a/voice.py b/voice.py
new file mode 100644 (file)
index 0000000..28909b5
--- /dev/null
+++ b/voice.py
@@ -0,0 +1,50 @@
+# -*- coding: utf-8 -*-
+"""
+Created on Sat Aug 18 19:46:34 2018
+
+@author: fuke masasi
+"""
+
+import pyaudio  #録音機能を使うためのライブラリ
+import wave     #wavファイルを扱うためのライブラリ
+RECORD_SECONDS = 10 #録音する時間の長さ(秒)
+WAVE_OUTPUT_FILENAME = "sample.wav" #音声を保存するファイル名
+iDeviceIndex = 0 #録音デバイスのインデックス番号
+#基本情報の設定
+FORMAT = pyaudio.paInt16 #音声のフォーマット
+CHANNELS = 2             #ステレオ
+RATE = 44100             #サンプルレート
+CHUNK = 2**11            #データ点数
+audio = pyaudio.PyAudio() #pyaudio.PyAudio()
+stream = audio.open(format=FORMAT, channels=CHANNELS,
+        rate=RATE, input=True,
+        input_device_index = iDeviceIndex, #録音デバイスのインデックス番号
+        frames_per_buffer=CHUNK)
+#--------------録音開始---------------
+print ("recording...")
+frames = []
+#for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
+while True:
+    data = stream.read(CHUNK)
+    frames.append(data)
+print ("finished recording")
+#--------------録音終了---------------
+stream.stop_stream()
+stream.close()
+audio.terminate()
+waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
+waveFile.setnchannels(CHANNELS)
+waveFile.setsampwidth(audio.get_sample_size(FORMAT))
+waveFile.setframerate(RATE)
+waveFile.writeframes(b''.join(frames))
+waveFile.close()
\ No newline at end of file