-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3a97206
commit 2913431
Showing
3 changed files
with
5 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1 @@ | ||
import dash | ||
from dash import dcc, html | ||
from dash.dependencies import Input, Output, State | ||
from pydub import AudioSegment | ||
from django_plotly_dash import DjangoDash | ||
import numpy as np | ||
import time | ||
|
||
app = dash.Dash(__name__) | ||
|
||
# Define a function to load audio data and duration | ||
def loadAudioData(audioPath): | ||
audio = AudioSegment.from_file(audioPath) | ||
audio_data = np.array(audio.get_array_of_samples()) | ||
audio_duration = len(audio_data) / audio.frame_rate | ||
subsampling_factor = 1 | ||
audio_data = audio_data[::subsampling_factor] | ||
|
||
target_duration = 0.877 | ||
num_repeats = 1 | ||
audio_data = np.tile(audio_data, num_repeats) | ||
sample_rate = 44100 | ||
duration = len(audio_data) / sample_rate | ||
|
||
return {'audio_data': audio_data.tolist(), 'audio_duration': duration} | ||
|
||
# Layout of the app | ||
app.layout = html.Div([ | ||
dcc.Input(id='audio-path-input',type='text',placeholder='Enter audio path...',style={'display':'none'}), | ||
dcc.Graph(id='animated-audio-chart', style={'height': '95vh'}, config={'responsive': True}), | ||
dcc.Store(id='audio-data-store', data={'audio_data': [], 'audio_duration': 0}), | ||
dcc.Store(id='interval-store', data=time.time()), # Store the start time and set default heart rate to 60 | ||
dcc.Interval(id='interval-component', interval=25, n_intervals=0) # Interval in milliseconds | ||
]) | ||
|
||
# Clientside callback to update the graph | ||
app.clientside_callback( | ||
""" | ||
function(n, audioData, startTime) { | ||
// Get the audio data and duration from the stored data | ||
var audioArray = audioData['audio_data']; | ||
var audioDuration = audioData['audio_duration']; | ||
var heartRate = 60 | ||
// Calculate the time passed since the start | ||
var currentTime = new Date().getTime() / 1000; // Convert milliseconds to seconds | ||
var elapsedTime = currentTime - startTime; | ||
// Calculate the position of the vertical line | ||
var timePerBeat = 60 / heartRate; | ||
var linePosition = Math.floor((elapsedTime % timePerBeat) * audioArray.length / timePerBeat); | ||
// Create the figure | ||
var figure = { | ||
data: [ | ||
{y: audioArray, type: 'line', name: 'HBR Signal', line: {color: 'green'}}, | ||
{x: [linePosition, linePosition], y: [Math.min.apply(null, audioArray), Math.max.apply(null, audioArray)], mode: 'lines', line: {color: 'black', width: 10}} | ||
], | ||
layout: { | ||
showlegend: false, | ||
paper_bgcolor: 'black', // Set background color to black | ||
plot_bgcolor: 'black' // Set plot background color to black | ||
} | ||
}; | ||
// Return the updated figure, startTime, and heartRate | ||
return [figure, startTime]; | ||
} | ||
""", | ||
Output('animated-audio-chart', 'figure'), | ||
Output('interval-store', 'data'), | ||
Input('interval-component', 'n_intervals'), | ||
State('audio-data-store', 'data'), | ||
State('interval-store', 'data'), | ||
prevent_initial_call=True | ||
) | ||
|
||
# Callback to load audio data when the input changes | ||
@app.callback( | ||
Output('audio-data-store', 'data'), | ||
Input('audio-path-input', 'value') | ||
) | ||
def update_audio_path(audioPath): | ||
input_string = 'app/static/audio/heart/normal_heart/E/combined_audio.wav' | ||
return loadAudioData(input_string) | ||
|
||
if __name__ == '__main__': | ||
app.run_server(debug=True) | ||
"normal_heart_sound_pulmonary_valve","split_first_heart_sound_pulmonary_valve","split_second_heart_sound_pulmonary_valve","third_heart_sound_pulmonary_valve","fourth_heart_sound_pulmonary_valve","functional_murmur_sound_pulmonary_valve","diastolic_murmur_sound_pulmonary_valve","opening_snap_sound_pulmonary_valve","holosystolic_murmur_sound_pulmonary_valve","early_systolic_murmur_sound_pulmonary_valve","mid_systolic_murmur_sound_pulmonary_valve","continuous_murmur_sound_pulmonary_valve","austin_flint_murmur_sound_pulmonary_valve","pericardial_rub_sound_pulmonary_valve","graham_steell_murmur_sound_pulmonary_valve","pulmonary_valve_regurgitation_sound_pulmonary_valve","pulmonary_valve_stenosis_sound_pulmonary_valve","pulmonary_valve_stenosis_regurgitation_sound_pulmonary_valve","congenital_pulmonary_stenosis_sound_pulmonary_valve","pulmonary_valve_regurgitation_sound_pulmonary_valve","pulmonary_valve_stenosis_sound_pulmonary_valve","pulmonary_valve_prolapse_sound_pulmonary_valve","pulmonary_stenosis_regurgitation_sound_pulmonary_valve","pulmonary_stenosis_tricuspid_regurgitation_sound_pulmonary_valve","pulmonary_valve_stenosis_sound_pulmonary_valve","pulmonary_valve_regurgitation_sound_pulmonary_valve","tricuspid_valve_regurgitation_sound_pulmonary_valve","coarctation_of_the_aorta_sound_pulmonary_valve","hypertrophic_cardiomyopathy_sound_pulmonary_valve","patent_ductus_arteriosus_sound_pulmonary_valve","atrial_septal_defect_sound_pulmonary_valve","ventricular_septal_defect_sound_pulmonary_valve","acute_myocardial_infarction_sound_pulmonary_valve","congestive_heart_failure_sound_pulmonary_valve","systemic_hypertension_sound_pulmonary_valve","acute_pericarditis_sound_pulmonary_valve","dilated_cardiomyopathy_sound_pulmonary_valve","pulmonary_hypertension_sound_pulmonary_valve","tetralogy_of_fallot_sound_pulmonary_valve","ventricular_aneurysm_sound_pulmonary_valve","ebstein_anomaly_sound_pulmonary_valve" |