Browse Source

Musik Nochtfilter

Musikfilter funktion noch fixen
master
seyffejn 4 years ago
parent
commit
5260a615d5

+ 54
- 40
TinnitusAnalyse/.idea/workspace.xml View File

@@ -3,8 +3,9 @@
<component name="ChangeListManager">
<list default="true" id="5b3f2136-e422-4072-84be-e86e40fa2d55" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/SoundGenerator.py" beforeDir="false" afterPath="$PROJECT_DIR$/SoundGenerator.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/TinnitusAnalyse_GUI.py" beforeDir="false" afterPath="$PROJECT_DIR$/TinnitusAnalyse_GUI.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/output/MeinTinnitus.wav" beforeDir="false" afterPath="$PROJECT_DIR$/output/MeinTinnitus.wav" afterDir="false" />
<change beforePath="$PROJECT_DIR$/TinnitusDaten.csv" beforeDir="false" afterPath="$PROJECT_DIR$/TinnitusDaten.csv" afterDir="false" />
</list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" />
@@ -14,11 +15,11 @@
</component>
<component name="FileEditorManager">
<leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
<file pinned="false" current-in-tab="true">
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/TinnitusAnalyse_GUI.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="321">
<caret line="198" column="81" lean-forward="true" selection-start-line="198" selection-start-column="81" selection-end-line="198" selection-end-column="81" />
<state relative-caret-position="767">
<caret line="398" column="40" lean-forward="true" selection-start-line="398" selection-start-column="40" selection-end-line="398" selection-end-column="40" />
<folding>
<element signature="e#0#30#0" expanded="true" />
</folding>
@@ -27,10 +28,10 @@
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$USER_HOME$/AppData/Local/Programs/Python/Python35/Lib/site-packages/scipy/signal/filter_design.py">
<entry file="file://$USER_HOME$/AppData/Local/Programs/Python/Python35/Lib/site-packages/pydub/utils.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="884">
<caret line="2211" column="41" selection-start-line="2211" selection-start-column="41" selection-end-line="2211" selection-end-column="41" />
<state relative-caret-position="313">
<caret line="169" selection-start-line="169" selection-end-line="169" />
</state>
</provider>
</entry>
@@ -38,17 +39,17 @@
<file pinned="false" current-in-tab="false">
<entry file="file://$USER_HOME$/AppData/Local/Programs/Python/Python35/Lib/tkinter/__init__.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="17034">
<caret line="1004" column="30" selection-start-line="1004" selection-start-column="30" selection-end-line="1004" selection-end-column="30" />
<state relative-caret-position="313">
<caret line="1557" selection-start-line="1557" selection-end-line="1557" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/SoundGenerator.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="942">
<caret line="141" column="36" selection-start-line="141" selection-start-column="36" selection-end-line="141" selection-end-column="36" />
<state relative-caret-position="523">
<caret line="252" column="36" lean-forward="true" selection-start-line="252" selection-start-column="36" selection-end-line="252" selection-end-column="36" />
<folding>
<element signature="e#0#42#0" expanded="true" />
</folding>
@@ -56,15 +57,6 @@
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$USER_HOME$/AppData/Local/Programs/Python/Python35/Lib/site-packages/sounddevice.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="24106">
<caret line="1423" column="8" selection-start-line="1423" selection-start-column="8" selection-end-line="1423" selection-end-column="8" />
</state>
</provider>
</entry>
</file>
</leaf>
</component>
<component name="FileTemplateManagerImpl">
@@ -97,6 +89,7 @@
<find>unten_button_play_press</find>
<find>StringVar</find>
<find>Speichern</find>
<find>csv</find>
</findStrings>
<dirStrings>
<dir>C:\Users\Julian\PycharmProjects\TinnitusAnalyse</dir>
@@ -114,13 +107,13 @@
<option value="$PROJECT_DIR$/DigitalFilter.py" />
<option value="$PROJECT_DIR$/../../../DigitalFilter.py" />
<option value="$PROJECT_DIR$/DigitalFilterTest2.py" />
<option value="$PROJECT_DIR$/SoundGenerator.py" />
<option value="$PROJECT_DIR$/DigitalFilterTest.py" />
<option value="$PROJECT_DIR$/TinnitusAnalyse_GUI.py" />
<option value="$PROJECT_DIR$/SoundGenerator.py" />
</list>
</option>
</component>
<component name="ProjectFrameBounds" extendedState="7">
<component name="ProjectFrameBounds" extendedState="6">
<option name="x" value="407" />
<option name="y" value="-8" />
<option name="width" value="1113" />
@@ -295,14 +288,14 @@
<window_info id="Favorites" order="2" side_tool="true" />
<window_info anchor="bottom" id="Message" order="0" />
<window_info anchor="bottom" id="Find" order="1" />
<window_info active="true" anchor="bottom" id="Run" order="2" visible="true" weight="0.24707717" />
<window_info active="true" anchor="bottom" id="Run" order="2" sideWeight="0.49960598" visible="true" weight="0.24707717" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.3990647" />
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
<window_info anchor="bottom" id="TODO" order="6" />
<window_info anchor="bottom" id="Version Control" order="7" />
<window_info anchor="bottom" id="Terminal" order="8" weight="0.32969603" />
<window_info anchor="bottom" id="Event Log" order="9" side_tool="true" />
<window_info anchor="bottom" id="Event Log" order="9" sideWeight="0.500394" side_tool="true" weight="0.24707717" />
<window_info anchor="bottom" id="Python Console" order="10" weight="0.32969603" />
<window_info anchor="right" id="Commander" internal_type="SLIDING" order="0" type="SLIDING" weight="0.4" />
<window_info anchor="right" id="Ant Build" order="1" weight="0.25" />
@@ -361,46 +354,67 @@
</state>
</provider>
</entry>
<entry file="file://$USER_HOME$/AppData/Local/Programs/Python/Python35/Lib/tkinter/__init__.py">
<entry file="file://$USER_HOME$/AppData/Local/Programs/Python/Python35/Lib/tkinter/filedialog.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="313">
<caret line="374" selection-start-line="374" selection-end-line="374" />
</state>
</provider>
</entry>
<entry file="file://$USER_HOME$/AppData/Local/Programs/Python/Python35/Lib/site-packages/scipy/signal/filter_design.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="884">
<caret line="2211" column="41" selection-start-line="2211" selection-start-column="41" selection-end-line="2211" selection-end-column="41" />
</state>
</provider>
</entry>
<entry file="file://$USER_HOME$/AppData/Local/Programs/Python/Python35/Lib/tkinter/commondialog.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="17034">
<caret line="1004" column="30" selection-start-line="1004" selection-start-column="30" selection-end-line="1004" selection-end-column="30" />
<state relative-caret-position="618">
<caret line="47" selection-start-line="47" selection-end-line="47" />
</state>
</provider>
</entry>
<entry file="file://$USER_HOME$/AppData/Local/Programs/Python/Python35/Lib/site-packages/sounddevice.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="24106">
<state relative-caret-position="1037">
<caret line="1423" column="8" selection-start-line="1423" selection-start-column="8" selection-end-line="1423" selection-end-column="8" />
</state>
</provider>
</entry>
<entry file="file://$USER_HOME$/AppData/Local/Programs/Python/Python35/Lib/site-packages/scipy/signal/filter_design.py">
<entry file="file://$USER_HOME$/AppData/Local/Programs/Python/Python35/Lib/site-packages/pydub/utils.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="884">
<caret line="2211" column="41" selection-start-line="2211" selection-start-column="41" selection-end-line="2211" selection-end-column="41" />
<state relative-caret-position="313">
<caret line="169" selection-start-line="169" selection-end-line="169" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/SoundGenerator.py">
<entry file="file://$USER_HOME$/AppData/Local/Programs/Python/Python35/Lib/tkinter/__init__.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="942">
<caret line="141" column="36" selection-start-line="141" selection-start-column="36" selection-end-line="141" selection-end-column="36" />
<folding>
<element signature="e#0#42#0" expanded="true" />
</folding>
<state relative-caret-position="313">
<caret line="1557" selection-start-line="1557" selection-end-line="1557" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/TinnitusAnalyse_GUI.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="321">
<caret line="198" column="81" lean-forward="true" selection-start-line="198" selection-start-column="81" selection-end-line="198" selection-end-column="81" />
<state relative-caret-position="767">
<caret line="398" column="40" lean-forward="true" selection-start-line="398" selection-start-column="40" selection-end-line="398" selection-end-column="40" />
<folding>
<element signature="e#0#30#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/SoundGenerator.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="523">
<caret line="252" column="36" lean-forward="true" selection-start-line="252" selection-start-column="36" selection-end-line="252" selection-end-column="36" />
<folding>
<element signature="e#0#42#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
</component>
</project>

+ 99
- 3
TinnitusAnalyse/SoundGenerator.py View File

@@ -4,7 +4,8 @@ import sounddevice as sd
import numpy as np
import sys # für Fehlermeldungen
from scipy import signal

import csv
import matplotlib.pyplot as plt
"""---------------------------------------------------------------------------------------------------------------------
In .wav-Dateien wird der Ton in absoluten Werte eingetragen. Die Standart-framerate ist 44100
das heißt für jede Sekunde an Ton gibt es 44100 Werte, die die Tonwelle über die Zeit beschreiben
@@ -76,6 +77,9 @@ class Sound:
self.sound_obj = sd.OutputStream(channels=2, callback=self.callback,
samplerate=self.framerate) # Objekt fürs Abspielen (siehe sound.play())
self.start_idx = 0 # wird für sound_obj benötigt
self.music_samplerate = 0 # die samplerate der ausgewählten Musikdatei
self.music_data = 0 # das Numpy Array der ausgewählten Musikdatei


def wav_speichern(self): # ezeugt/aktuallisiert die .wav-Datei
print("Sound wird als .wav-Datei gespeichert. Bitte warten...\nDer Vorgang kann ca. 10 Sekunden dauern")
@@ -152,13 +156,105 @@ class Sound:

# Sinus addieren: f(t) = A * sin(2 * pi * f * t)
for x in range(len(outdata)):
# links:
# links: rauschen und sinus wieder zusammen addieren
outdata[x][0] += self.tinnitus.linksLautstaerke * np.sin(2 * np.pi * self.tinnitus.linksFrequenz *
((x + self.start_idx) / self.framerate))
# rechts:
# rechts: rauschen und sinus wieder zusammen addieren
outdata[x][1] += self.tinnitus.rechtsLautstaerke * np.sin(2 * np.pi * self.tinnitus.rechtsFrequenz *
((x + self.start_idx) / self.framerate))

self.start_idx += frames

return outdata

def musik_filtern(self):
""" Die Tinnitus Frequenz aus der Musik Filtern... easy"""

# Die Parameter des Tinnitus aus .csv Datei getten
csvstring = open("TinnitusDaten.csv").read()
#print(csvstring)
x = csvstring.split("\n")
#print("\nx= ", x)

# linke Frequenz aus csv Datei holen
lf = x[2]
lf = lf.split(";")
lf = float(lf[1])


# rechte Frequenz aus csv Datei holen
rf = x[7]
rf = rf.split(";")
rf = float(rf[1])


# linke Lautstärke aus cvs Datei holen
ll = x[3]
ll = ll.split(";")
ll = float(ll[1])

# rechte Lautstärke aus cvs Datei holen
rl = x[8]
rl = rl.split(";")
rl = float(rl[1])

print("lf = ", lf)
print("rf = ", rf)
print("ll = ", ll)
print("rl = ", rl)

# Die Musik filtern
w0 = float(lf/(self.music_samplerate/2)) # Frequency to remove from a signal. If fs is specified, this is in the same units as fs. By default, it is a normalized scalar that must satisfy 0 < w0 < 1, with w0 = 1 corresponding to half of the sampling frequency.
Q = 30.0 # Quality factor. Dimensionless parameter that characterizes notch filter -3 dB bandwidth bw relative to its center frequency, Q = w0/bw.

self.music_data = self.music_data/32767 # convert array from int16 to float

b, a = signal.iirnotch(lf, Q, fs=self.music_samplerate)
if ll != 0.0:
print(self.music_data[20000:20010])
musicLinks = signal.lfilter(b, a , self.music_data[:, 0]) # links
sd.play(musicLinks, self.music_samplerate)
sd.wait()

print(musicLinks[20000:20010])

freq, h = signal.freqz(b, a, fs=self.music_samplerate)
# Plot
fig, ax = plt.subplots(2, 1, figsize=(8, 6))
ax[0].plot(freq, 20 * np.log10(abs(h)), color='blue')
ax[0].set_title("Frequency Response")
ax[0].set_ylabel("Amplitude (dB)", color='blue')
ax[0].set_xlim([0, 10000])
ax[0].set_ylim([-25, 10])
ax[0].grid()
ax[1].plot(freq, np.unwrap(np.angle(h)) * 180 / np.pi, color='green')
ax[1].set_ylabel("Angle (degrees)", color='green')
ax[1].set_xlabel("Frequency (Hz)")
ax[1].set_xlim([0, 10000])
ax[1].set_yticks([-90, -60, -30, 0, 30, 60, 90])
ax[1].set_ylim([-90, 90])
ax[1].grid()
plt.show()


if rl != 0.0:
musicRechts = signal.lfilter(b, a, self.music_data[:, 1]) # rechts



wav_obj = wave.open("musikTest.wav", "w")
# Rahmenparameter für die .wav-Datei setzen


self.nframes = len(musicLinks)
wav_obj.setparams((self.nchannels, self.sampwidth, self.music_samplerate, self.nframes, self.comptype, self.compname))

frames = self.music_samplerate * 10
# Die Audiosamples schreiben
for x in range(frames):
# Die Audiodaten müssen von float in einen passenden int-Wert umgerechnet werden
wav_obj.writeframes(struct.pack('h', int(musicLinks[x] * 32767.0))) # Werte für links und rechts werden bei
wav_obj.writeframes(struct.pack('h', int(musicRechts[x] * 32767.0))) # wav abwechselnd eingetragen

wav_obj.close()
print("Speichern beendet.")

+ 21
- 36
TinnitusAnalyse/TinnitusAnalyse_GUI.py View File

@@ -2,8 +2,12 @@ from tkinter import filedialog
from tkinter.ttk import Frame, Label, Button, LabelFrame, Combobox, Style, Checkbutton, Entry, Panedwindow, Separator
from tkinter import *
from SoundGenerator import *
import csv
import time
import sounddevice as sd
from scipy.io import wavfile





"""--------------------------FUNKTIONEN DIE DURCH GUI KLICKS AUSGEFÜHRT WERDEN---------------------------------------"""
"""-------Funktionen links-------------"""
@@ -176,8 +180,16 @@ def unten_button_musikdatei_laden_press():
""" This function opends a window that lets you select .mp3 and .wav files. The user is supposed to select their
music files here"""
print("button musikdatei laden pressed")
untererFrame.musikdatei = filedialog.askopenfilename(initialdir="/", title="Wähle die Musikdatei deiner Wahl aus (.mp3)",
filetypes=(("wav Dateien", "*.wav"), ("mp3 Dateien", "*.mp3")))

untererFrame.musikdatei = filedialog.askopenfilename(initialdir="/",
title="Wähle die Musikdatei(.wav) deiner Wahl aus",
filetypes=(("wav Dateien", "*.wav"),))

samplerate, data = wavfile.read(untererFrame.musikdatei)

sound.music_samplerate = samplerate # die samplerate der ausgewählten Musikdatei an SoundGenerator.py übergeben
sound.music_data = data # das Numpy Array der ausgewählten Musikdatei an SoundGenerator.py übergeben

print(untererFrame.musikdatei)
untenTextMusikDatei.config(state=NORMAL) # activate text field (otherwise it is readonly)
untenTextMusikDatei.delete(1.0, END) # delete everything from text widget
@@ -187,28 +199,12 @@ def unten_button_musikdatei_laden_press():
feedback("Musikdatei ausgewählt", fontcolor="black") # place text in feedback field, fontcololor, backgroundcolor


def unten_button_tinnitusdatei_laden_press():
""" This function opends a window that lets you select .mp3 and .wav files. The user is supposed to select their
tinnitus files here"""
print("button tinnitusdatei laden pressed")
untererFrame.tinnitusdatei = filedialog.askopenfilename(initialdir="/",
title="Wähle die Musikdatei deiner Wahl aus (.mp3)",
filetypes=(("wav Dateien", "*.wav"), ("mp3 Dateien", "*.mp3")))
print(untererFrame.tinnitusdatei)
untenTextTinnitusDatei.config(state=NORMAL) # activate text field (otherwise it is readonly)
untenTextTinnitusDatei.delete(1.0, END) # delete everything from text widget
untenTextTinnitusDatei.insert(INSERT, untererFrame.musikdatei) # insert selected file path to text widget
untenTextTinnitusDatei.config(state=DISABLED) # activate text field (otherwise it is readonly)

feedback("Tinnitusdatei ausgewählt", fontcolor="black") # place text in feedback field, fontcololor, backgroundcolor

def unten_button_filtere_tinnitus_aus_musik():
print("button filtere tinnitus aus musik pressed")
try:
feedback("Gefilterte Musikdatei erstellt", "blue")

except:
feedback("Fehlgeschlagener Filterversuch!", "red", "white")
feedback("Gefilterte Musikdatei erstellt", "blue")
sound.musik_filtern()
# except:
# feedback("Fehlgeschlagener Filterversuch!", "red", "white")


""" Initialisierungen """
@@ -407,18 +403,7 @@ untenButtonMusikDateiLaden = Button(untererFrame, text="Musikdatei auswählen",
untenButtonMusikDateiLaden.grid(column=7, row=7, sticky=(N+S+E+W))


# --------------- LOAD MEINTINNTISU.WAV FILE---------------------
untenTextTinnitusDatei = Text(untererFrame, height=1, width=50)
untenTextTinnitusDatei.grid(column=0, row=8, sticky=(N+S+E+W), columnspan=6)
untenTextTinnitusDatei.insert(INSERT, "Deine MeinTinnitus.wav Datei hier auswählen") # insert selected file path to text widget
untenTextTinnitusDatei.config(state=DISABLED) # activate text field (otherwise it is readonly)


untenButtonTinnitusDateiLaden = Button(untererFrame, text="Tinnitusdatei auswählen",
command=unten_button_tinnitusdatei_laden_press)
untenButtonTinnitusDateiLaden.grid(column=7, row=8, sticky=(N+S+E+W))


#------------BUTTON FILTERE TINNITUS AUS MUSIK-----------------
untenButtonFiltereTinnitusAusMusik = Button(untererFrame, text="Filtere Tinnitus Frequenzen aus Musik",
command=unten_button_filtere_tinnitus_aus_musik, font="bold",
relief="raised", bg="blue", fg="white",)

+ 11
- 11
TinnitusAnalyse/TinnitusDaten.csv View File

@@ -1,14 +1,14 @@
Vorname;Seyffer
Nachname;JUlian
linke Frequenz;0.0
linke Lautstärke;0.0
linkes Rauschen Lautstärke;0.011
linkes Rauschen untere Grenzfrequenz;4059.9999999999995
Vorname;asd
Nachname;asd
linke Frequenz;950.0
linke Lautstärke;0.2
linkes Rauschen Lautstärke;0.0
linkes Rauschen untere Grenzfrequenz;10.0
linkes Rauschen obere Grenzfrequenz;20000.0
rechte Frequenz;0.0
rechte Lautstärke;0.0
rechtes Rauschen Lautstärke;0.009
rechtes Rauschen untere Grenzfrequenz;1380.0
rechte Frequenz;920.0
rechte Lautstärke;0.28
rechtes Rauschen Lautstärke;0.0
rechtes Rauschen untere Grenzfrequenz;10.0
rechtes Rauschen obere Grenzfrequenz;20000.0
Kommentar;asd
Kommentar;


BIN
TinnitusAnalyse/__pycache__/SoundGenerator.cpython-35.pyc View File


BIN
TinnitusAnalyse/musikTest.wav View File


Loading…
Cancel
Save